Skip to content

Commit 0a14bc1

Browse files
committed
erc20: Add testnet usdc.
1 parent cad0336 commit 0a14bc1

18 files changed

Lines changed: 331 additions & 146 deletions

File tree

client/asset/eth/eth.go

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030
"decred.org/dcrdex/dex/config"
3131
"decred.org/dcrdex/dex/encode"
3232
"decred.org/dcrdex/dex/keygen"
33-
"decred.org/dcrdex/dex/networks/erc20"
3433
dexeth "decred.org/dcrdex/dex/networks/eth"
3534
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
3635
"github.com/decred/dcrd/hdkeychain/v3"
@@ -60,7 +59,8 @@ func registerToken(tokenID uint32, desc string, nets ...dex.Network) {
6059
func init() {
6160
asset.Register(BipID, &Driver{})
6261
// Test token
63-
registerToken(testTokenID, "A token wallet for the DEX test token. Used for testing DEX software.", dex.Simnet)
62+
registerToken(simnetTokenID, "A token wallet for the DEX test token. Used for testing DEX software.", dex.Simnet)
63+
registerToken(usdcTokenID, "The USDC Ethereum ERC20 token.", dex.Testnet)
6464
}
6565

6666
const (
@@ -83,7 +83,8 @@ const (
8383
)
8484

8585
var (
86-
testTokenID, _ = dex.BipSymbolID("dextt.eth")
86+
simnetTokenID, _ = dex.BipSymbolID("dextt.eth")
87+
usdcTokenID, _ = dex.BipSymbolID("usdc.eth")
8788
// blockTicker is the delay between calls to check for new blocks.
8889
blockTicker = time.Second
8990
peerCountTicker = 5 * time.Second
@@ -1464,7 +1465,7 @@ func (w *assetWallet) approvalGas(newGas *big.Int, cfg *dex.Asset) (uint64, erro
14641465
if approveEst, err := w.estimateApproveGas(newGas); err != nil {
14651466
return 0, fmt.Errorf("error estimating approve gas: %v", err)
14661467
} else if approveEst > approveGas {
1467-
w.log.Warnf("Approve gas estimate %d is greater than the expected value %d. Using live estimate + 10%.")
1468+
w.log.Warnf("Approve gas estimate %d is greater than the expected value %d. Using live estimate + 10%%.", approveEst, approveGas)
14681469
return approveEst * 11 / 10, nil
14691470
}
14701471
return approveGas, nil
@@ -1742,6 +1743,8 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
17421743
return fail("Swap: initiate error: %w", err)
17431744
}
17441745

1746+
contractAddr := dexeth.Tokens[w.assetID].NetTokens[w.net].SwapContracts[cfg.Version].Address.String()
1747+
17451748
txHash := tx.Hash()
17461749
for _, swap := range swaps.Contracts {
17471750
var secretHash [dexeth.SecretHashSize]byte
@@ -1752,7 +1755,7 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
17521755
txHash: txHash,
17531756
secretHash: secretHash,
17541757
ver: cfg.Version,
1755-
contractAddr: erc20.ContractAddresses[cfg.Version][w.net].String(),
1758+
contractAddr: contractAddr,
17561759
})
17571760
}
17581761

@@ -3373,7 +3376,7 @@ func (w *assetWallet) confirmRedemption(coinID dex.Bytes, redemption *asset.Rede
33733376

33743377
monitoredTx, err := w.getLatestMonitoredTx(txHash)
33753378
if err != nil {
3376-
w.log.Error("getLatestMonitoredTx error: %v", err)
3379+
w.log.Errorf("getLatestMonitoredTx error: %v", err)
33773380
return w.confirmRedemptionWithoutMonitoredTx(txHash, redemption, feeWallet)
33783381
}
33793382
// This mutex is locked inside of getLatestMonitoredTx.
@@ -3785,7 +3788,7 @@ func checkTxStatus(receipt *types.Receipt, gasLimit uint64) error {
37853788
// factor of 2. The account should already have a trading balance of at least
37863789
// maxSwaps gwei (token or eth), and sufficient eth balance to cover the
37873790
// requisite tx fees.
3788-
func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps int, g *dexeth.Gases, waitForMined func()) error {
3791+
func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps int, g *dexeth.Gases, waitForMined func(), waitForReceipt func(ethFetcher, *types.Transaction) (*types.Receipt, error)) error {
37893792
tokenContractor, isToken := c.(tokenContractor)
37903793

37913794
stats := struct {
@@ -3895,7 +3898,7 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
38953898
return fmt.Errorf("initiate error for %d swaps: %v", n, err)
38963899
}
38973900
waitForMined()
3898-
receipt, _, err := cl.transactionReceipt(ctx, tx.Hash())
3901+
receipt, err := waitForReceipt(cl, tx)
38993902
if err != nil {
39003903
return err
39013904
}
@@ -3905,15 +3908,15 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
39053908
stats.swaps = append(stats.swaps, receipt.GasUsed)
39063909

39073910
if isToken {
3908-
receipt, _, err = cl.transactionReceipt(ctx, approveTx.Hash())
3911+
receipt, err = waitForReceipt(cl, approveTx)
39093912
if err != nil {
39103913
return err
39113914
}
39123915
if err := checkTxStatus(receipt, txOpts.GasLimit); err != nil {
39133916
return err
39143917
}
39153918
stats.approves = append(stats.approves, receipt.GasUsed)
3916-
receipt, _, err = cl.transactionReceipt(ctx, transferTx.Hash())
3919+
receipt, err = waitForReceipt(cl, transferTx)
39173920
if err != nil {
39183921
return err
39193922
}
@@ -3948,7 +3951,7 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
39483951
return fmt.Errorf("redeem error for %d swaps: %v", n, err)
39493952
}
39503953
waitForMined()
3951-
receipt, _, err = cl.transactionReceipt(ctx, tx.Hash())
3954+
receipt, err = waitForReceipt(cl, tx)
39523955
if err != nil {
39533956
return err
39543957
}

client/asset/eth/eth_test.go

Lines changed: 61 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ var (
4646
testAddressC = common.HexToAddress("2b84C791b79Ee37De042AD2ffF1A253c3ce9bc27")
4747

4848
ethGases = dexeth.VersionedGases[0]
49-
tokenGases = dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts[0].Gas
49+
tokenGases = dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts[0].Gas
5050

5151
tETH = &dex.Asset{
5252
ID: 60,
@@ -69,7 +69,7 @@ var (
6969
}
7070

7171
tToken = &dex.Asset{
72-
ID: testTokenID,
72+
ID: simnetTokenID,
7373
Symbol: "dextt.eth",
7474
Version: 0,
7575
SwapSize: tokenGases.Swap,
@@ -628,19 +628,15 @@ func tassetWallet(assetID uint32) (asset.Wallet, *assetWallet, *testNode, contex
628628
assetID: BipID,
629629
atomize: dexeth.WeiToGwei,
630630
}
631-
testTokenID, found := dex.BipSymbolID("dextt.eth")
632-
if !found {
633-
panic("could not find test token ID")
634-
}
635631
w = &TokenWallet{
636632
assetWallet: aw,
637633
cfg: &tokenWalletConfig{},
638634
parent: node.tokenParent,
639-
token: dexeth.Tokens[testTokenID],
635+
token: dexeth.Tokens[simnetTokenID],
640636
}
641637
aw.wallets = map[uint32]*assetWallet{
642-
testTokenID: aw,
643-
BipID: node.tokenParent,
638+
simnetTokenID: aw,
639+
BipID: node.tokenParent,
644640
}
645641
}
646642

@@ -734,7 +730,7 @@ func TestBalance(t *testing.T) {
734730
for _, test := range tests {
735731
var assetID uint32 = BipID
736732
if test.token {
737-
assetID = testTokenID
733+
assetID = simnetTokenID
738734
}
739735

740736
_, eth, node, shutdown := tassetWallet(assetID)
@@ -847,7 +843,7 @@ func TestFeeRate(t *testing.T) {
847843

848844
func TestRefund(t *testing.T) {
849845
t.Run("eth", func(t *testing.T) { testRefund(t, BipID) })
850-
t.Run("token", func(t *testing.T) { testRefund(t, testTokenID) })
846+
t.Run("token", func(t *testing.T) { testRefund(t, simnetTokenID) })
851847
}
852848

853849
func testRefund(t *testing.T, assetID uint32) {
@@ -870,7 +866,7 @@ func testRefund(t *testing.T, assetID uint32) {
870866
dexeth.VersionedGases[1] = gasesV1
871867
defer delete(dexeth.VersionedGases, 1)
872868
} else {
873-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
869+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
874870
tc := *tokenContracts[0]
875871
tc.Gas = *gasesV1
876872
tokenContracts[1] = &tc
@@ -1030,7 +1026,7 @@ func (b *badCoin) Value() uint64 {
10301026

10311027
func TestFundOrderReturnCoinsFundingCoins(t *testing.T) {
10321028
t.Run("eth", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, BipID) })
1033-
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, testTokenID) })
1029+
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, simnetTokenID) })
10341030
}
10351031

10361032
func testFundOrderReturnCoinsFundingCoins(t *testing.T, assetID uint32) {
@@ -1446,7 +1442,7 @@ func TestPreSwap(t *testing.T) {
14461442
var assetID uint32 = BipID
14471443
assetCfg := tETH
14481444
if test.token {
1449-
assetID = testTokenID
1445+
assetID = simnetTokenID
14501446
assetCfg = tToken
14511447
}
14521448

@@ -1502,7 +1498,7 @@ func TestPreSwap(t *testing.T) {
15021498

15031499
func TestSwap(t *testing.T) {
15041500
t.Run("eth", func(t *testing.T) { testSwap(t, BipID) })
1505-
t.Run("token", func(t *testing.T) { testSwap(t, testTokenID) })
1501+
t.Run("token", func(t *testing.T) { testSwap(t, simnetTokenID) })
15061502
}
15071503

15081504
func testSwap(t *testing.T, assetID uint32) {
@@ -1781,7 +1777,7 @@ func TestPreRedeem(t *testing.T) {
17811777
}
17821778

17831779
// Token
1784-
w, _, node, shutdown2 := tassetWallet(testTokenID)
1780+
w, _, node, shutdown2 := tassetWallet(simnetTokenID)
17851781
defer shutdown2()
17861782

17871783
form.AssetConfig = tToken
@@ -1799,7 +1795,7 @@ func TestPreRedeem(t *testing.T) {
17991795

18001796
func TestRedeem(t *testing.T) {
18011797
t.Run("eth", func(t *testing.T) { testRedeem(t, BipID) })
1802-
t.Run("token", func(t *testing.T) { testRedeem(t, testTokenID) })
1798+
t.Run("token", func(t *testing.T) { testRedeem(t, simnetTokenID) })
18031799
}
18041800

18051801
func testRedeem(t *testing.T, assetID uint32) {
@@ -1809,7 +1805,7 @@ func testRedeem(t *testing.T, assetID uint32) {
18091805
// Test with a non-zero contract version to ensure it makes it into the receipt
18101806
contractVer := uint32(1)
18111807
dexeth.VersionedGases[1] = ethGases // for dexeth.RedeemGas(..., 1)
1812-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
1808+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
18131809
tokenContracts[1] = tokenContracts[0]
18141810
defer delete(dexeth.VersionedGases, 1)
18151811
defer delete(tokenContracts, 1)
@@ -2474,7 +2470,7 @@ func TestMaxOrder(t *testing.T) {
24742470
var assetID uint32 = BipID
24752471
dexAsset := tETH
24762472
if test.token {
2477-
assetID = testTokenID
2473+
assetID = simnetTokenID
24782474
}
24792475

24802476
w, _, node, shutdown := tassetWallet(assetID)
@@ -2562,7 +2558,7 @@ func packRedeemDataV0(redemptions []*dexeth.Redemption) ([]byte, error) {
25622558

25632559
func TestAuditContract(t *testing.T) {
25642560
t.Run("eth", func(t *testing.T) { testAuditContract(t, BipID) })
2565-
t.Run("token", func(t *testing.T) { testAuditContract(t, testTokenID) })
2561+
t.Run("token", func(t *testing.T) { testAuditContract(t, simnetTokenID) })
25662562
}
25672563

25682564
func testAuditContract(t *testing.T, assetID uint32) {
@@ -3148,7 +3144,7 @@ func TestLocktimeExpired(t *testing.T) {
31483144

31493145
func TestFindRedemption(t *testing.T) {
31503146
t.Run("eth", func(t *testing.T) { testFindRedemption(t, BipID) })
3151-
t.Run("token", func(t *testing.T) { testFindRedemption(t, testTokenID) })
3147+
t.Run("token", func(t *testing.T) { testFindRedemption(t, simnetTokenID) })
31523148
}
31533149

31543150
func testFindRedemption(t *testing.T, assetID uint32) {
@@ -3290,7 +3286,7 @@ func testFindRedemption(t *testing.T, assetID uint32) {
32903286

32913287
func TestRefundReserves(t *testing.T) {
32923288
t.Run("eth", func(t *testing.T) { testRefundReserves(t, BipID) })
3293-
t.Run("token", func(t *testing.T) { testRefundReserves(t, testTokenID) })
3289+
t.Run("token", func(t *testing.T) { testRefundReserves(t, simnetTokenID) })
32943290
}
32953291

32963292
func testRefundReserves(t *testing.T, assetID uint32) {
@@ -3318,7 +3314,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {
33183314
feeWallet = node.tokenParent
33193315
assetV0 = *tToken
33203316
assetV1 = *tToken
3321-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
3317+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
33223318
gasesV0 = &tokenGases
33233319
tc := *tokenContracts[0]
33243320
tc.Gas = *gasesV1
@@ -3385,7 +3381,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {
33853381

33863382
func TestRedemptionReserves(t *testing.T) {
33873383
t.Run("eth", func(t *testing.T) { testRedemptionReserves(t, BipID) })
3388-
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, testTokenID) })
3384+
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, simnetTokenID) })
33893385
}
33903386

33913387
func testRedemptionReserves(t *testing.T, assetID uint32) {
@@ -3413,7 +3409,7 @@ func testRedemptionReserves(t *testing.T, assetID uint32) {
34133409
feeWallet = node.tokenParent
34143410
assetV0 = *tToken
34153411
assetV1 = *tToken
3416-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
3412+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
34173413
gasesV0 = &tokenGases
34183414
tc := *tokenContracts[0]
34193415
tc.Gas = *gasesV1
@@ -3517,7 +3513,7 @@ func TestReconfigure(t *testing.T) {
35173513

35183514
func TestSend(t *testing.T) {
35193515
t.Run("eth", func(t *testing.T) { testSend(t, BipID) })
3520-
t.Run("token", func(t *testing.T) { testSend(t, testTokenID) })
3516+
t.Run("token", func(t *testing.T) { testSend(t, simnetTokenID) })
35213517
}
35223518

35233519
func testSend(t *testing.T, assetID uint32) {
@@ -3602,7 +3598,7 @@ func testSend(t *testing.T, assetID uint32) {
36023598

36033599
func TestConfirmRedemption(t *testing.T) {
36043600
t.Run("eth", func(t *testing.T) { testConfirmRedemption(t, BipID) })
3605-
t.Run("token", func(t *testing.T) { testConfirmRedemption(t, testTokenID) })
3601+
t.Run("token", func(t *testing.T) { testConfirmRedemption(t, simnetTokenID) })
36063602
}
36073603

36083604
func testConfirmRedemption(t *testing.T, assetID uint32) {
@@ -4458,7 +4454,7 @@ func TestMarshalMonitoredTx(t *testing.T) {
44584454

44594455
func TestEstimateSendTxFee(t *testing.T) {
44604456
t.Run("eth", func(t *testing.T) { testEstimateSendTxFee(t, BipID) })
4461-
t.Run("token", func(t *testing.T) { testEstimateSendTxFee(t, testTokenID) })
4457+
t.Run("token", func(t *testing.T) { testEstimateSendTxFee(t, simnetTokenID) })
44624458
}
44634459

44644460
func testEstimateSendTxFee(t *testing.T, assetID uint32) {
@@ -4542,7 +4538,7 @@ func testEstimateSendTxFee(t *testing.T, assetID uint32) {
45424538
// contract (that require more gas) are added.
45434539
func TestMaxSwapRedeemLots(t *testing.T) {
45444540
t.Run("eth", func(t *testing.T) { testMaxSwapRedeemLots(t, BipID) })
4545-
t.Run("token", func(t *testing.T) { testMaxSwapRedeemLots(t, testTokenID) })
4541+
t.Run("token", func(t *testing.T) { testMaxSwapRedeemLots(t, simnetTokenID) })
45464542
}
45474543

45484544
func testMaxSwapRedeemLots(t *testing.T, assetID uint32) {
@@ -4837,6 +4833,42 @@ func TestReceiptCache(t *testing.T) {
48374833

48384834
}
48394835

4836+
func TestGoodNonce(t *testing.T) {
4837+
mRPC := new(multiRPCClient)
4838+
tests := []struct {
4839+
name string
4840+
nonce uint64
4841+
want bool
4842+
wait bool
4843+
}{{
4844+
name: "ok initiation",
4845+
nonce: 0,
4846+
want: true,
4847+
}, {
4848+
name: "ok larger",
4849+
nonce: 1,
4850+
want: true,
4851+
}, {
4852+
name: "same nonce",
4853+
nonce: 1,
4854+
// Uncomment for full tests.
4855+
// }, {
4856+
// name: "ok after expiration",
4857+
// nonce: 1,
4858+
// wait: true,
4859+
// want: true,
4860+
}}
4861+
for _, test := range tests {
4862+
if test.wait {
4863+
time.Sleep(time.Minute + time.Second)
4864+
}
4865+
got := mRPC.goodNonce(test.nonce)
4866+
if test.want != got {
4867+
t.Fatalf("%q: wanted %v got %v", test.name, test.want, got)
4868+
}
4869+
}
4870+
}
4871+
48404872
func parseRecoveryID(c asset.Coin) []byte {
48414873
return c.(asset.RecoveryCoin).RecoveryID()
48424874
}

0 commit comments

Comments
 (0)