Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eth/polygon: Add USDT #2747

Merged
merged 2 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions client/asset/eth/cmd/deploy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func mainErr() error {
flag.BoolVar(&useMainnet, "mainnet", false, "use mainnet")
flag.BoolVar(&useTestnet, "testnet", false, "use testnet")
flag.BoolVar(&useSimnet, "simnet", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "use simnet")
flag.BoolVar(&debug, "debug", false, "use simnet")
flag.BoolVar(&trace, "trace", false, "trace logs")
flag.BoolVar(&debug, "debug", false, "debug logs")
flag.StringVar(&chain, "chain", "eth", "symbol of the base chain")
flag.StringVar(&tokenAddress, "tokenaddr", "", "launches an erc20-linked contract with this token. default launches a base chain contract")
flag.IntVar(&contractVerI, "ver", 0, "contract version")
Expand Down
8 changes: 2 additions & 6 deletions client/asset/eth/eth.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func registerToken(tokenID uint32, desc string) {
func init() {
asset.Register(BipID, &Driver{})
registerToken(usdcTokenID, "The USDC Ethereum ERC20 token.")
registerToken(usdtTokenID, "The USDT Ethereum ERC20 token.")
}

const (
Expand Down Expand Up @@ -106,6 +107,7 @@ const (

var (
usdcTokenID, _ = dex.BipSymbolID("usdc.eth")
usdtTokenID, _ = dex.BipSymbolID("usdt.eth")
// blockTicker is the delay between calls to check for new blocks.
blockTicker = time.Second
peerCountTicker = 5 * time.Second
Expand Down Expand Up @@ -4938,12 +4940,6 @@ type providersFile struct {
Providers map[string] /* symbol */ map[string] /* network */ []string `json:"providers"`
}

// fileCredentials contain the seed and providers to use for GetGasEstimates.
type fileCredentials struct {
Seed dex.Bytes `json:"seed"`
Providers map[string]string `json:"providers"`
}

// getFileCredentials reads the file at path and extracts the seed and the
// provider for the network.
func getFileCredentials(chain, path string, net dex.Network) (seed []byte, providers []string, err error) {
Expand Down
2 changes: 2 additions & 0 deletions client/asset/polygon/polygon.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func registerToken(tokenID uint32, desc string, nets ...dex.Network) {
func init() {
asset.Register(BipID, &Driver{})
registerToken(usdcTokenID, "The USDC Ethereum ERC20 token.", dex.Mainnet, dex.Testnet, dex.Simnet)
registerToken(usdtTokenID, "The USDT Ethereum ERC20 token.", dex.Mainnet, dex.Testnet, dex.Simnet)
registerToken(wbtcTokenID, "Wrapped BTC.", dex.Mainnet)
registerToken(wethTokenID, "Wrapped ETH.", dex.Mainnet)
}
Expand All @@ -53,6 +54,7 @@ const (

var (
usdcTokenID, _ = dex.BipSymbolID("usdc.polygon")
usdtTokenID, _ = dex.BipSymbolID("usdt.polygon")
wethTokenID, _ = dex.BipSymbolID("weth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("wbtc.polygon")
// WalletInfo defines some general information about a Polygon Wallet(EVM
Expand Down
Binary file added client/webserver/site/src/img/coins/usdt.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions client/webserver/site/src/js/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@ const BipIDs: Record<number, string> = {
133: 'zec',
966: 'polygon',
60001: 'usdc.eth',
60002: 'usdt.eth',
966001: 'usdc.polygon',
966002: 'weth.polygon',
966003: 'wbtc.polygon',
966004: 'usdt.polygon',
147: 'zcl'
}

Expand Down
2 changes: 2 additions & 0 deletions dex/bip-id.go
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ var bipIDs = map[uint32]string{
49344: "stash",
// Ethereum reserved token range 60000-60999
60001: "usdc.eth",
60002: "usdt.eth",
// END Ethereum reserved token range
65536: "keth",
88888: "ryo[c0ban]",
Expand All @@ -619,6 +620,7 @@ var bipIDs = map[uint32]string{
966001: "usdc.polygon",
966002: "weth.polygon",
966003: "wbtc.polygon",
966004: "usdt.polygon",
// END Polygon reserved token range
1171337: "ilt",
1313114: "etho",
Expand Down
1 change: 1 addition & 0 deletions dex/networks/eth/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ type Redemption struct {
}

var usdcTokenID, _ = dex.BipSymbolID("usdc.eth")
var usdtTokenID, _ = dex.BipSymbolID("usdt.eth")

// Gases lists the expected gas required for various DEX and wallet operations.
type Gases struct {
Expand Down
106 changes: 105 additions & 1 deletion dex/networks/eth/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,20 +173,119 @@ var Tokens = map[uint32]*Token{
},
},
},
usdtTokenID: {
EVMFactor: new(int64),
Token: &dex.Token{
ParentID: EthBipID,
Name: "Tether",
UnitInfo: dex.UnitInfo{
AtomicUnit: "microUSD",
Conventional: dex.Denomination{
Unit: "USDT",
ConversionFactor: 1e6,
},
},
},
NetTokens: map[dex.Network]*NetToken{
dex.Mainnet: {
Address: common.HexToAddress("0xdac17f958d2ee523a2206206994597c13d831ec7"), // https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7
SwapContracts: map[uint32]*SwapContract{
0: {
// swap contract: https://etherscan.io/address/0x97a53fEF7854f4CB846F2eaCCf847229F1E10e4f
Address: common.HexToAddress("0x97a53fEF7854f4CB846F2eaCCf847229F1E10e4f"),
// USDT's contract is upgradable, using a proxy call, so
// gas cost could change without notice, so we do not
// want to set limits too low, even with live estimates.
martonp marked this conversation as resolved.
Show resolved Hide resolved
Gas: Gases{
// Results from client's GetGasEstimates.
//
// First swap used 181405 gas Recommended Gases.Swap = 235826
// 1 additional swaps averaged 112591 gas each. Recommended Gases.SwapAdd = 146368
// [181405 293996]
// First redeem used 76487 gas. Recommended Gases.Redeem = 99433
// 1 additional redeems averaged 31617 gas each. recommended Gases.RedeemAdd = 41102
// [76487 108104]
// Average of 2 refunds: 61963. Recommended Gases.Refund = 80551
// [61684 62242]
// Average of 2 approvals: 48897. Recommended Gases.Approve = 63566
// [48897 48897]
// Average of 1 transfers: 63173. Recommended Gases.Transfer = 82124
// [63173]
Swap: 235_826,
SwapAdd: 146_368,
Redeem: 99_433,
RedeemAdd: 41_102,
Refund: 80_551,
Approve: 63_566,
Transfer: 82_124,
},
},
},
},
dex.Testnet: {
Address: common.HexToAddress("0x7169D38820dfd117C3FA1f22a697dBA58d90BA06"),
SwapContracts: map[uint32]*SwapContract{
0: {
Address: common.HexToAddress("0x97a53fEF7854f4CB846F2eaCCf847229F1E10e4f"),
Gas: Gases{
// Results from client's GetGasEstimates.
//
// First swap used 181441 gas Recommended Gases.Swap = 235873
// 4 additional swaps averaged 112591 gas each. Recommended Gases.SwapAdd = 146368
// [181441 294032 406623 519202 631805]
// First redeem used 76530 gas. Recommended Gases.Redeem = 99489
// 4 additional redeems averaged 31626 gas each. recommended Gases.RedeemAdd = 41113
// [76530 108159 139800 171418 203035]
// Average of 5 refunds: 62183. Recommended Gases.Refund = 80837
// [61739 62297 62297 62285 62297]
// Average of 2 approvals: 48930. Recommended Gases.Approve = 63609
// [48930 48930]
// Average of 1 transfers: 63228. Recommended Gases.Transfer = 82196
// [63228]
Swap: 235_873,
SwapAdd: 146_368,
Redeem: 99_489,
RedeemAdd: 41_113,
Refund: 80_837,
Approve: 63_609,
Transfer: 82_196,
},
},
},
},
dex.Simnet: {
Address: common.Address{},
SwapContracts: map[uint32]*SwapContract{
0: {
Address: common.Address{},
Gas: Gases{
Swap: 242_000,
SwapAdd: 146_400,
Redeem: 109_000,
RedeemAdd: 31_600,
Refund: 77_000,
Approve: 78_400,
Transfer: 85_100,
}},
},
},
},
},
}

// MaybeReadSimnetAddrs attempts to read the info files generated by the eth
// simnet harness to populate swap contract and token addresses in
// ContractAddresses and Tokens.
func MaybeReadSimnetAddrs() {
MaybeReadSimnetAddrsDir("eth", ContractAddresses, MultiBalanceAddresses, Tokens[usdcTokenID].NetTokens[dex.Simnet])
MaybeReadSimnetAddrsDir("eth", ContractAddresses, MultiBalanceAddresses, Tokens[usdcTokenID].NetTokens[dex.Simnet], Tokens[usdtTokenID].NetTokens[dex.Simnet])
}

func MaybeReadSimnetAddrsDir(
dir string,
contractsAddrs map[uint32]map[dex.Network]common.Address,
multiBalandAddresses map[dex.Network]common.Address,
usdcToken *NetToken,
usdtToken *NetToken,
) {

usr, err := user.Current()
Expand All @@ -206,13 +305,18 @@ func MaybeReadSimnetAddrsDir(
ethSwapContractAddrFile := filepath.Join(harnessDir, "eth_swap_contract_address.txt")
testUSDCSwapContractAddrFile := filepath.Join(harnessDir, "usdc_swap_contract_address.txt")
testUSDCContractAddrFile := filepath.Join(harnessDir, "test_usdc_contract_address.txt")
testUSDTSwapContractAddrFile := filepath.Join(harnessDir, "usdt_swap_contract_address.txt")
testUSDTContractAddrFile := filepath.Join(harnessDir, "test_usdt_contract_address.txt")
multiBalanceContractAddrFile := filepath.Join(harnessDir, "multibalance_address.txt")

contractsAddrs[0][dex.Simnet] = maybeGetContractAddrFromFile(ethSwapContractAddrFile)
multiBalandAddresses[dex.Simnet] = maybeGetContractAddrFromFile(multiBalanceContractAddrFile)

usdcToken.SwapContracts[0].Address = maybeGetContractAddrFromFile(testUSDCSwapContractAddrFile)
usdcToken.Address = maybeGetContractAddrFromFile(testUSDCContractAddrFile)

usdtToken.SwapContracts[0].Address = maybeGetContractAddrFromFile(testUSDTSwapContractAddrFile)
usdtToken.Address = maybeGetContractAddrFromFile(testUSDTContractAddrFile)
}

func maybeGetContractAddrFromFile(fileName string) (addr common.Address) {
Expand Down
70 changes: 69 additions & 1 deletion dex/networks/polygon/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,13 @@ var (
}

usdcTokenID, _ = dex.BipSymbolID("usdc.polygon")
usdtTokenID, _ = dex.BipSymbolID("usdt.polygon")
wethTokenID, _ = dex.BipSymbolID("weth.polygon")
wbtcTokenID, _ = dex.BipSymbolID("wbtc.polygon")

Tokens = map[uint32]*dexeth.Token{
usdcTokenID: TokenUSDC,
usdtTokenID: TokenUSDT,
wethTokenID: TokenWETH,
wbtcTokenID: TokenWBTC,
}
Expand Down Expand Up @@ -161,6 +163,72 @@ var (
},
}

TokenUSDT = &dexeth.Token{
EVMFactor: new(int64),
Token: &dex.Token{
ParentID: PolygonBipID,
Name: "Tether",
UnitInfo: dex.UnitInfo{
AtomicUnit: "microUSD",
Conventional: dex.Denomination{
Unit: "USDT",
ConversionFactor: 1e6,
},
},
},
NetTokens: map[dex.Network]*dexeth.NetToken{
dex.Mainnet: {
Address: common.HexToAddress("0xc2132D05D31c914a87C6611C10748AEb04B58e8F"), // https://polygonscan.com/address/0xc2132D05D31c914a87C6611C10748AEb04B58e8F
SwapContracts: map[uint32]*dexeth.SwapContract{
0: {
// swap contract: https://polygonscan.com/address/0x97a53fEF7854f4CB846F2eaCCf847229F1E10e4f
Address: common.HexToAddress("0x97a53fEF7854f4CB846F2eaCCf847229F1E10e4f"),
// Results from client's GetGasEstimates.
//
// First swap used 181278 gas Recommended Gases.Swap = 235661
// 4 additional swaps averaged 112591 gas each. Recommended Gases.SwapAdd = 146368
// [181278 293869 406448 519051 631642]
// First redeem used 70794 gas. Recommended Gases.Redeem = 92032
// 4 additional redeems averaged 31629 gas each. recommended Gases.RedeemAdd = 41117
// [70794 102411 134028 165670 197311]
// Average of 5 refunds: 63123. Recommended Gases.Refund = 82059
// [63126 63126 63114 63126 63126]
// Average of 2 approvals: 52072. Recommended Gases.Approve = 67693
// [52072 52072]
// Average of 1 transfers: 57270. Recommended Gases.Transfer = 74451
// [57270]
Gas: dexeth.Gases{
Swap: 235_661,
SwapAdd: 146_368,
Redeem: 92_032,
RedeemAdd: 41_117,
Refund: 82_059,
Approve: 67_693,
Transfer: 74_451,
},
},
},
},
dex.Simnet: {
Address: common.Address{},
SwapContracts: map[uint32]*dexeth.SwapContract{
0: {
Address: common.Address{}, // Set in MaybeReadSimnetAddrs
Gas: dexeth.Gases{
Swap: 223_163,
SwapAdd: 146_399,
Redeem: 82_121,
RedeemAdd: 41_113,
Refund: 62_527,
Approve: 58_180,
Transfer: 64_539,
},
},
},
},
},
}

// TokenWETH is for Wrapped ETH.
TokenWETH = &dexeth.Token{
Token: &dex.Token{
Expand Down Expand Up @@ -267,5 +335,5 @@ var (
// simnet harness to populate swap contract and token addresses in
// ContractAddresses and Tokens.
func MaybeReadSimnetAddrs() {
dexeth.MaybeReadSimnetAddrsDir("polygon", ContractAddresses, MultiBalanceAddresses, Tokens[usdcTokenID].NetTokens[dex.Simnet])
dexeth.MaybeReadSimnetAddrsDir("polygon", ContractAddresses, MultiBalanceAddresses, Tokens[usdcTokenID].NetTokens[dex.Simnet], Tokens[usdtTokenID].NetTokens[dex.Simnet])
}
39 changes: 39 additions & 0 deletions dex/testing/dcrdex/genmarkets.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,24 @@ if [ $ETH_ON -eq 0 ]; then
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2,
"parcelSize": 4
},
{
"base": "BTC_simnet",
"quote": "USDT_simnet",
"lotSize": 1000000,
"rateStep": 10000,
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2,
"parcelSize": 4
},
{
"base": "USDC_simnet",
"quote": "USDT_simnet",
"lotSize": 10000000,
"rateStep": 10000,
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2,
"parcelSize": 4
EOF
else echo "Ethereum is not running. Configuring dcrdex markets without ETH."
fi
Expand All @@ -172,6 +190,15 @@ if [ $POLYGON_ON -eq 0 ]; then
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2,
"parcelSize": 4
},
{
"base": "DCR_simnet",
"quote": "USDT_POLYGON_simnet",
"lotSize": 1000000,
"rateStep": 10000,
"epochDuration": ${EPOCH_DURATION},
"marketBuyBuffer": 1.2,
"parcelSize": 4
EOF
else echo "Polygon is not running. Configuring dcrdex markets without Polygon."
fi
Expand Down Expand Up @@ -350,6 +377,12 @@ cat << EOF >> "${FILEPATH}"
"network": "simnet",
"maxFeeRate": 200,
"swapConf": 2
},
"USDT_simnet": {
"bip44symbol": "usdt.eth",
"network": "simnet",
"maxFeeRate": 200,
"swapConf": 2
EOF
fi # end if ETH_ON

Expand Down Expand Up @@ -378,6 +411,12 @@ cat << EOF >> "${FILEPATH}"
"network": "simnet",
"maxFeeRate": 200,
"swapConf": 2
},
"USDT_POLYGON_simnet": {
"bip44symbol": "usdt.polygon",
"network": "simnet",
"maxFeeRate": 200,
"swapConf": 2
EOF
fi # end if POLYGON_ON

Expand Down