Skip to content

Commit

Permalink
client/asset/eth: token prefactor
Browse files Browse the repository at this point in the history
Preparing for tokens.

Moves contractor from nodeClient to ExchangeWallet. The changes the
ethFetcher API quite a bit, but is overall a simplification. To accomplish
this, I moved the nonceSendMtx to from nodeClient to ExchangeWallet too.
Nonce generation and signing is moved into nodeClient.txOpts to dedupe.

Use block headers instead of full blocks for tip monitoring.

Simplify fundingCoin and eliminate fundingCoinID.

Simplify tests using more consts and simpler structs.
  • Loading branch information
buck54321 committed Apr 8, 2022
1 parent 95c9a38 commit 11fe415
Show file tree
Hide file tree
Showing 8 changed files with 1,204 additions and 1,383 deletions.
21 changes: 10 additions & 11 deletions client/asset/eth/contractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
)

// contractor is a translation layer between the abigen bindings and the DEX app.
Expand All @@ -38,14 +37,14 @@ type contractor interface {
estimateRefundGas(ctx context.Context, secretHash [32]byte) (uint64, error)
isRedeemable(secretHash, secret [32]byte) (bool, error)
// incomingValue checks if the transaction redeems or refunds to the
// contract and sums the incoming value. It is not an error if the
// contract and returns the incoming value. It is not an error if the
// transaction does not pay to the contract, and the value returned in that
// case will always be zero.
incomingValue(context.Context, *types.Transaction) (uint64, error)
isRefundable(secretHash [32]byte) (bool, error)
}

type contractorConstructor func(net dex.Network, addr common.Address, ec *ethclient.Client) (contractor, error)
type contractorConstructor func(net dex.Network, addr common.Address, cb bind.ContractBackend) (contractor, error)

type contractV0 interface {
Initiate(opts *bind.TransactOpts, initiations []swapv0.ETHSwapInitiation) (*types.Transaction, error)
Expand All @@ -61,24 +60,24 @@ type contractV0 interface {
type contractorV0 struct {
contractV0 // *swapv0.ETHSwap
abi *abi.ABI
ec *ethclient.Client
cb bind.ContractBackend
contractAddr common.Address
acctAddr common.Address
}

func newV0contractor(net dex.Network, acctAddr common.Address, ec *ethclient.Client) (contractor, error) {
func newV0Contractor(net dex.Network, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
contractAddr, exists := dexeth.ContractAddresses[0][net]
if !exists || contractAddr == (common.Address{}) {
return nil, fmt.Errorf("no contract address for version 0, net %s", net)
}
c, err := swapv0.NewETHSwap(contractAddr, ec)
c, err := swapv0.NewETHSwap(contractAddr, cb)
if err != nil {
return nil, err
}
return &contractorV0{
contractV0: c,
abi: dexeth.ABIs[0],
ec: ec,
cb: cb,
contractAddr: contractAddr,
acctAddr: acctAddr,
}, nil
Expand Down Expand Up @@ -189,7 +188,7 @@ func (c *contractorV0) estimateRedeemGas(ctx context.Context, secrets [][32]byte
return 0, err
}

return c.ec.EstimateGas(ctx, ethereum.CallMsg{
return c.cb.EstimateGas(ctx, ethereum.CallMsg{
From: c.acctAddr,
To: &c.contractAddr,
Data: data,
Expand All @@ -202,7 +201,7 @@ func (c *contractorV0) estimateRefundGas(ctx context.Context, secretHash [32]byt
return 0, fmt.Errorf("unexpected error packing abi: %v", err)
}

return c.ec.EstimateGas(ctx, ethereum.CallMsg{
return c.cb.EstimateGas(ctx, ethereum.CallMsg{
From: c.acctAddr,
To: &c.contractAddr,
Data: data,
Expand All @@ -226,7 +225,7 @@ func (c *contractorV0) estimateInitGas(ctx context.Context, n int) (uint64, erro
return 0, nil
}

return c.ec.EstimateGas(ctx, ethereum.CallMsg{
return c.cb.EstimateGas(ctx, ethereum.CallMsg{
From: c.acctAddr,
To: &c.contractAddr,
Value: big.NewInt(int64(n)),
Expand Down Expand Up @@ -262,5 +261,5 @@ func (c *contractorV0) incomingValue(ctx context.Context, tx *types.Transaction)
}

var contractorConstructors = map[uint32]contractorConstructor{
0: newV0contractor,
0: newV0Contractor,
}

0 comments on commit 11fe415

Please sign in to comment.