Skip to content

Commit 89d26a1

Browse files
committed
eth v1 contract
Implements the version 1 contracts for ethereum and tokens. Based on feedback in decred#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 dd661f0 commit 89d26a1

29 files changed

Lines changed: 1555 additions & 1009 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
@@ -221,11 +221,11 @@ func (c *contractorV0) vector(ctx context.Context, locator []byte) (*dexeth.Swap
221221
return nil, err
222222
}
223223
vector := &dexeth.SwapVector{
224-
From: swap.Participant,
225-
To: swap.Initiator,
226-
Value: dexeth.WeiToGwei(swap.Value),
224+
From: swap.Initiator,
225+
To: swap.Participant,
226+
Value: swap.Value,
227227
SecretHash: secretHash,
228-
LockTime: uint64(swap.LockTime.UnixMilli()),
228+
LockTime: uint64(swap.LockTime.Unix()),
229229
}
230230
return vector, nil
231231
}
@@ -243,11 +243,11 @@ func (c *contractorV0) statusAndVector(ctx context.Context, locator []byte) (*de
243243
return nil, nil, err
244244
}
245245
vector := &dexeth.SwapVector{
246-
From: swap.Participant,
247-
To: swap.Initiator,
248-
Value: dexeth.WeiToGwei(swap.Value),
246+
From: swap.Initiator,
247+
To: swap.Participant,
248+
Value: swap.Value,
249249
SecretHash: secretHash,
250-
LockTime: uint64(swap.LockTime.UnixMilli()),
250+
LockTime: uint64(swap.LockTime.Unix()),
251251
}
252252
status := &dexeth.SwapStatus{
253253
Step: swap.State,
@@ -443,7 +443,6 @@ func (c *erc20Contractor) balance(ctx context.Context) (*big.Int, error) {
443443
// allowance exposes the read-only allowance method of the erc20 token contract.
444444
func (c *erc20Contractor) allowance(ctx context.Context) (*big.Int, error) {
445445
callOpts := &bind.CallOpts{
446-
Pending: true,
447446
From: c.acct,
448447
Context: ctx,
449448
}
@@ -598,8 +597,7 @@ func (c *tokenContractorV0) tokenAddress() common.Address {
598597

599598
type contractorV1 struct {
600599
contractV1
601-
abi *abi.ABI
602-
// net dex.Network
600+
abi *abi.ABI
603601
contractAddr common.Address
604602
acctAddr common.Address
605603
cb bind.ContractBackend
@@ -611,19 +609,18 @@ type contractorV1 struct {
611609
var _ contractor = (*contractorV1)(nil)
612610

613611
func newV1Contractor(net dex.Network, contractAddr, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
614-
615612
c, err := swapv1.NewETHSwap(contractAddr, cb)
616613
if err != nil {
617614
return nil, err
618615
}
619616
return &contractorV1{
620-
contractV1: c,
621-
abi: dexeth.ABIs[1],
622-
// net: net,
617+
contractV1: c,
618+
abi: dexeth.ABIs[1],
623619
contractAddr: contractAddr,
624620
acctAddr: acctAddr,
625621
cb: cb,
626622
atomize: dexeth.WeiToGwei,
623+
evmify: dexeth.GweiToWei,
627624
}, nil
628625
}
629626

@@ -679,7 +676,7 @@ func (c *contractorV1) initiate(txOpts *bind.TransactOpts, contracts []*asset.Co
679676
v := &dexeth.SwapVector{
680677
From: c.acctAddr,
681678
To: common.HexToAddress(ac.Address),
682-
Value: ac.Value,
679+
Value: c.evmify(ac.Value),
683680
LockTime: ac.LockTime,
684681
}
685682
copy(v.SecretHash[:], ac.SecretHash)
@@ -705,7 +702,7 @@ func (c *contractorV1) redeem(txOpts *bind.TransactOpts, redeems []*asset.Redemp
705702

706703
// Not checking version from DecodeLocator because it was already
707704
// audited and incorrect version locator would err below anyway.
708-
_, locator, err := dexeth.DecodeLocator(r.Spends.Contract)
705+
_, locator, err := dexeth.DecodeContractData(r.Spends.Contract)
709706
if err != nil {
710707
return nil, fmt.Errorf("error parsing locator redeem: %w", err)
711708
}
@@ -739,7 +736,7 @@ func (c *contractorV1) estimateInitGas(ctx context.Context, n int) (uint64, erro
739736
SecretHash: secretHash,
740737
Initiator: c.acctAddr,
741738
Participant: common.BytesToAddress(encode.RandomBytes(20)),
742-
Value: 1,
739+
Value: big.NewInt(dexeth.GweiFactor),
743740
})
744741
}
745742

@@ -814,23 +811,23 @@ func (c *contractorV1) isRefundable(locator []byte) (bool, error) {
814811

815812
func (c *contractorV1) incomingValue(ctx context.Context, tx *types.Transaction) (uint64, error) {
816813
if redeems, err := dexeth.ParseRedeemDataV1(tx.Data()); err == nil {
817-
var redeemed uint64
814+
var redeemed *big.Int
818815
for _, r := range redeems {
819-
redeemed += r.Contract.Value
816+
redeemed.Add(redeemed, r.Contract.Value)
820817
}
821-
return redeemed, nil
818+
return c.atomize(redeemed), nil
822819
}
823820
refund, err := dexeth.ParseRefundDataV1(tx.Data())
824821
if err != nil {
825822
return 0, nil
826823
}
827-
return refund.Value, nil
824+
return c.atomize(refund.Value), nil
828825
}
829826

830827
func (c *contractorV1) outgoingValue(tx *types.Transaction) (swapped uint64) {
831828
if inits, err := dexeth.ParseInitiateDataV1(tx.Data()); err == nil {
832829
for _, init := range inits {
833-
swapped += init.Value
830+
swapped += c.atomize(init.Value)
834831
}
835832
}
836833
return
@@ -956,14 +953,6 @@ func (c *tokenContractorV1) tokenAddress() common.Address {
956953
var _ contractor = (*tokenContractorV1)(nil)
957954
var _ tokenContractor = (*tokenContractorV1)(nil)
958955

959-
// readOnlyCallOpts is the CallOpts used for read-only contract method calls.
960-
func readOnlyCallOpts(ctx context.Context) *bind.CallOpts {
961-
return &bind.CallOpts{
962-
Pending: true,
963-
Context: ctx,
964-
}
965-
}
966-
967956
func estimateGas(ctx context.Context, from, to common.Address, abi *abi.ABI, cb bind.ContractBackend, value *big.Int, method string, args ...interface{}) (uint64, error) {
968957
data, err := abi.Pack(method, args...)
969958
if err != nil {

0 commit comments

Comments
 (0)