Skip to content

Commit 9f934c8

Browse files
committed
checkpoint. redid server. tests passing. no v1 tests on server yet.
1 parent cedd287 commit 9f934c8

15 files changed

Lines changed: 867 additions & 555 deletions

File tree

client/asset/eth/contractor.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,6 @@ func (c *tokenContractorV0) tokenAddress() common.Address {
586586
type contractorV1 struct {
587587
contractV1
588588
abi *abi.ABI
589-
net dex.Network
590589
contractAddr common.Address
591590
acctAddr common.Address
592591
cb bind.ContractBackend
@@ -597,19 +596,14 @@ type contractorV1 struct {
597596

598597
var _ contractor = (*contractorV1)(nil)
599598

600-
func newV1Contractor(net dex.Network, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
601-
contractAddr, exists := dexeth.ContractAddresses[1][net]
602-
if !exists || contractAddr == (common.Address{}) {
603-
return nil, fmt.Errorf("no contract address for version 0, net %s", net)
604-
}
599+
func newV1Contractor(contractAddr, acctAddr common.Address, cb bind.ContractBackend) (contractor, error) {
605600
c, err := swapv1.NewETHSwap(contractAddr, cb)
606601
if err != nil {
607602
return nil, err
608603
}
609604
return &contractorV1{
610605
contractV1: c,
611606
abi: dexeth.ABIs[1],
612-
net: net,
613607
contractAddr: contractAddr,
614608
acctAddr: acctAddr,
615609
cb: cb,
@@ -701,7 +695,7 @@ func (c *contractorV1) redeem(txOpts *bind.TransactOpts, redeems []*asset.Redemp
701695

702696
// Not checking version from DecodeLocator because it was already
703697
// audited and incorrect version locator would err below anyway.
704-
_, locator, err := dexeth.DecodeLocator(r.Spends.Contract)
698+
_, locator, err := dexeth.DecodeContractData(r.Spends.Contract)
705699
if err != nil {
706700
return nil, fmt.Errorf("error parsing locator redeem: %w", err)
707701
}
@@ -852,11 +846,17 @@ type tokenContractorV1 struct {
852846
tokenAddr common.Address
853847
}
854848

855-
func newV1TokenContractor(net dex.Network, assetID uint32, acctAddr common.Address, cb bind.ContractBackend) (tokenContractor, error) {
856-
token, tokenAddr, swapContractAddr, err := dexeth.VersionedNetworkToken(assetID, 1, net)
857-
if err != nil {
858-
return nil, err
849+
func newV1TokenContractor(net dex.Network, token *dexeth.Token, acctAddr common.Address, cb bind.ContractBackend) (tokenContractor, error) {
850+
netToken, ok := token.NetTokens[net]
851+
if !ok {
852+
return nil, fmt.Errorf("No %s token for network %s", token.Name, net)
859853
}
854+
swapContract, ok := netToken.SwapContracts[1]
855+
if !ok {
856+
return nil, fmt.Errorf("no version 1 swap contract for %s", token.Name)
857+
}
858+
859+
tokenAddr, swapContractAddr := netToken.Address, swapContract.Address
860860

861861
c, err := erc20v1.NewERC20Swap(swapContractAddr, cb)
862862
if err != nil {
@@ -880,7 +880,6 @@ func newV1TokenContractor(net dex.Network, assetID uint32, acctAddr common.Addre
880880
contractorV1: &contractorV1{
881881
contractV1: c,
882882
abi: dexeth.ABIs[1],
883-
net: net,
884883
contractAddr: swapContractAddr,
885884
acctAddr: acctAddr,
886885
cb: cb,

client/asset/eth/eth.go

Lines changed: 26 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,6 @@ const (
9797
// TODO: Find a way to ask the host about their config set max fee and
9898
// gas values.
9999
maxTxFeeGwei = 1_000_000_000
100-
101-
contractVersionERC20 = ^uint32(0)
102-
contractVersionUnknown = contractVersionERC20 - 1
103100
)
104101

105102
var (
@@ -619,14 +616,7 @@ func privKeyFromSeed(seed []byte) (pk []byte, zero func(), err error) {
619616
// contractVersion converts a server version to a contract version. It applies
620617
// to both tokens and eth right now, but that may not always be the case.
621618
func contractVersion(serverVer uint32) uint32 {
622-
switch serverVer {
623-
case 0:
624-
return 0
625-
case 1:
626-
return 1
627-
default:
628-
return contractVersionUnknown
629-
}
619+
return dexeth.ProtocolVersion(serverVer).ContractVersion()
630620
}
631621

632622
func CreateEVMWallet(chainID int64, createWalletParams *asset.CreateWalletParams, compat *CompatibilityData, skipConnect bool) error {
@@ -1392,9 +1382,10 @@ func (w *baseWallet) MaxFundingFees(_ uint32, _ uint64, _ map[string]string) uin
13921382

13931383
// SingleLotSwapRefundFees returns the fees for a swap transaction for a single lot.
13941384
func (w *assetWallet) SingleLotSwapRefundFees(serverVer uint32, feeSuggestion uint64, _ bool) (swapFees uint64, refundFees uint64, err error) {
1395-
g := w.gases(contractVersion(serverVer))
1385+
contractVer := contractVersion(serverVer)
1386+
g := w.gases(contractVer)
13961387
if g == nil {
1397-
return 0, 0, fmt.Errorf("no gases known for %d version %d", w.assetID, version)
1388+
return 0, 0, fmt.Errorf("no gases known for %d, contract version %d", w.assetID, contractVer)
13981389
}
13991390
return g.Swap * feeSuggestion, g.Refund * feeSuggestion, nil
14001391
}
@@ -2054,7 +2045,7 @@ func (w *ETHWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uint6
20542045
txHash: txHash,
20552046
locator: acToLocator(contractVer, swap, w.addr),
20562047
contractVer: contractVer,
2057-
contractAddr: w.versionedContracts[contractVer][w.net].String(),
2048+
contractAddr: w.versionedContracts[contractVer].String(),
20582049
})
20592050
}
20602051

@@ -2221,7 +2212,7 @@ func (w *assetWallet) Redeem(form *asset.RedeemForm, feeWallet *assetWallet, non
22212212
// from redemption.Spends.Contract. Even for scriptable UTXO assets, the
22222213
// redeem script in this Contract field is redundant with the SecretHash
22232214
// field as ExtractSwapDetails can be applied to extract the hash.
2224-
ver, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
2215+
ver, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
22252216
if err != nil {
22262217
return fail(fmt.Errorf("Redeem: invalid versioned swap contract data: %w", err))
22272218
}
@@ -2376,7 +2367,7 @@ func recoverPubkey(msgHash, sig []byte) ([]byte, error) {
23762367
// tokenBalance checks the token balance of the account handled by the wallet.
23772368
func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
23782369
// We don't care about the version.
2379-
return bal, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
2370+
return bal, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
23802371
bal, err = c.balance(w.ctx)
23812372
return err
23822373
})
@@ -2385,7 +2376,7 @@ func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
23852376
// tokenAllowance checks the amount of tokens that the swap contract is approved
23862377
// to spend on behalf of the account handled by the wallet.
23872378
func (w *assetWallet) tokenAllowance() (allowance *big.Int, err error) {
2388-
return allowance, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
2379+
return allowance, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
23892380
allowance, err = c.allowance(w.ctx)
23902381
return err
23912382
})
@@ -2750,7 +2741,7 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
27502741
return nil, fmt.Errorf("AuditContract: coin id != txHash - coin id: %x, txHash: %s", coinID, tx.Hash())
27512742
}
27522743

2753-
version, locator, err := dexeth.DecodeLocator(contract)
2744+
version, locator, err := dexeth.DecodeContractData(contract)
27542745
if err != nil {
27552746
return nil, fmt.Errorf("AuditContract: failed to decode contract data: %w", err)
27562747
}
@@ -2792,19 +2783,8 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
27922783
if !ok {
27932784
return nil, errors.New("AuditContract: tx does not initiate secret hash")
27942785
}
2795-
// Check vector equivalence. Secret hash equivalence is implied by the
2796-
// vectors presence in the map returned from ParseInitiateData.
2797-
if vec.Value != txVec.Value {
2798-
return nil, errors.New("tx data value doesn't match reported locator data")
2799-
}
2800-
if vec.To != txVec.To {
2801-
return nil, errors.New("tx to address doesn't match reported locator data")
2802-
}
2803-
if vec.From != txVec.From {
2804-
return nil, errors.New("tx from address doesn't match reported locator data")
2805-
}
2806-
if vec.LockTime != txVec.LockTime {
2807-
return nil, errors.New("tx lock time doesn't match reported locator data")
2786+
if !dexeth.CompareVectors(vec, txVec) {
2787+
return nil, fmt.Errorf("tx vector doesn't match expectation. %+v != %+v", txVec, vec)
28082788
}
28092789
val = vec.Value
28102790
participant = vec.To.String()
@@ -2853,7 +2833,7 @@ func (w *assetWallet) LockTimeExpired(ctx context.Context, lockTime time.Time) (
28532833
// ContractLockTimeExpired returns true if the specified contract's locktime has
28542834
// expired, making it possible to issue a Refund.
28552835
func (w *assetWallet) ContractLockTimeExpired(ctx context.Context, contract dex.Bytes) (bool, time.Time, error) {
2856-
contractVer, locator, err := dexeth.DecodeLocator(contract)
2836+
contractVer, locator, err := dexeth.DecodeContractData(contract)
28572837
if err != nil {
28582838
return false, time.Time{}, err
28592839
}
@@ -2941,7 +2921,7 @@ func (w *assetWallet) FindRedemption(ctx context.Context, _, contract dex.Bytes)
29412921
// contract, so we are basically doing the next best thing here.
29422922
const coinIDTmpl = coinIDTakerFoundMakerRedemption + "%s"
29432923

2944-
contractVer, locator, err := dexeth.DecodeLocator(contract)
2924+
contractVer, locator, err := dexeth.DecodeContractData(contract)
29452925
if err != nil {
29462926
return nil, nil, err
29472927
}
@@ -3020,7 +3000,7 @@ func (w *assetWallet) findSecret(locator []byte, contractVer uint32) ([]byte, st
30203000
// Refund refunds a contract. This can only be used after the time lock has
30213001
// expired.
30223002
func (w *assetWallet) Refund(_, contract dex.Bytes, feeRate uint64) (dex.Bytes, error) {
3023-
contractVer, locator, err := dexeth.DecodeLocator(contract)
3003+
contractVer, locator, err := dexeth.DecodeContractData(contract)
30243004
if err != nil {
30253005
return nil, fmt.Errorf("Refund: failed to decode contract: %w", err)
30263006
}
@@ -3118,7 +3098,7 @@ func (w *ETHWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
31183098
// EstimateRegistrationTxFee returns an estimate for the tx fee needed to
31193099
// pay the registration fee using the provided feeRate.
31203100
func (w *TokenWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
3121-
g := w.gases(contractVersionERC20)
3101+
g := w.gases(dexeth.ContractVersionERC20)
31223102
if g == nil {
31233103
w.log.Errorf("no gas table")
31243104
return math.MaxUint64
@@ -3193,7 +3173,7 @@ func (w *TokenWallet) canSend(value uint64, verifyBalance, isPreEstimate bool) (
31933173
return 0, nil, fmt.Errorf("error getting max fee rate: %w", err)
31943174
}
31953175

3196-
g := w.gases(contractVersionERC20)
3176+
g := w.gases(dexeth.ContractVersionERC20)
31973177
if g == nil {
31983178
return 0, nil, fmt.Errorf("gas table not found")
31993179
}
@@ -3285,7 +3265,7 @@ func (w *ETHWallet) RestorationInfo(seed []byte) ([]*asset.WalletRestoration, er
32853265
// SwapConfirmations gets the number of confirmations and the spend status
32863266
// for the specified swap.
32873267
func (w *assetWallet) SwapConfirmations(ctx context.Context, coinID dex.Bytes, contract dex.Bytes, _ time.Time) (confs uint32, spent bool, err error) {
3288-
contractVer, secretHash, err := dexeth.DecodeLocator(contract)
3268+
contractVer, secretHash, err := dexeth.DecodeContractData(contract)
32893269
if err != nil {
32903270
return 0, false, err
32913271
}
@@ -3464,7 +3444,7 @@ func (eth *assetWallet) DynamicRedemptionFeesPaid(ctx context.Context, coinID, c
34643444
// secret hashes.
34653445
func (eth *baseWallet) swapOrRedemptionFeesPaid(ctx context.Context, coinID, contractData dex.Bytes,
34663446
isInit bool) (fee uint64, secretHashes [][]byte, err error) {
3467-
contractVer, locator, err := dexeth.DecodeLocator(contractData)
3447+
contractVer, locator, err := dexeth.DecodeContractData(contractData)
34683448
if err != nil {
34693449
return 0, nil, err
34703450
}
@@ -4068,7 +4048,7 @@ func (w *assetWallet) checkUnconfirmedRedemption(locator []byte, contractVer uin
40684048
// entire redemption batch, a new transaction containing only the swap we are
40694049
// searching for will be created.
40704050
func (w *assetWallet) confirmRedemptionWithoutMonitoredTx(txHash common.Hash, redemption *asset.Redemption, feeWallet *assetWallet) (*asset.ConfirmRedemptionStatus, error) {
4071-
contractVer, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
4051+
contractVer, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
40724052
if err != nil {
40734053
return nil, fmt.Errorf("failed to decode contract data: %w", err)
40744054
}
@@ -4161,7 +4141,7 @@ func (w *assetWallet) confirmRedemption(coinID dex.Bytes, redemption *asset.Rede
41614141
txHash = monitoredTxHash
41624142
}
41634143

4164-
contractVer, locator, err := dexeth.DecodeLocator(redemption.Spends.Contract)
4144+
contractVer, locator, err := dexeth.DecodeContractData(redemption.Spends.Contract)
41654145
if err != nil {
41664146
return nil, fmt.Errorf("failed to decode contract data: %w", err)
41674147
}
@@ -4465,15 +4445,15 @@ func (w *ETHWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *big.
44654445
func (w *TokenWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *big.Int) (tx *types.Transaction, err error) {
44664446
w.baseWallet.nonceSendMtx.Lock()
44674447
defer w.baseWallet.nonceSendMtx.Unlock()
4468-
g := w.gases(contractVersionERC20)
4448+
g := w.gases(dexeth.ContractVersionERC20)
44694449
if g == nil {
44704450
return nil, fmt.Errorf("no gas table")
44714451
}
44724452
txOpts, err := w.node.txOpts(w.ctx, 0, g.Transfer, nil, nil)
44734453
if err != nil {
44744454
return nil, err
44754455
}
4476-
return tx, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4456+
return tx, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
44774457
tx, err = c.transfer(txOpts, addr, w.evmify(amt))
44784458
if err != nil {
44794459
c.voidUnusedNonce()
@@ -4596,7 +4576,7 @@ func (w *assetWallet) loadContractors() error {
45964576

45974577
// withContractor runs the provided function with the versioned contractor.
45984578
func (w *assetWallet) withContractor(contractVer uint32, f func(contractor) error) error {
4599-
if contractVer == contractVersionERC20 {
4579+
if contractVer == dexeth.ContractVersionERC20 {
46004580
// For ERC02 methods, use the most recent contractor version.
46014581
var bestVer uint32
46024582
var bestContractor contractor
@@ -4629,16 +4609,17 @@ func (w *assetWallet) withTokenContractor(assetID, ver uint32, f func(tokenContr
46294609
// estimateApproveGas estimates the gas required for a transaction approving a
46304610
// spender for an ERC20 contract.
46314611
func (w *assetWallet) estimateApproveGas(newGas *big.Int) (gas uint64, err error) {
4632-
return gas, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4612+
return gas, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
46334613
gas, err = c.estimateApproveGas(w.ctx, newGas)
46344614
return err
46354615
})
46364616
}
46374617

46384618
// estimateTransferGas estimates the gas needed for a token transfer call to an
46394619
// ERC20 contract.
4620+
// TODO: Delete this and contractor methods. Unused.
46404621
func (w *assetWallet) estimateTransferGas(val uint64) (gas uint64, err error) {
4641-
return gas, w.withTokenContractor(w.assetID, contractVersionERC20, func(c tokenContractor) error {
4622+
return gas, w.withTokenContractor(w.assetID, dexeth.ContractVersionERC20, func(c tokenContractor) error {
46424623
gas, err = c.estimateTransferGas(w.ctx, w.evmify(val))
46434624
return err
46444625
})

0 commit comments

Comments
 (0)