@@ -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
105102var (
@@ -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.
621618func 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
632622func 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.
13941384func (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.
23772368func (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.
23872378func (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.
28552835func (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.
30223002func (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.
31203100func (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.
32873267func (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.
34653445func (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.
40704050func (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.
44654445func (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.
45984578func (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.
46314611func (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.
46404621func (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