Skip to content

Commit f9700e1

Browse files
committed
erc20: Add testnet usdc.
1 parent 2294cae commit f9700e1

14 files changed

Lines changed: 218 additions & 124 deletions

File tree

client/asset/eth/eth.go

Lines changed: 13 additions & 10 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
@@ -1445,7 +1446,7 @@ func (w *assetWallet) approvalGas(newGas *big.Int, cfg *dex.Asset) (uint64, erro
14451446
if approveEst, err := w.estimateApproveGas(newGas); err != nil {
14461447
return 0, fmt.Errorf("error estimating approve gas: %v", err)
14471448
} else if approveEst > approveGas {
1448-
w.log.Warnf("Approve gas estimate %d is greater than the expected value %d. Using live estimate + 10%.")
1449+
w.log.Warnf("Approve gas estimate %d is greater than the expected value %d. Using live estimate + 10%%.", approveEst, approveGas)
14491450
return approveEst * 11 / 10, nil
14501451
}
14511452
return approveGas, nil
@@ -1723,6 +1724,8 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
17231724
return fail("Swap: initiate error: %w", err)
17241725
}
17251726

1727+
contractAddr := dexeth.Tokens[w.assetID].NetTokens[w.net].SwapContracts[cfg.Version].Address.String()
1728+
17261729
txHash := tx.Hash()
17271730
for _, swap := range swaps.Contracts {
17281731
var secretHash [dexeth.SecretHashSize]byte
@@ -1733,7 +1736,7 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
17331736
txHash: txHash,
17341737
secretHash: secretHash,
17351738
ver: cfg.Version,
1736-
contractAddr: erc20.ContractAddresses[cfg.Version][w.net].String(),
1739+
contractAddr: contractAddr,
17371740
})
17381741
}
17391742

@@ -3766,7 +3769,7 @@ func checkTxStatus(receipt *types.Receipt, gasLimit uint64) error {
37663769
// factor of 2. The account should already have a trading balance of at least
37673770
// maxSwaps gwei (token or eth), and sufficient eth balance to cover the
37683771
// requisite tx fees.
3769-
func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps int, g *dexeth.Gases, waitForMined func()) error {
3772+
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 {
37703773
tokenContractor, isToken := c.(tokenContractor)
37713774

37723775
stats := struct {
@@ -3876,7 +3879,7 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
38763879
return fmt.Errorf("initiate error for %d swaps: %v", n, err)
38773880
}
38783881
waitForMined()
3879-
receipt, _, err := cl.transactionReceipt(ctx, tx.Hash())
3882+
receipt, err := waitForReceipt(cl, tx)
38803883
if err != nil {
38813884
return err
38823885
}
@@ -3886,15 +3889,15 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
38863889
stats.swaps = append(stats.swaps, receipt.GasUsed)
38873890

38883891
if isToken {
3889-
receipt, _, err = cl.transactionReceipt(ctx, approveTx.Hash())
3892+
receipt, err = waitForReceipt(cl, approveTx)
38903893
if err != nil {
38913894
return err
38923895
}
38933896
if err := checkTxStatus(receipt, txOpts.GasLimit); err != nil {
38943897
return err
38953898
}
38963899
stats.approves = append(stats.approves, receipt.GasUsed)
3897-
receipt, _, err = cl.transactionReceipt(ctx, transferTx.Hash())
3900+
receipt, err = waitForReceipt(cl, transferTx)
38983901
if err != nil {
38993902
return err
39003903
}
@@ -3929,7 +3932,7 @@ func GetGasEstimates(ctx context.Context, cl ethFetcher, c contractor, maxSwaps
39293932
return fmt.Errorf("redeem error for %d swaps: %v", n, err)
39303933
}
39313934
waitForMined()
3932-
receipt, _, err = cl.transactionReceipt(ctx, tx.Hash())
3935+
receipt, err = waitForReceipt(cl, tx)
39333936
if err != nil {
39343937
return err
39353938
}

client/asset/eth/eth_test.go

Lines changed: 23 additions & 23 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,
@@ -639,8 +639,8 @@ func tassetWallet(assetID uint32) (asset.Wallet, *assetWallet, *testNode, contex
639639
token: dexeth.Tokens[testTokenID],
640640
}
641641
aw.wallets = map[uint32]*assetWallet{
642-
testTokenID: aw,
643-
BipID: node.tokenParent,
642+
simnetTokenID: aw,
643+
BipID: node.tokenParent,
644644
}
645645
}
646646

@@ -734,7 +734,7 @@ func TestBalance(t *testing.T) {
734734
for _, test := range tests {
735735
var assetID uint32 = BipID
736736
if test.token {
737-
assetID = testTokenID
737+
assetID = simnetTokenID
738738
}
739739

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

848848
func TestRefund(t *testing.T) {
849849
t.Run("eth", func(t *testing.T) { testRefund(t, BipID) })
850-
t.Run("token", func(t *testing.T) { testRefund(t, testTokenID) })
850+
t.Run("token", func(t *testing.T) { testRefund(t, simnetTokenID) })
851851
}
852852

853853
func testRefund(t *testing.T, assetID uint32) {
@@ -870,7 +870,7 @@ func testRefund(t *testing.T, assetID uint32) {
870870
dexeth.VersionedGases[1] = gasesV1
871871
defer delete(dexeth.VersionedGases, 1)
872872
} else {
873-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
873+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
874874
tc := *tokenContracts[0]
875875
tc.Gas = *gasesV1
876876
tokenContracts[1] = &tc
@@ -1030,7 +1030,7 @@ func (b *badCoin) Value() uint64 {
10301030

10311031
func TestFundOrderReturnCoinsFundingCoins(t *testing.T) {
10321032
t.Run("eth", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, BipID) })
1033-
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, testTokenID) })
1033+
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, simnetTokenID) })
10341034
}
10351035

10361036
func testFundOrderReturnCoinsFundingCoins(t *testing.T, assetID uint32) {
@@ -1446,7 +1446,7 @@ func TestPreSwap(t *testing.T) {
14461446
var assetID uint32 = BipID
14471447
assetCfg := tETH
14481448
if test.token {
1449-
assetID = testTokenID
1449+
assetID = simnetTokenID
14501450
assetCfg = tToken
14511451
}
14521452

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

15031503
func TestSwap(t *testing.T) {
15041504
t.Run("eth", func(t *testing.T) { testSwap(t, BipID) })
1505-
t.Run("token", func(t *testing.T) { testSwap(t, testTokenID) })
1505+
t.Run("token", func(t *testing.T) { testSwap(t, simnetTokenID) })
15061506
}
15071507

15081508
func testSwap(t *testing.T, assetID uint32) {
@@ -1781,7 +1781,7 @@ func TestPreRedeem(t *testing.T) {
17811781
}
17821782

17831783
// Token
1784-
w, _, node, shutdown2 := tassetWallet(testTokenID)
1784+
w, _, node, shutdown2 := tassetWallet(simnetTokenID)
17851785
defer shutdown2()
17861786

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

18001800
func TestRedeem(t *testing.T) {
18011801
t.Run("eth", func(t *testing.T) { testRedeem(t, BipID) })
1802-
t.Run("token", func(t *testing.T) { testRedeem(t, testTokenID) })
1802+
t.Run("token", func(t *testing.T) { testRedeem(t, simnetTokenID) })
18031803
}
18041804

18051805
func testRedeem(t *testing.T, assetID uint32) {
@@ -1809,7 +1809,7 @@ func testRedeem(t *testing.T, assetID uint32) {
18091809
// Test with a non-zero contract version to ensure it makes it into the receipt
18101810
contractVer := uint32(1)
18111811
dexeth.VersionedGases[1] = ethGases // for dexeth.RedeemGas(..., 1)
1812-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
1812+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
18131813
tokenContracts[1] = tokenContracts[0]
18141814
defer delete(dexeth.VersionedGases, 1)
18151815
defer delete(tokenContracts, 1)
@@ -2474,7 +2474,7 @@ func TestMaxOrder(t *testing.T) {
24742474
var assetID uint32 = BipID
24752475
dexAsset := tETH
24762476
if test.token {
2477-
assetID = testTokenID
2477+
assetID = simnetTokenID
24782478
}
24792479

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

25632563
func TestAuditContract(t *testing.T) {
25642564
t.Run("eth", func(t *testing.T) { testAuditContract(t, BipID) })
2565-
t.Run("token", func(t *testing.T) { testAuditContract(t, testTokenID) })
2565+
t.Run("token", func(t *testing.T) { testAuditContract(t, simnetTokenID) })
25662566
}
25672567

25682568
func testAuditContract(t *testing.T, assetID uint32) {
@@ -3148,7 +3148,7 @@ func TestLocktimeExpired(t *testing.T) {
31483148

31493149
func TestFindRedemption(t *testing.T) {
31503150
t.Run("eth", func(t *testing.T) { testFindRedemption(t, BipID) })
3151-
t.Run("token", func(t *testing.T) { testFindRedemption(t, testTokenID) })
3151+
t.Run("token", func(t *testing.T) { testFindRedemption(t, simnetTokenID) })
31523152
}
31533153

31543154
func testFindRedemption(t *testing.T, assetID uint32) {
@@ -3290,7 +3290,7 @@ func testFindRedemption(t *testing.T, assetID uint32) {
32903290

32913291
func TestRefundReserves(t *testing.T) {
32923292
t.Run("eth", func(t *testing.T) { testRefundReserves(t, BipID) })
3293-
t.Run("token", func(t *testing.T) { testRefundReserves(t, testTokenID) })
3293+
t.Run("token", func(t *testing.T) { testRefundReserves(t, simnetTokenID) })
32943294
}
32953295

32963296
func testRefundReserves(t *testing.T, assetID uint32) {
@@ -3318,7 +3318,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {
33183318
feeWallet = node.tokenParent
33193319
assetV0 = *tToken
33203320
assetV1 = *tToken
3321-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
3321+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
33223322
gasesV0 = &tokenGases
33233323
tc := *tokenContracts[0]
33243324
tc.Gas = *gasesV1
@@ -3385,7 +3385,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {
33853385

33863386
func TestRedemptionReserves(t *testing.T) {
33873387
t.Run("eth", func(t *testing.T) { testRedemptionReserves(t, BipID) })
3388-
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, testTokenID) })
3388+
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, simnetTokenID) })
33893389
}
33903390

33913391
func testRedemptionReserves(t *testing.T, assetID uint32) {
@@ -3413,7 +3413,7 @@ func testRedemptionReserves(t *testing.T, assetID uint32) {
34133413
feeWallet = node.tokenParent
34143414
assetV0 = *tToken
34153415
assetV1 = *tToken
3416-
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
3416+
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
34173417
gasesV0 = &tokenGases
34183418
tc := *tokenContracts[0]
34193419
tc.Gas = *gasesV1
@@ -3517,7 +3517,7 @@ func TestReconfigure(t *testing.T) {
35173517

35183518
func TestSend(t *testing.T) {
35193519
t.Run("eth", func(t *testing.T) { testSend(t, BipID) })
3520-
t.Run("token", func(t *testing.T) { testSend(t, testTokenID) })
3520+
t.Run("token", func(t *testing.T) { testSend(t, simnetTokenID) })
35213521
}
35223522

35233523
func testSend(t *testing.T, assetID uint32) {
@@ -3602,7 +3602,7 @@ func testSend(t *testing.T, assetID uint32) {
36023602

36033603
func TestConfirmRedemption(t *testing.T) {
36043604
t.Run("eth", func(t *testing.T) { testConfirmRedemption(t, BipID) })
3605-
t.Run("token", func(t *testing.T) { testConfirmRedemption(t, testTokenID) })
3605+
t.Run("token", func(t *testing.T) { testConfirmRedemption(t, simnetTokenID) })
36063606
}
36073607

36083608
func testConfirmRedemption(t *testing.T, assetID uint32) {
@@ -4458,7 +4458,7 @@ func TestMarshalMonitoredTx(t *testing.T) {
44584458

44594459
func TestEstimateSendTxFee(t *testing.T) {
44604460
t.Run("eth", func(t *testing.T) { testEstimateSendTxFee(t, BipID) })
4461-
t.Run("token", func(t *testing.T) { testEstimateSendTxFee(t, testTokenID) })
4461+
t.Run("token", func(t *testing.T) { testEstimateSendTxFee(t, simnetTokenID) })
44624462
}
44634463

44644464
func testEstimateSendTxFee(t *testing.T, assetID uint32) {

client/asset/eth/multirpc.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,13 +570,19 @@ func (m *multiRPCClient) transactionReceipt(ctx context.Context, txHash common.H
570570
return r, tx, nil
571571
}
572572

573+
var notFound bool
573574
// Fetch a fresh one.
574575
if err = m.withPreferred(func(p *provider) error {
575576
r, err = p.ec.TransactionReceipt(ctx, txHash)
577+
if err != nil {
578+
if strings.Contains(err.Error(), "not found") {
579+
notFound = true
580+
}
581+
}
576582
return err
577583
}); err != nil {
578584
if isNotFoundError(err) {
579-
return nil, nil, asset.ErrNotEnoughConfirms
585+
return nil, nil, asset.CoinNotFoundError
580586
}
581587
return nil, nil, err
582588
}

0 commit comments

Comments
 (0)