Skip to content

Commit eb16ea9

Browse files
committed
eth v1 contract
Implements the version 1 contracts for ethereum and tokens. Based on feedback in #1426, everything is now encoded in the "contract data". This "contract data", is the msgjson.Init.Contract -> msgjson.Audit.Contract -> MatchMetaData.Proof.CounterContract, AuditInfo.Contract -> Redemption.Spends.Contract. A few new terms are introduced to differentiate various encodings and data sets. The aforementioned contract data did encode a version and a secret hash. It now encodes a version and a "locator", which is a []byte whose length and content depend on the version. For version 0, the locator is still just the secretHash[:]. For v1, the locator encodes all of the immutable data that defines the swap. This immutable data is now collected in something called a "vector" (dexeth.SwapVector). For version 0, some vector data is stored on-chain indexed by the secret hash. For version 1, all vector data is encoded in the locator. I've also made an effort to standardize the use of status/step, and eliminated the use of ambiguous "ver" variables throughout. A "status" is now the collection of mutable contract data: the step, the init block height, and the secret. The status and vector collectively fully characterize the swap. client/asset/eth: New contractV1 and tokenContractorV1 interfaces. To avoid duplication, the ERC20 parts of the tokenContractors are separated into a new type erc20Contractor that is embedded by both versions. Getters for status and vector are added in place of the old method "swap". assetWallet and embedding types are updated to work with the new version-dependent locators and the status and vector model. dex/networks/{eth,erc20}: New contracts added. New methods for dealing with locators. Simnet entries added for eth and dextt.eth in the ContractAddresses and Tokens maps. txDataHandler interace is replaced with versioned package-level functions. server/asset/eth: Server is fully switched to version 1. No option to use version 0. Translation to new version was straightforward, with one notable difference that we can no longer get a block height from the contract once the swap is redeemed.
1 parent fd8198e commit eb16ea9

29 files changed

Lines changed: 1548 additions & 1002 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ server/cmd/validatemarkets
4848
client/cmd/translationsreport/translationsreport
4949
client/cmd/translationsreport/worksheets
5050
server/cmd/dexadm/dexadm
51+
server/cmd/dcrdex/evm-protocol-overrides.json

client/asset/eth/cmd/getgas/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ func mainErr() error {
4848
flag.BoolVar(&useMainnet, "mainnet", false, "use mainnet")
4949
flag.BoolVar(&useTestnet, "testnet", false, "use testnet")
5050
flag.BoolVar(&useSimnet, "simnet", false, "use simnet")
51-
flag.BoolVar(&trace, "trace", false, "use simnet")
52-
flag.BoolVar(&debug, "debug", false, "use simnet")
51+
flag.BoolVar(&trace, "trace", false, "use trace logging")
52+
flag.BoolVar(&debug, "debug", false, "use debug logging")
5353
flag.IntVar(&maxSwaps, "n", 5, "max number of swaps per transaction. minimum is 2. test will run from 2 swap up to n swaps.")
5454
flag.StringVar(&chain, "chain", "eth", "symbol of the base chain")
5555
flag.StringVar(&token, "token", "", "symbol of the token. if token is not specified, will check gas for base chain")

client/asset/eth/contractor.go

Lines changed: 20 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ func (c *contractorV0) vector(ctx context.Context, locator []byte) (*dexeth.Swap
220220
return nil, err
221221
}
222222
vector := &dexeth.SwapVector{
223-
From: swap.Participant,
224-
To: swap.Initiator,
225-
Value: dexeth.WeiToGwei(swap.Value),
223+
From: swap.Initiator,
224+
To: swap.Participant,
225+
Value: swap.Value,
226226
SecretHash: secretHash,
227-
LockTime: uint64(swap.LockTime.UnixMilli()),
227+
LockTime: uint64(swap.LockTime.Unix()),
228228
}
229229
return vector, nil
230230
}
@@ -242,11 +242,11 @@ func (c *contractorV0) statusAndVector(ctx context.Context, locator []byte) (*de
242242
return nil, nil, err
243243
}
244244
vector := &dexeth.SwapVector{
245-
From: swap.Participant,
246-
To: swap.Initiator,
247-
Value: dexeth.WeiToGwei(swap.Value),
245+
From: swap.Initiator,
246+
To: swap.Participant,
247+
Value: swap.Value,
248248
SecretHash: secretHash,
249-
LockTime: uint64(swap.LockTime.UnixMilli()),
249+
LockTime: uint64(swap.LockTime.Unix()),
250250
}
251251
status := &dexeth.SwapStatus{
252252
Step: swap.State,
@@ -442,7 +442,6 @@ func (c *erc20Contractor) balance(ctx context.Context) (*big.Int, error) {
442442
// allowance exposes the read-only allowance method of the erc20 token contract.
443443
func (c *erc20Contractor) allowance(ctx context.Context) (*big.Int, error) {
444444
callOpts := &bind.CallOpts{
445-
Pending: true,
446445
From: c.acct,
447446
Context: ctx,
448447
}
@@ -575,8 +574,7 @@ func (c *tokenContractorV0) tokenAddress() common.Address {
575574

576575
type contractorV1 struct {
577576
contractV1
578-
abi *abi.ABI
579-
// net dex.Network
577+
abi *abi.ABI
580578
contractAddr common.Address
581579
acctAddr common.Address
582580
cb bind.ContractBackend
@@ -588,19 +586,18 @@ type contractorV1 struct {
588586
var _ contractor = (*contractorV1)(nil)
589587

590588
func newV1Contractor(net dex.Network, contractAddr, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
591-
592589
c, err := swapv1.NewETHSwap(contractAddr, cb)
593590
if err != nil {
594591
return nil, err
595592
}
596593
return &contractorV1{
597-
contractV1: c,
598-
abi: dexeth.ABIs[1],
599-
// net: net,
594+
contractV1: c,
595+
abi: dexeth.ABIs[1],
600596
contractAddr: contractAddr,
601597
acctAddr: acctAddr,
602598
cb: cb,
603599
atomize: dexeth.WeiToGwei,
600+
evmify: dexeth.GweiToWei,
604601
}, nil
605602
}
606603

@@ -656,7 +653,7 @@ func (c *contractorV1) initiate(txOpts *bind.TransactOpts, contracts []*asset.Co
656653
v := &dexeth.SwapVector{
657654
From: c.acctAddr,
658655
To: common.HexToAddress(ac.Address),
659-
Value: ac.Value,
656+
Value: c.evmify(ac.Value),
660657
LockTime: ac.LockTime,
661658
}
662659
copy(v.SecretHash[:], ac.SecretHash)
@@ -682,7 +679,7 @@ func (c *contractorV1) redeem(txOpts *bind.TransactOpts, redeems []*asset.Redemp
682679

683680
// Not checking version from DecodeLocator because it was already
684681
// audited and incorrect version locator would err below anyway.
685-
_, locator, err := dexeth.DecodeLocator(r.Spends.Contract)
682+
_, locator, err := dexeth.DecodeContractData(r.Spends.Contract)
686683
if err != nil {
687684
return nil, fmt.Errorf("error parsing locator redeem: %w", err)
688685
}
@@ -716,7 +713,7 @@ func (c *contractorV1) estimateInitGas(ctx context.Context, n int) (uint64, erro
716713
SecretHash: secretHash,
717714
Initiator: c.acctAddr,
718715
Participant: common.BytesToAddress(encode.RandomBytes(20)),
719-
Value: 1,
716+
Value: big.NewInt(dexeth.GweiFactor),
720717
})
721718
}
722719

@@ -791,23 +788,23 @@ func (c *contractorV1) isRefundable(locator []byte) (bool, error) {
791788

792789
func (c *contractorV1) incomingValue(ctx context.Context, tx *types.Transaction) (uint64, error) {
793790
if redeems, err := dexeth.ParseRedeemDataV1(tx.Data()); err == nil {
794-
var redeemed uint64
791+
var redeemed *big.Int
795792
for _, r := range redeems {
796-
redeemed += r.Contract.Value
793+
redeemed.Add(redeemed, r.Contract.Value)
797794
}
798-
return redeemed, nil
795+
return c.atomize(redeemed), nil
799796
}
800797
refund, err := dexeth.ParseRefundDataV1(tx.Data())
801798
if err != nil {
802799
return 0, nil
803800
}
804-
return refund.Value, nil
801+
return c.atomize(refund.Value), nil
805802
}
806803

807804
func (c *contractorV1) outgoingValue(tx *types.Transaction) (swapped uint64) {
808805
if inits, err := dexeth.ParseInitiateDataV1(tx.Data()); err == nil {
809806
for _, init := range inits {
810-
swapped += init.Value
807+
swapped += c.atomize(init.Value)
811808
}
812809
}
813810
return
@@ -933,14 +930,6 @@ func (c *tokenContractorV1) tokenAddress() common.Address {
933930
var _ contractor = (*tokenContractorV1)(nil)
934931
var _ tokenContractor = (*tokenContractorV1)(nil)
935932

936-
// readOnlyCallOpts is the CallOpts used for read-only contract method calls.
937-
func readOnlyCallOpts(ctx context.Context) *bind.CallOpts {
938-
return &bind.CallOpts{
939-
Pending: true,
940-
Context: ctx,
941-
}
942-
}
943-
944933
func estimateGas(ctx context.Context, from, to common.Address, abi *abi.ABI, cb bind.ContractBackend, value *big.Int, method string, args ...interface{}) (uint64, error) {
945934
data, err := abi.Pack(method, args...)
946935
if err != nil {

0 commit comments

Comments
 (0)