Skip to content

Commit

Permalink
erc20: Add testnet usdc.
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeGruffins committed Jul 26, 2022
1 parent c5b40ba commit 9d7413e
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 70 deletions.
19 changes: 10 additions & 9 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"decred.org/dcrdex/dex/config"
"decred.org/dcrdex/dex/encode"
"decred.org/dcrdex/dex/keygen"
"decred.org/dcrdex/dex/networks/erc20"
dexeth "decred.org/dcrdex/dex/networks/eth"
"github.com/decred/dcrd/dcrec/secp256k1/v4/ecdsa"
"github.com/decred/dcrd/hdkeychain/v3"
Expand All @@ -53,7 +52,7 @@ func registerToken(tokenID uint32, desc string, nets ...dex.Network) {
func init() {
asset.Register(BipID, &Driver{})
// Test token
registerToken(testTokenID, "A token wallet for the DEX test token. Used for testing DEX software.", dex.Simnet)
registerToken(simnetTokenID, "A token wallet for the DEX test token. Used for testing DEX software.", dex.Simnet)
}

const (
Expand All @@ -72,7 +71,7 @@ const (
)

var (
testTokenID, _ = dex.BipSymbolID("dextt.eth")
simnetTokenID, _ = dex.BipSymbolID("dextt.eth")
// blockTicker is the delay between calls to check for new blocks.
blockTicker = time.Second
peerCountTicker = 5 * time.Second
Expand Down Expand Up @@ -1359,6 +1358,8 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
return fail("Swap: initiate error: %w", err)
}

contractAddr := dexeth.Tokens[w.assetID].NetTokens[w.net].SwapContracts[cfg.Version].Address.String()

txHash := tx.Hash()
for _, swap := range swaps.Contracts {
var secretHash [dexeth.SecretHashSize]byte
Expand All @@ -1369,7 +1370,7 @@ func (w *TokenWallet) Swap(swaps *asset.Swaps) ([]asset.Receipt, asset.Coin, uin
txHash: txHash,
secretHash: secretHash,
ver: cfg.Version,
contractAddr: erc20.ContractAddresses[cfg.Version][w.net].String(),
contractAddr: contractAddr,
})
}

Expand Down Expand Up @@ -2696,7 +2697,7 @@ func checkTxStatus(receipt *types.Receipt, gasLimit uint64) error {
// factor of 2. The account should already have a trading balance of at least
// maxSwaps gwei (token or eth), and sufficient eth balance to cover the
// requisite tx fees.
func GetGasEstimates(ctx context.Context, cl *nodeClient, c contractor, maxSwaps int, g *dexeth.Gases, waitForMined func()) error {
func GetGasEstimates(ctx context.Context, cl *nodeClient, c contractor, maxSwaps int, g *dexeth.Gases, waitForMined func(), waitForReceipt func(*nodeClient, *types.Transaction) (*types.Receipt, error)) error {
tokenContractor, isToken := c.(tokenContractor)

stats := struct {
Expand Down Expand Up @@ -2806,7 +2807,7 @@ func GetGasEstimates(ctx context.Context, cl *nodeClient, c contractor, maxSwaps
return fmt.Errorf("initiate error for %d swaps: %v", n, err)
}
waitForMined()
receipt, err := cl.transactionReceipt(ctx, tx.Hash())
receipt, err := waitForReceipt(cl, tx)
if err != nil {
return err
}
Expand All @@ -2816,15 +2817,15 @@ func GetGasEstimates(ctx context.Context, cl *nodeClient, c contractor, maxSwaps
stats.swaps = append(stats.swaps, receipt.GasUsed)

if isToken {
receipt, err = cl.transactionReceipt(ctx, approveTx.Hash())
receipt, err = waitForReceipt(cl, approveTx)
if err != nil {
return err
}
if err := checkTxStatus(receipt, txOpts.GasLimit); err != nil {
return err
}
stats.approves = append(stats.approves, receipt.GasUsed)
receipt, err = cl.transactionReceipt(ctx, transferTx.Hash())
receipt, err = waitForReceipt(cl, transferTx)
if err != nil {
return err
}
Expand Down Expand Up @@ -2859,7 +2860,7 @@ func GetGasEstimates(ctx context.Context, cl *nodeClient, c contractor, maxSwaps
return fmt.Errorf("redeem error for %d swaps: %v", n, err)
}
waitForMined()
receipt, err = cl.transactionReceipt(ctx, tx.Hash())
receipt, err = waitForReceipt(cl, tx)
if err != nil {
return err
}
Expand Down
42 changes: 21 additions & 21 deletions client/asset/eth/eth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var (
testAddressC = common.HexToAddress("2b84C791b79Ee37De042AD2ffF1A253c3ce9bc27")

ethGases = dexeth.VersionedGases[0]
tokenGases = dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts[0].Gas
tokenGases = dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts[0].Gas

tETH = &dex.Asset{
ID: 60,
Expand All @@ -66,7 +66,7 @@ var (
}

tToken = &dex.Asset{
ID: testTokenID,
ID: simnetTokenID,
Symbol: "dextt.eth",
Version: 0,
SwapSize: tokenGases.Swap,
Expand Down Expand Up @@ -573,8 +573,8 @@ func tassetWallet(assetID uint32) (asset.Wallet, *assetWallet, *testNode, contex
parent: node.tokenParent,
}
aw.wallets = map[uint32]*assetWallet{
testTokenID: aw,
BipID: node.tokenParent,
simnetTokenID: aw,
BipID: node.tokenParent,
}
}

Expand Down Expand Up @@ -668,7 +668,7 @@ func TestBalance(t *testing.T) {
for _, test := range tests {
var assetID uint32 = BipID
if test.token {
assetID = testTokenID
assetID = simnetTokenID
}

_, eth, node, shutdown := tassetWallet(assetID)
Expand Down Expand Up @@ -781,7 +781,7 @@ func TestFeeRate(t *testing.T) {

func TestRefund(t *testing.T) {
t.Run("eth", func(t *testing.T) { testRefund(t, BipID) })
t.Run("token", func(t *testing.T) { testRefund(t, testTokenID) })
t.Run("token", func(t *testing.T) { testRefund(t, simnetTokenID) })
}

func testRefund(t *testing.T, assetID uint32) {
Expand All @@ -804,7 +804,7 @@ func testRefund(t *testing.T, assetID uint32) {
dexeth.VersionedGases[1] = gasesV1
defer delete(dexeth.VersionedGases, 1)
} else {
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
tc := *tokenContracts[0]
tc.Gas = *gasesV1
tokenContracts[1] = &tc
Expand Down Expand Up @@ -964,7 +964,7 @@ func (b *badCoin) Value() uint64 {

func TestFundOrderReturnCoinsFundingCoins(t *testing.T) {
t.Run("eth", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, BipID) })
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, testTokenID) })
t.Run("token", func(t *testing.T) { testFundOrderReturnCoinsFundingCoins(t, simnetTokenID) })
}

func testFundOrderReturnCoinsFundingCoins(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -1380,7 +1380,7 @@ func TestPreSwap(t *testing.T) {
var assetID uint32 = BipID
assetCfg := tETH
if test.token {
assetID = testTokenID
assetID = simnetTokenID
assetCfg = tToken
}

Expand Down Expand Up @@ -1436,7 +1436,7 @@ func TestPreSwap(t *testing.T) {

func TestSwap(t *testing.T) {
t.Run("eth", func(t *testing.T) { testSwap(t, BipID) })
t.Run("token", func(t *testing.T) { testSwap(t, testTokenID) })
t.Run("token", func(t *testing.T) { testSwap(t, simnetTokenID) })
}

func testSwap(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -1715,7 +1715,7 @@ func TestPreRedeem(t *testing.T) {
}

// Token
w, _, node, shutdown2 := tassetWallet(testTokenID)
w, _, node, shutdown2 := tassetWallet(simnetTokenID)
defer shutdown2()

form.AssetConfig = tToken
Expand All @@ -1733,7 +1733,7 @@ func TestPreRedeem(t *testing.T) {

func TestRedeem(t *testing.T) {
t.Run("eth", func(t *testing.T) { testRedeem(t, BipID) })
t.Run("token", func(t *testing.T) { testRedeem(t, testTokenID) })
t.Run("token", func(t *testing.T) { testRedeem(t, simnetTokenID) })
}

func testRedeem(t *testing.T, assetID uint32) {
Expand All @@ -1743,7 +1743,7 @@ func testRedeem(t *testing.T, assetID uint32) {
// Test with a non-zero contract version to ensure it makes it into the receipt
contractVer := uint32(1)
dexeth.VersionedGases[1] = ethGases // for dexeth.RedeemGas(..., 1)
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
tokenContracts[1] = tokenContracts[0]
defer delete(dexeth.VersionedGases, 1)
defer delete(tokenContracts, 1)
Expand Down Expand Up @@ -2387,7 +2387,7 @@ func TestMaxOrder(t *testing.T) {
var assetID uint32 = BipID
dexAsset := tETH
if test.token {
assetID = testTokenID
assetID = simnetTokenID
}

w, _, node, shutdown := tassetWallet(assetID)
Expand Down Expand Up @@ -2464,7 +2464,7 @@ func packInitiateDataV0(initiations []*dexeth.Initiation) ([]byte, error) {

func TestAuditContract(t *testing.T) {
t.Run("eth", func(t *testing.T) { testAuditContract(t, BipID) })
t.Run("token", func(t *testing.T) { testAuditContract(t, testTokenID) })
t.Run("token", func(t *testing.T) { testAuditContract(t, simnetTokenID) })
}

func testAuditContract(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -3050,7 +3050,7 @@ func TestLocktimeExpired(t *testing.T) {

func TestFindRedemption(t *testing.T) {
t.Run("eth", func(t *testing.T) { testFindRedemption(t, BipID) })
t.Run("token", func(t *testing.T) { testFindRedemption(t, testTokenID) })
t.Run("token", func(t *testing.T) { testFindRedemption(t, simnetTokenID) })
}

func testFindRedemption(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -3192,7 +3192,7 @@ func testFindRedemption(t *testing.T, assetID uint32) {

func TestRefundReserves(t *testing.T) {
t.Run("eth", func(t *testing.T) { testRefundReserves(t, BipID) })
t.Run("token", func(t *testing.T) { testRefundReserves(t, testTokenID) })
t.Run("token", func(t *testing.T) { testRefundReserves(t, simnetTokenID) })
}

func testRefundReserves(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -3220,7 +3220,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {
feeWallet = node.tokenParent
assetV0 = *tToken
assetV1 = *tToken
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
gasesV0 = &tokenGases
tc := *tokenContracts[0]
tc.Gas = *gasesV1
Expand Down Expand Up @@ -3287,7 +3287,7 @@ func testRefundReserves(t *testing.T, assetID uint32) {

func TestRedemptionReserves(t *testing.T) {
t.Run("eth", func(t *testing.T) { testRedemptionReserves(t, BipID) })
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, testTokenID) })
t.Run("token", func(t *testing.T) { testRedemptionReserves(t, simnetTokenID) })
}

func testRedemptionReserves(t *testing.T, assetID uint32) {
Expand Down Expand Up @@ -3315,7 +3315,7 @@ func testRedemptionReserves(t *testing.T, assetID uint32) {
feeWallet = node.tokenParent
assetV0 = *tToken
assetV1 = *tToken
tokenContracts := dexeth.Tokens[testTokenID].NetTokens[dex.Simnet].SwapContracts
tokenContracts := dexeth.Tokens[simnetTokenID].NetTokens[dex.Simnet].SwapContracts
gasesV0 = &tokenGases
tc := *tokenContracts[0]
tc.Gas = *gasesV1
Expand Down Expand Up @@ -3383,7 +3383,7 @@ func ethToWei(v uint64) *big.Int {

func TestSend(t *testing.T) {
t.Run("eth", func(t *testing.T) { testSend(t, BipID) })
t.Run("token", func(t *testing.T) { testSend(t, testTokenID) })
t.Run("token", func(t *testing.T) { testSend(t, simnetTokenID) })
}

func testSend(t *testing.T, assetID uint32) {
Expand Down
Loading

0 comments on commit 9d7413e

Please sign in to comment.