Skip to content

Commit

Permalink
eth v1 contract
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
buck54321 committed Jun 3, 2024
1 parent fd8198e commit eb16ea9
Show file tree
Hide file tree
Showing 29 changed files with 1,548 additions and 1,002 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,4 @@ server/cmd/validatemarkets
client/cmd/translationsreport/translationsreport
client/cmd/translationsreport/worksheets
server/cmd/dexadm/dexadm
server/cmd/dcrdex/evm-protocol-overrides.json
4 changes: 2 additions & 2 deletions client/asset/eth/cmd/getgas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ func mainErr() error {
flag.BoolVar(&useMainnet, "mainnet", false, "use mainnet")
flag.BoolVar(&useTestnet, "testnet", false, "use testnet")
flag.BoolVar(&useSimnet, "simnet", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "use simnet")
flag.BoolVar(&debug, "debug", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "use trace logging")
flag.BoolVar(&debug, "debug", false, "use debug logging")
flag.IntVar(&maxSwaps, "n", 5, "max number of swaps per transaction. minimum is 2. test will run from 2 swap up to n swaps.")
flag.StringVar(&chain, "chain", "eth", "symbol of the base chain")
flag.StringVar(&token, "token", "", "symbol of the token. if token is not specified, will check gas for base chain")
Expand Down
51 changes: 20 additions & 31 deletions client/asset/eth/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,11 @@ func (c *contractorV0) vector(ctx context.Context, locator []byte) (*dexeth.Swap
return nil, err
}
vector := &dexeth.SwapVector{
From: swap.Participant,
To: swap.Initiator,
Value: dexeth.WeiToGwei(swap.Value),
From: swap.Initiator,
To: swap.Participant,
Value: swap.Value,
SecretHash: secretHash,
LockTime: uint64(swap.LockTime.UnixMilli()),
LockTime: uint64(swap.LockTime.Unix()),
}
return vector, nil
}
Expand All @@ -242,11 +242,11 @@ func (c *contractorV0) statusAndVector(ctx context.Context, locator []byte) (*de
return nil, nil, err
}
vector := &dexeth.SwapVector{
From: swap.Participant,
To: swap.Initiator,
Value: dexeth.WeiToGwei(swap.Value),
From: swap.Initiator,
To: swap.Participant,
Value: swap.Value,
SecretHash: secretHash,
LockTime: uint64(swap.LockTime.UnixMilli()),
LockTime: uint64(swap.LockTime.Unix()),
}
status := &dexeth.SwapStatus{
Step: swap.State,
Expand Down Expand Up @@ -442,7 +442,6 @@ func (c *erc20Contractor) balance(ctx context.Context) (*big.Int, error) {
// allowance exposes the read-only allowance method of the erc20 token contract.
func (c *erc20Contractor) allowance(ctx context.Context) (*big.Int, error) {
callOpts := &bind.CallOpts{
Pending: true,
From: c.acct,
Context: ctx,
}
Expand Down Expand Up @@ -575,8 +574,7 @@ func (c *tokenContractorV0) tokenAddress() common.Address {

type contractorV1 struct {
contractV1
abi *abi.ABI
// net dex.Network
abi *abi.ABI
contractAddr common.Address
acctAddr common.Address
cb bind.ContractBackend
Expand All @@ -588,19 +586,18 @@ type contractorV1 struct {
var _ contractor = (*contractorV1)(nil)

func newV1Contractor(net dex.Network, contractAddr, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {

c, err := swapv1.NewETHSwap(contractAddr, cb)
if err != nil {
return nil, err
}
return &contractorV1{
contractV1: c,
abi: dexeth.ABIs[1],
// net: net,
contractV1: c,
abi: dexeth.ABIs[1],
contractAddr: contractAddr,
acctAddr: acctAddr,
cb: cb,
atomize: dexeth.WeiToGwei,
evmify: dexeth.GweiToWei,
}, nil
}

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

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

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

func (c *contractorV1) incomingValue(ctx context.Context, tx *types.Transaction) (uint64, error) {
if redeems, err := dexeth.ParseRedeemDataV1(tx.Data()); err == nil {
var redeemed uint64
var redeemed *big.Int
for _, r := range redeems {
redeemed += r.Contract.Value
redeemed.Add(redeemed, r.Contract.Value)
}
return redeemed, nil
return c.atomize(redeemed), nil
}
refund, err := dexeth.ParseRefundDataV1(tx.Data())
if err != nil {
return 0, nil
}
return refund.Value, nil
return c.atomize(refund.Value), nil
}

func (c *contractorV1) outgoingValue(tx *types.Transaction) (swapped uint64) {
if inits, err := dexeth.ParseInitiateDataV1(tx.Data()); err == nil {
for _, init := range inits {
swapped += init.Value
swapped += c.atomize(init.Value)
}
}
return
Expand Down Expand Up @@ -933,14 +930,6 @@ func (c *tokenContractorV1) tokenAddress() common.Address {
var _ contractor = (*tokenContractorV1)(nil)
var _ tokenContractor = (*tokenContractorV1)(nil)

// readOnlyCallOpts is the CallOpts used for read-only contract method calls.
func readOnlyCallOpts(ctx context.Context) *bind.CallOpts {
return &bind.CallOpts{
Pending: true,
Context: ctx,
}
}

func estimateGas(ctx context.Context, from, to common.Address, abi *abi.ABI, cb bind.ContractBackend, value *big.Int, method string, args ...interface{}) (uint64, error) {
data, err := abi.Pack(method, args...)
if err != nil {
Expand Down
Loading

0 comments on commit eb16ea9

Please sign in to comment.