@@ -50,14 +50,19 @@ func registerToken(tokenID uint32, desc string) {
5050 panic ("token " + strconv .Itoa (int (tokenID )) + " not known" )
5151 }
5252 netAddrs := make (map [dex.Network ]string )
53+ netAssetVersions := make (map [dex.Network ][]uint32 , 3 )
5354 for net , netToken := range token .NetTokens {
5455 netAddrs [net ] = netToken .Address .String ()
56+ netAssetVersions [net ] = make ([]uint32 , 0 , 1 )
57+ for ver := range netToken .SwapContracts {
58+ netAssetVersions [net ] = append (netAssetVersions [net ], ver )
59+ }
5560 }
5661 asset .RegisterToken (tokenID , token .Token , & asset.WalletDefinition {
5762 Type : walletTypeToken ,
5863 Tab : "Ethereum token" ,
5964 Description : desc ,
60- }, netAddrs )
65+ }, netAddrs , netAssetVersions )
6166}
6267
6368func init () {
@@ -1231,11 +1236,13 @@ func (w *ETHWallet) OpenTokenWallet(tokenCfg *asset.TokenConfig) (asset.Wallet,
12311236 return nil , fmt .Errorf ("could not find token with ID %d on network %s" , w .assetID , w .net )
12321237 }
12331238
1239+ supportedAssetVersions := make ([]uint32 , 0 , 1 )
12341240 contracts := make (map [uint32 ]common.Address )
12351241 gases := make (map [uint32 ]* dexeth.Gases )
12361242 for ver , c := range netToken .SwapContracts {
12371243 contracts [ver ] = c .Address
12381244 gases [ver ] = & c .Gas
1245+ supportedAssetVersions = append (supportedAssetVersions , ver )
12391246 }
12401247
12411248 aw := & assetWallet {
@@ -1254,7 +1261,7 @@ func (w *ETHWallet) OpenTokenWallet(tokenCfg *asset.TokenConfig) (asset.Wallet,
12541261 ui : token .UnitInfo ,
12551262 wi : asset.WalletInfo {
12561263 Name : token .Name ,
1257- SupportedVersions : w . wi . SupportedVersions ,
1264+ SupportedVersions : supportedAssetVersions ,
12581265 UnitInfo : token .UnitInfo ,
12591266 },
12601267 tokenAddr : netToken .Address ,
@@ -1671,8 +1678,7 @@ func (w *TokenWallet) FundOrder(ord *asset.Order) (asset.Coins, []dex.Bytes, uin
16711678 dex .BipIDSymbol (w .assetID ), ord .MaxFeeRate , w .gasFeeLimit ())
16721679 }
16731680
1674- contractVer := contractVersion (ord .AssetVersion )
1675- approvalStatus , err := w .approvalStatus (contractVer )
1681+ approvalStatus , err := w .approvalStatus (ord .AssetVersion )
16761682 if err != nil {
16771683 return nil , nil , 0 , fmt .Errorf ("error getting approval status: %v" , err )
16781684 }
@@ -1686,7 +1692,7 @@ func (w *TokenWallet) FundOrder(ord *asset.Order) (asset.Coins, []dex.Bytes, uin
16861692 return nil , nil , 0 , fmt .Errorf ("unknown approval status %d" , approvalStatus )
16871693 }
16881694
1689- g , err := w .initGasEstimate (int (ord .MaxSwapCount ), contractVer ,
1695+ g , err := w .initGasEstimate (int (ord .MaxSwapCount ), contractVersion ( ord . AssetVersion ) ,
16901696 ord .RedeemVersion , ord .RedeemAssetID , ord .MaxFeeRate )
16911697 if err != nil {
16921698 return nil , nil , 0 , fmt .Errorf ("error estimating swap gas: %v" , err )
@@ -1724,7 +1730,7 @@ func (w *ETHWallet) FundMultiOrder(ord *asset.MultiOrder, maxLock uint64) ([]ass
17241730 dex .BipIDSymbol (w .assetID ), ord .MaxFeeRate , w .gasFeeLimit ())
17251731 }
17261732
1727- g , err := w .initGasEstimate (1 , ord .Version , ord .RedeemVersion , ord .RedeemAssetID , ord .MaxFeeRate )
1733+ g , err := w .initGasEstimate (1 , ord .AssetVersion , ord .RedeemVersion , ord .RedeemAssetID , ord .MaxFeeRate )
17281734 if err != nil {
17291735 return nil , nil , 0 , fmt .Errorf ("error estimating swap gas: %v" , err )
17301736 }
@@ -1762,7 +1768,7 @@ func (w *TokenWallet) FundMultiOrder(ord *asset.MultiOrder, maxLock uint64) ([]a
17621768 dex .BipIDSymbol (w .assetID ), ord .MaxFeeRate , w .gasFeeLimit ())
17631769 }
17641770
1765- approvalStatus , err := w .approvalStatus (ord .Version )
1771+ approvalStatus , err := w .approvalStatus (ord .AssetVersion )
17661772 if err != nil {
17671773 return nil , nil , 0 , fmt .Errorf ("error getting approval status: %v" , err )
17681774 }
@@ -1776,7 +1782,7 @@ func (w *TokenWallet) FundMultiOrder(ord *asset.MultiOrder, maxLock uint64) ([]a
17761782 return nil , nil , 0 , fmt .Errorf ("unknown approval status %d" , approvalStatus )
17771783 }
17781784
1779- g , err := w .initGasEstimate (1 , ord .Version ,
1785+ g , err := w .initGasEstimate (1 , ord .AssetVersion ,
17801786 ord .RedeemVersion , ord .RedeemAssetID , ord .MaxFeeRate )
17811787 if err != nil {
17821788 return nil , nil , 0 , fmt .Errorf ("error estimating swap gas: %v" , err )
@@ -1936,7 +1942,7 @@ func (w *assetWallet) approvalGas(newGas *big.Int, contractVer uint32) (uint64,
19361942
19371943 approveGas := ourGas .Approve
19381944
1939- if approveEst , err := w .estimateApproveGas (newGas ); err != nil {
1945+ if approveEst , err := w .estimateApproveGas (contractVer , newGas ); err != nil {
19401946 return 0 , fmt .Errorf ("error estimating approve gas: %v" , err )
19411947 } else if approveEst > approveGas {
19421948 w .log .Warnf ("Approve gas estimate %d is greater than the expected value %d. Using live estimate + 10%%." , approveEst , approveGas )
@@ -2495,8 +2501,8 @@ func (w *assetWallet) tokenBalance() (bal *big.Int, err error) {
24952501
24962502// tokenAllowance checks the amount of tokens that the swap contract is approved
24972503// to spend on behalf of the account handled by the wallet.
2498- func (w * assetWallet ) tokenAllowance () (allowance * big.Int , err error ) {
2499- return allowance , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
2504+ func (w * assetWallet ) tokenAllowance (contractVer uint32 ) (allowance * big.Int , err error ) {
2505+ return allowance , w .withTokenContractor (w .assetID , contractVer , func (c tokenContractor ) error {
25002506 allowance , err = c .allowance (w .ctx )
25012507 return err
25022508 })
@@ -2523,41 +2529,43 @@ func (w *assetWallet) approveToken(ctx context.Context, amount *big.Int, gasLimi
25232529 })
25242530}
25252531
2526- func (w * assetWallet ) approvalStatus (version uint32 ) (asset.ApprovalStatus , error ) {
2532+ func (w * assetWallet ) approvalStatus (assetVer uint32 ) (asset.ApprovalStatus , error ) {
25272533 if w .assetID == w .baseChainID {
25282534 return asset .Approved , nil
25292535 }
25302536
2537+ contractVer := contractVersion (assetVer )
2538+
25312539 // If the result has been cached, return what is in the cache.
25322540 // The cache is cleared if an approval/unapproval tx is done.
25332541 w .approvalsMtx .RLock ()
2534- if approved , cached := w .approvalCache [version ]; cached {
2535- w .approvalsMtx .RUnlock ()
2542+ approved , cached := w .approvalCache [contractVer ]
2543+ _ , pending := w .pendingApprovals [contractVer ]
2544+ w .approvalsMtx .RUnlock ()
2545+ if cached {
25362546 if approved {
25372547 return asset .Approved , nil
25382548 } else {
25392549 return asset .NotApproved , nil
25402550 }
25412551 }
25422552
2543- if _ , pending := w .pendingApprovals [version ]; pending {
2544- w .approvalsMtx .RUnlock ()
2553+ if pending {
25452554 return asset .Pending , nil
25462555 }
2547- w .approvalsMtx .RUnlock ()
25482556
25492557 w .approvalsMtx .Lock ()
25502558 defer w .approvalsMtx .Unlock ()
25512559
2552- currentAllowance , err := w .tokenAllowance ()
2560+ currentAllowance , err := w .tokenAllowance (contractVer )
25532561 if err != nil {
25542562 return asset .NotApproved , fmt .Errorf ("error retrieving current allowance: %w" , err )
25552563 }
25562564 if currentAllowance .Cmp (unlimitedAllowanceReplenishThreshold ) >= 0 {
2557- w .approvalCache [version ] = true
2565+ w .approvalCache [contractVer ] = true
25582566 return asset .Approved , nil
25592567 }
2560- w .approvalCache [version ] = false
2568+ w .approvalCache [contractVer ] = false
25612569 return asset .NotApproved , nil
25622570}
25632571
@@ -2687,16 +2695,14 @@ func (w *TokenWallet) ApprovalFee(assetVer uint32, approve bool) (uint64, error)
26872695// ApprovalStatus returns the approval status for each version of the
26882696// token's swap contract.
26892697func (w * TokenWallet ) ApprovalStatus () map [uint32 ]asset.ApprovalStatus {
2690- versions := w .Info ().SupportedVersions
2691-
26922698 statuses := map [uint32 ]asset.ApprovalStatus {}
2693- for _ , version := range versions {
2694- status , err := w .approvalStatus (version )
2699+ for _ , assetVer := range w . wi . SupportedVersions {
2700+ status , err := w .approvalStatus (assetVer )
26952701 if err != nil {
2696- w .log .Errorf ("error checking approval status for version %d: %w" , version , err )
2702+ w .log .Errorf ("error checking approval status for swap contract version %d: %w" , assetVer , err )
26972703 continue
26982704 }
2699- statuses [version ] = status
2705+ statuses [assetVer ] = status
27002706 }
27012707
27022708 return statuses
@@ -4456,31 +4462,21 @@ func (w *assetWallet) withTokenContractor(assetID, contractVer uint32, f func(to
44564462 return w .withContractor (contractVer , func (c contractor ) error {
44574463 tc , is := c .(tokenContractor )
44584464 if ! is {
4459- return fmt .Errorf ("contractor for %d %T is not a tokenContractor" , assetID , c )
4465+ return fmt .Errorf ("contractor for %s version %d is not a tokenContractor. type = %T " , w . ui . Conventional . Unit , contractVer , c )
44604466 }
44614467 return f (tc )
44624468 })
44634469}
44644470
44654471// estimateApproveGas estimates the gas required for a transaction approving a
44664472// spender for an ERC20 contract.
4467- func (w * assetWallet ) estimateApproveGas (newGas * big.Int ) (gas uint64 , err error ) {
4468- return gas , w .withTokenContractor (w .assetID , dexeth . ContractVersionERC20 , func (c tokenContractor ) error {
4473+ func (w * assetWallet ) estimateApproveGas (contractVer uint32 , newGas * big.Int ) (gas uint64 , err error ) {
4474+ return gas , w .withTokenContractor (w .assetID , contractVer , func (c tokenContractor ) error {
44694475 gas , err = c .estimateApproveGas (w .ctx , newGas )
44704476 return err
44714477 })
44724478}
44734479
4474- // estimateTransferGas estimates the gas needed for a token transfer call to an
4475- // ERC20 contract.
4476- // TODO: Delete this and contractor methods. Unused.
4477- func (w * assetWallet ) estimateTransferGas (val uint64 ) (gas uint64 , err error ) {
4478- return gas , w .withTokenContractor (w .assetID , dexeth .ContractVersionERC20 , func (c tokenContractor ) error {
4479- gas , err = c .estimateTransferGas (w .ctx , w .evmify (val ))
4480- return err
4481- })
4482- }
4483-
44844480// Can uncomment here and in redeem to test rejected redemption reauthorization.
44854481// var firstRedemptionBorked atomic.Bool
44864482
0 commit comments