@@ -96,9 +96,6 @@ const (
9696 // TODO: Find a way to ask the host about their config set max fee and
9797 // gas values.
9898 maxTxFeeGwei = 1_000_000_000
99-
100- contractVersionERC20 = ^ uint32 (0 )
101- contractVersionUnknown = contractVersionERC20 - 1
10299)
103100
104101var (
@@ -549,14 +546,7 @@ func privKeyFromSeed(seed []byte) (pk []byte, zero func(), err error) {
549546// contractVersion converts a server version to a contract version. It applies
550547// to both tokens and eth right now, but that may not always be the case.
551548func contractVersion (serverVer uint32 ) uint32 {
552- switch serverVer {
553- case 0 :
554- return 0
555- case 1 :
556- return 1
557- default :
558- return contractVersionUnknown
559- }
549+ return dexeth .ProtocolVersion (serverVer ).ContractVersion ()
560550}
561551
562552func CreateEVMWallet (chainID int64 , createWalletParams * asset.CreateWalletParams , compat * CompatibilityData , skipConnect bool ) error {
@@ -1326,9 +1316,10 @@ func (w *baseWallet) MaxFundingFees(_ uint32, _ uint64, _ map[string]string) uin
13261316
13271317// SingleLotSwapRefundFees returns the fees for a swap transaction for a single lot.
13281318func (w * assetWallet ) SingleLotSwapRefundFees (serverVer uint32 , feeSuggestion uint64 , _ bool ) (swapFees uint64 , refundFees uint64 , err error ) {
1329- g := w .gases (contractVersion (serverVer ))
1319+ contractVer := contractVersion (serverVer )
1320+ g := w .gases (contractVer )
13301321 if g == nil {
1331- return 0 , 0 , fmt .Errorf ("no gases known for %d version %d" , w .assetID , serverVer )
1322+ return 0 , 0 , fmt .Errorf ("no gases known for %d, contract version %d" , w .assetID , contractVer )
13321323 }
13331324 return g .Swap * feeSuggestion , g .Refund * feeSuggestion , nil
13341325}
@@ -2155,7 +2146,7 @@ func (w *assetWallet) Redeem(form *asset.RedeemForm, feeWallet *assetWallet, non
21552146 // from redemption.Spends.Contract. Even for scriptable UTXO assets, the
21562147 // redeem script in this Contract field is redundant with the SecretHash
21572148 // field as ExtractSwapDetails can be applied to extract the hash.
2158- ver , locator , err := dexeth .DecodeLocator (redemption .Spends .Contract )
2149+ ver , locator , err := dexeth .DecodeContractData (redemption .Spends .Contract )
21592150 if err != nil {
21602151 return fail (fmt .Errorf ("Redeem: invalid versioned swap contract data: %w" , err ))
21612152 }
@@ -2310,7 +2301,7 @@ func recoverPubkey(msgHash, sig []byte) ([]byte, error) {
23102301// tokenBalance checks the token balance of the account handled by the wallet.
23112302func (w * assetWallet ) tokenBalance () (bal * big.Int , err error ) {
23122303 // We don't care about the version.
2313- return bal , w .withTokenContractor (w .assetID , contractVersionERC20 , func (c tokenContractor ) error {
2304+ return bal , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
23142305 bal , err = c .balance (w .ctx )
23152306 return err
23162307 })
@@ -2319,7 +2310,7 @@ func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
23192310// tokenAllowance checks the amount of tokens that the swap contract is approved
23202311// to spend on behalf of the account handled by the wallet.
23212312func (w * assetWallet ) tokenAllowance () (allowance * big.Int , err error ) {
2322- return allowance , w .withTokenContractor (w .assetID , contractVersionERC20 , func (c tokenContractor ) error {
2313+ return allowance , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
23232314 allowance , err = c .allowance (w .ctx )
23242315 return err
23252316 })
@@ -2685,7 +2676,7 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
26852676 return nil , fmt .Errorf ("AuditContract: coin id != txHash - coin id: %x, txHash: %s" , coinID , tx .Hash ())
26862677 }
26872678
2688- version , locator , err := dexeth .DecodeLocator (contract )
2679+ version , locator , err := dexeth .DecodeContractData (contract )
26892680 if err != nil {
26902681 return nil , fmt .Errorf ("AuditContract: failed to decode contract data: %w" , err )
26912682 }
@@ -2727,19 +2718,8 @@ func (w *assetWallet) AuditContract(coinID, contract, serializedTx dex.Bytes, re
27272718 if ! ok {
27282719 return nil , errors .New ("AuditContract: tx does not initiate secret hash" )
27292720 }
2730- // Check vector equivalence. Secret hash equivalence is implied by the
2731- // vectors presence in the map returned from ParseInitiateData.
2732- if vec .Value != txVec .Value {
2733- return nil , errors .New ("tx data value doesn't match reported locator data" )
2734- }
2735- if vec .To != txVec .To {
2736- return nil , errors .New ("tx to address doesn't match reported locator data" )
2737- }
2738- if vec .From != txVec .From {
2739- return nil , errors .New ("tx from address doesn't match reported locator data" )
2740- }
2741- if vec .LockTime != txVec .LockTime {
2742- return nil , errors .New ("tx lock time doesn't match reported locator data" )
2721+ if ! dexeth .CompareVectors (vec , txVec ) {
2722+ return nil , fmt .Errorf ("tx vector doesn't match expectation. %+v != %+v" , txVec , vec )
27432723 }
27442724 val = vec .Value
27452725 participant = vec .To .String ()
@@ -2788,7 +2768,7 @@ func (w *assetWallet) LockTimeExpired(ctx context.Context, lockTime time.Time) (
27882768// ContractLockTimeExpired returns true if the specified contract's locktime has
27892769// expired, making it possible to issue a Refund.
27902770func (w * assetWallet ) ContractLockTimeExpired (ctx context.Context , contract dex.Bytes ) (bool , time.Time , error ) {
2791- contractVer , locator , err := dexeth .DecodeLocator (contract )
2771+ contractVer , locator , err := dexeth .DecodeContractData (contract )
27922772 if err != nil {
27932773 return false , time.Time {}, err
27942774 }
@@ -2855,7 +2835,7 @@ func (w *assetWallet) FindRedemption(ctx context.Context, _, contract dex.Bytes)
28552835 // contract, so we are basically doing the next best thing here.
28562836 const coinIDTmpl = coinIDTakerFoundMakerRedemption + "%s"
28572837
2858- contractVer , locator , err := dexeth .DecodeLocator (contract )
2838+ contractVer , locator , err := dexeth .DecodeContractData (contract )
28592839 if err != nil {
28602840 return nil , nil , err
28612841 }
@@ -2934,7 +2914,7 @@ func (w *assetWallet) findSecret(locator []byte, contractVer uint32) ([]byte, st
29342914// Refund refunds a contract. This can only be used after the time lock has
29352915// expired.
29362916func (w * assetWallet ) Refund (_ , contract dex.Bytes , feeRate uint64 ) (dex.Bytes , error ) {
2937- contractVer , locator , err := dexeth .DecodeLocator (contract )
2917+ contractVer , locator , err := dexeth .DecodeContractData (contract )
29382918 if err != nil {
29392919 return nil , fmt .Errorf ("Refund: failed to decode contract: %w" , err )
29402920 }
@@ -3032,7 +3012,7 @@ func (w *ETHWallet) EstimateRegistrationTxFee(feeRate uint64) uint64 {
30323012// EstimateRegistrationTxFee returns an estimate for the tx fee needed to
30333013// pay the registration fee using the provided feeRate.
30343014func (w * TokenWallet ) EstimateRegistrationTxFee (feeRate uint64 ) uint64 {
3035- g := w .gases (contractVersionERC20 )
3015+ g := w .gases (dexeth . ContractVersionERC20 )
30363016 if g == nil {
30373017 w .log .Errorf ("no gas table" )
30383018 return math .MaxUint64
@@ -3107,7 +3087,7 @@ func (w *TokenWallet) canSend(value uint64, verifyBalance, isPreEstimate bool) (
31073087 return 0 , nil , fmt .Errorf ("error getting max fee rate: %w" , err )
31083088 }
31093089
3110- g := w .gases (contractVersionERC20 )
3090+ g := w .gases (dexeth . ContractVersionERC20 )
31113091 if g == nil {
31123092 return 0 , nil , fmt .Errorf ("gas table not found" )
31133093 }
@@ -3199,7 +3179,7 @@ func (w *ETHWallet) RestorationInfo(seed []byte) ([]*asset.WalletRestoration, er
31993179// SwapConfirmations gets the number of confirmations and the spend status
32003180// for the specified swap.
32013181func (w * assetWallet ) SwapConfirmations (ctx context.Context , coinID dex.Bytes , contract dex.Bytes , _ time.Time ) (confs uint32 , spent bool , err error ) {
3202- contractVer , secretHash , err := dexeth .DecodeLocator (contract )
3182+ contractVer , secretHash , err := dexeth .DecodeContractData (contract )
32033183 if err != nil {
32043184 return 0 , false , err
32053185 }
@@ -3384,7 +3364,7 @@ func (eth *assetWallet) DynamicRedemptionFeesPaid(ctx context.Context, coinID, c
33843364// secret hashes.
33853365func (eth * baseWallet ) swapOrRedemptionFeesPaid (ctx context.Context , coinID , contractData dex.Bytes ,
33863366 isInit bool ) (fee uint64 , secretHashes [][]byte , err error ) {
3387- contractVer , locator , err := dexeth .DecodeLocator (contractData )
3367+ contractVer , locator , err := dexeth .DecodeContractData (contractData )
33883368 if err != nil {
33893369 return 0 , nil , err
33903370 }
@@ -4001,7 +3981,7 @@ func (w *assetWallet) checkUnconfirmedRedemption(locator []byte, contractVer uin
40013981// entire redemption batch, a new transaction containing only the swap we are
40023982// searching for will be created.
40033983func (w * assetWallet ) confirmRedemptionWithoutMonitoredTx (txHash common.Hash , redemption * asset.Redemption , feeWallet * assetWallet ) (* asset.ConfirmRedemptionStatus , error ) {
4004- contractVer , locator , err := dexeth .DecodeLocator (redemption .Spends .Contract )
3984+ contractVer , locator , err := dexeth .DecodeContractData (redemption .Spends .Contract )
40053985 if err != nil {
40063986 return nil , fmt .Errorf ("failed to decode contract data: %w" , err )
40073987 }
@@ -4094,7 +4074,7 @@ func (w *assetWallet) confirmRedemption(coinID dex.Bytes, redemption *asset.Rede
40944074 txHash = monitoredTxHash
40954075 }
40964076
4097- contractVer , locator , err := dexeth .DecodeLocator (redemption .Spends .Contract )
4077+ contractVer , locator , err := dexeth .DecodeContractData (redemption .Spends .Contract )
40984078 if err != nil {
40994079 return nil , fmt .Errorf ("failed to decode contract data: %w" , err )
41004080 }
@@ -4495,7 +4475,7 @@ func (w *ETHWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *big.
44954475func (w * TokenWallet ) sendToAddr (addr common.Address , amt uint64 , maxFeeRate * big.Int ) (tx * types.Transaction , err error ) {
44964476 w .baseWallet .nonceSendMtx .Lock ()
44974477 defer w .baseWallet .nonceSendMtx .Unlock ()
4498- g := w .gases (contractVersionERC20 )
4478+ g := w .gases (dexeth . ContractVersionERC20 )
44994479 if g == nil {
45004480 return nil , fmt .Errorf ("no gas table" )
45014481 }
@@ -4504,7 +4484,7 @@ func (w *TokenWallet) sendToAddr(addr common.Address, amt uint64, maxFeeRate *bi
45044484 if err != nil {
45054485 return nil , err
45064486 }
4507- return tx , w .withTokenContractor (w .assetID , contractVersionERC20 , func (c tokenContractor ) error {
4487+ return tx , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
45084488 tx , err = c .transfer (txOpts , addr , w .evmify (amt ))
45094489 if err != nil {
45104490 c .voidUnusedNonce ()
@@ -4627,7 +4607,7 @@ func (w *assetWallet) loadContractors() error {
46274607
46284608// withContractor runs the provided function with the versioned contractor.
46294609func (w * assetWallet ) withContractor (contractVer uint32 , f func (contractor ) error ) error {
4630- if contractVer == contractVersionERC20 {
4610+ if contractVer == dexeth . ContractVersionERC20 {
46314611 // For ERC02 methods, use the most recent contractor version.
46324612 var bestVer uint32
46334613 var bestContractor contractor
@@ -4660,16 +4640,17 @@ func (w *assetWallet) withTokenContractor(assetID, ver uint32, f func(tokenContr
46604640// estimateApproveGas estimates the gas required for a transaction approving a
46614641// spender for an ERC20 contract.
46624642func (w * assetWallet ) estimateApproveGas (newGas * big.Int ) (gas uint64 , err error ) {
4663- return gas , w .withTokenContractor (w .assetID , contractVersionERC20 , func (c tokenContractor ) error {
4643+ return gas , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
46644644 gas , err = c .estimateApproveGas (w .ctx , newGas )
46654645 return err
46664646 })
46674647}
46684648
46694649// estimateTransferGas estimates the gas needed for a token transfer call to an
46704650// ERC20 contract.
4651+ // TODO: Delete this and contractor methods. Unused.
46714652func (w * assetWallet ) estimateTransferGas (val uint64 ) (gas uint64 , err error ) {
4672- return gas , w .withTokenContractor (w .assetID , contractVersionERC20 , func (c tokenContractor ) error {
4653+ return gas , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
46734654 gas , err = c .estimateTransferGas (w .ctx , w .evmify (val ))
46744655 return err
46754656 })
0 commit comments