Skip to content

Commit 5e0a1b5

Browse files
committed
more version clarification. base supported versions on available contracts
1 parent 97c2cae commit 5e0a1b5

19 files changed

Lines changed: 103 additions & 118 deletions

File tree

client/asset/driver.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import (
1616
// networks enables filtering out tokens via the package's SetNetwork.
1717
type nettedToken struct {
1818
*Token
19-
addrs map[dex.Network]string
19+
erc20NetAddrs map[dex.Network]string
20+
netSupportedAssetVersions map[dex.Network][]uint32
2021
}
2122

2223
var (
@@ -85,7 +86,13 @@ func Register(assetID uint32, driver Driver) {
8586
// RegisterToken should be called to register tokens. If no nets are specified
8687
// the token will be registered for all networks. The user must invoke
8788
// SetNetwork to enable net-based filtering of package function output.
88-
func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition, addrs map[dex.Network]string) {
89+
func RegisterToken(
90+
tokenID uint32,
91+
token *dex.Token,
92+
walletDef *WalletDefinition,
93+
erc20NetAddrs map[dex.Network]string,
94+
netSupportedAssetVersions map[dex.Network][]uint32,
95+
) {
8996
driversMtx.Lock()
9097
defer driversMtx.Unlock()
9198
if _, exists := tokens[tokenID]; exists {
@@ -101,7 +108,8 @@ func RegisterToken(tokenID uint32, token *dex.Token, walletDef *WalletDefinition
101108
Definition: walletDef,
102109
// ContractAddress specified in SetNetwork.
103110
},
104-
addrs: addrs,
111+
erc20NetAddrs: erc20NetAddrs,
112+
netSupportedAssetVersions: netSupportedAssetVersions,
105113
}
106114
}
107115

@@ -262,12 +270,13 @@ func UnitInfo(assetID uint32) (dex.UnitInfo, error) {
262270
// network. SetNetwork need only be called once during initialization.
263271
func SetNetwork(net dex.Network) {
264272
for assetID, nt := range tokens {
265-
addr, exists := nt.addrs[net]
273+
addr, exists := nt.erc20NetAddrs[net]
266274
if !exists {
267275
delete(tokens, assetID)
268276
continue
269277
}
270278
nt.Token.ContractAddress = addr
279+
nt.Token.SupportedAssetVersions = nt.netSupportedAssetVersions[net]
271280
}
272281
}
273282

client/asset/eth/eth.go

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -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

6368
func 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.
26892697
func (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

client/asset/eth/eth_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2189,8 +2189,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
21892189
tokenBal: uint64(dexeth.GweiFactor),
21902190
parentBal: uint64(dexeth.GweiFactor),
21912191
multiOrder: &asset.MultiOrder{
2192-
Version: fromAsset.Version,
2193-
MaxFeeRate: fromAsset.MaxFeeRate,
2192+
AssetVersion: fromAsset.Version,
2193+
MaxFeeRate: fromAsset.MaxFeeRate,
21942194
Values: []*asset.MultiOrderValue{
21952195
{
21962196
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2209,8 +2209,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
22092209
tokenBal: uint64(dexeth.GweiFactor),
22102210
parentBal: uint64(dexeth.GweiFactor),
22112211
multiOrder: &asset.MultiOrder{
2212-
Version: fromAsset.Version,
2213-
MaxFeeRate: fromAsset.MaxFeeRate,
2212+
AssetVersion: fromAsset.Version,
2213+
MaxFeeRate: fromAsset.MaxFeeRate,
22142214
Values: []*asset.MultiOrderValue{
22152215
{
22162216
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2231,8 +2231,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
22312231
tokenBal: uint64(dexeth.GweiFactor),
22322232
parentBal: uint64(dexeth.GweiFactor),
22332233
multiOrder: &asset.MultiOrder{
2234-
Version: fromAsset.Version,
2235-
MaxFeeRate: fromAsset.MaxFeeRate,
2234+
AssetVersion: fromAsset.Version,
2235+
MaxFeeRate: fromAsset.MaxFeeRate,
22362236
Values: []*asset.MultiOrderValue{
22372237
{
22382238
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2254,8 +2254,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
22542254
tokenBal: uint64(dexeth.GweiFactor),
22552255
parentBal: uint64(dexeth.GweiFactor),
22562256
multiOrder: &asset.MultiOrder{
2257-
Version: fromAsset.Version,
2258-
MaxFeeRate: fromAsset.MaxFeeRate,
2257+
AssetVersion: fromAsset.Version,
2258+
MaxFeeRate: fromAsset.MaxFeeRate,
22592259
Values: []*asset.MultiOrderValue{
22602260
{
22612261
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2276,8 +2276,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
22762276
tokenBal: uint64(dexeth.GweiFactor),
22772277
parentBal: uint64(dexeth.GweiFactor),
22782278
multiOrder: &asset.MultiOrder{
2279-
Version: fromAsset.Version,
2280-
MaxFeeRate: fromAsset.MaxFeeRate,
2279+
AssetVersion: fromAsset.Version,
2280+
MaxFeeRate: fromAsset.MaxFeeRate,
22812281
Values: []*asset.MultiOrderValue{
22822282
{
22832283
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2298,8 +2298,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
22982298
tokenBal: uint64(dexeth.GweiFactor) - 1,
22992299
parentBal: uint64(dexeth.GweiFactor),
23002300
multiOrder: &asset.MultiOrder{
2301-
Version: fromAsset.Version,
2302-
MaxFeeRate: fromAsset.MaxFeeRate,
2301+
AssetVersion: fromAsset.Version,
2302+
MaxFeeRate: fromAsset.MaxFeeRate,
23032303
Values: []*asset.MultiOrderValue{
23042304
{
23052305
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2319,8 +2319,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
23192319
tokenBal: uint64(dexeth.GweiFactor),
23202320
parentBal: swapGas * 4 * fromAsset.MaxFeeRate,
23212321
multiOrder: &asset.MultiOrder{
2322-
Version: fromAsset.Version,
2323-
MaxFeeRate: fromAsset.MaxFeeRate,
2322+
AssetVersion: fromAsset.Version,
2323+
MaxFeeRate: fromAsset.MaxFeeRate,
23242324
Values: []*asset.MultiOrderValue{
23252325
{
23262326
Value: uint64(dexeth.GweiFactor) / 2,
@@ -2339,8 +2339,8 @@ func testFundMultiOrder(t *testing.T, assetID uint32) {
23392339
tokenBal: uint64(dexeth.GweiFactor),
23402340
parentBal: swapGas*4*fromAsset.MaxFeeRate - 1,
23412341
multiOrder: &asset.MultiOrder{
2342-
Version: fromAsset.Version,
2343-
MaxFeeRate: fromAsset.MaxFeeRate,
2342+
AssetVersion: fromAsset.Version,
2343+
MaxFeeRate: fromAsset.MaxFeeRate,
23442344
Values: []*asset.MultiOrderValue{
23452345
{
23462346
Value: uint64(dexeth.GweiFactor) / 2,

client/asset/interface.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,8 +300,9 @@ type WalletDefinition struct {
300300
// Token combines the generic dex.Token with a WalletDefinition.
301301
type Token struct {
302302
*dex.Token
303-
Definition *WalletDefinition `json:"definition"`
304-
ContractAddress string `json:"contractAddress"` // Set in SetNetwork
303+
Definition *WalletDefinition `json:"definition"`
304+
ContractAddress string `json:"contractAddress"` // Set in SetNetwork
305+
SupportedAssetVersions []uint32 `json:"supportedAssetVersions"`
305306
}
306307

307308
// WalletInfo is auxiliary information about an ExchangeWallet.
@@ -1440,10 +1441,10 @@ type MultiOrderValue struct {
14401441

14411442
// MultiOrder is order details needed for FundMultiOrder.
14421443
type MultiOrder struct {
1443-
// Version is the asset version of the "from" asset with the init
1444+
// AssetVersion is the asset version of the "from" asset with the init
14441445
// transaction.
1445-
Version uint32
1446-
Values []*MultiOrderValue
1446+
AssetVersion uint32
1447+
Values []*MultiOrderValue
14471448
// MaxFeeRate is the largest possible fee rate for the init transaction (of
14481449
// this "from" asset) specific to and provided by a particular server, and
14491450
// is used to calculate the funding required to cover fees.

0 commit comments

Comments
 (0)