Skip to content

Commit ef6129b

Browse files
authored
Merge pull request #82 from liquality/get-send-gas-limit-for-erc20
feat: getSendGasLimitERC20
2 parents 9f6e332 + 37427c5 commit ef6129b

File tree

6 files changed

+87
-31
lines changed

6 files changed

+87
-31
lines changed

src/assets/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { nativeAssets, testnetNativeAssets } from './native'
22
import { erc20Assets, testnetErc20Assets, chainToTokenAddressMap, chainToTestnetTokenAddressMap } from './erc20'
3+
import { getSendGasLimitERC20 } from './sendGasLimits'
34

45
const assets = {
56
...nativeAssets,
@@ -11,4 +12,4 @@ const testnetAssets = {
1112
...testnetErc20Assets
1213
}
1314

14-
export { assets, testnetAssets, chainToTokenAddressMap, chainToTestnetTokenAddressMap }
15+
export { assets, testnetAssets, chainToTokenAddressMap, chainToTestnetTokenAddressMap, getSendGasLimitERC20 }

src/assets/native.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { TESTNET_NATIVE } from './testnet'
2-
import { AssetMap, ChainId } from '../types'
3-
import { sendGasLimits } from '../assets/sendGasLimits'
2+
import { AssetMap, ChainId, AssetTypes } from '../types'
3+
import { sendGasLimits } from './sendGasLimits'
44

55
const nativeAssets: AssetMap = {
66
BTC: {
77
name: 'Bitcoin',
88
chain: ChainId.Bitcoin,
9-
type: 'native',
9+
type: AssetTypes.native,
1010
code: 'BTC',
1111
coinGeckoId: 'bitcoin',
1212
color: '#f7931a',
@@ -16,7 +16,7 @@ const nativeAssets: AssetMap = {
1616
BCH: {
1717
name: 'Bitcoin Cash',
1818
chain: ChainId.BitcoinCash,
19-
type: 'native',
19+
type: AssetTypes.native,
2020
code: 'BCH',
2121
coinGeckoId: 'bitcoin-cash',
2222
color: '#a1db5e',
@@ -26,7 +26,7 @@ const nativeAssets: AssetMap = {
2626
ETH: {
2727
name: 'Ether',
2828
chain: ChainId.Ethereum,
29-
type: 'native',
29+
type: AssetTypes.native,
3030
code: 'ETH',
3131
coinGeckoId: 'ethereum',
3232
color: '#627eea',
@@ -36,7 +36,7 @@ const nativeAssets: AssetMap = {
3636
RBTC: {
3737
name: 'Rootstock BTC',
3838
chain: ChainId.Rootstock,
39-
type: 'native',
39+
type: AssetTypes.native,
4040
code: 'RBTC',
4141
coinGeckoId: 'rootstock',
4242
color: '#006e3c',
@@ -46,7 +46,7 @@ const nativeAssets: AssetMap = {
4646
BNB: {
4747
name: 'Binance Coin',
4848
chain: ChainId.BinanceSmartChain,
49-
type: 'native',
49+
type: AssetTypes.native,
5050
code: 'BNB',
5151
coinGeckoId: 'binancecoin',
5252
color: '#f9a825',
@@ -56,7 +56,7 @@ const nativeAssets: AssetMap = {
5656
NEAR: {
5757
name: 'Near',
5858
chain: ChainId.Near,
59-
type: 'native',
59+
type: AssetTypes.native,
6060
code: 'NEAR',
6161
coinGeckoId: 'near',
6262
color: '#000000',
@@ -66,7 +66,7 @@ const nativeAssets: AssetMap = {
6666
SOL: {
6767
name: 'Solana',
6868
chain: ChainId.Solana,
69-
type: 'native',
69+
type: AssetTypes.native,
7070
code: 'SOL',
7171
coinGeckoId: 'solana',
7272
color: '#008080',
@@ -76,7 +76,7 @@ const nativeAssets: AssetMap = {
7676
MATIC: {
7777
name: 'Matic',
7878
chain: ChainId.Polygon,
79-
type: 'native',
79+
type: AssetTypes.native,
8080
code: 'MATIC',
8181
coinGeckoId: 'matic-network',
8282
color: '#8247E5',
@@ -86,7 +86,7 @@ const nativeAssets: AssetMap = {
8686
ARBETH: {
8787
name: 'Arbitrum ETH',
8888
chain: ChainId.Arbitrum,
89-
type: 'native',
89+
type: AssetTypes.native,
9090
code: 'ARBETH',
9191
coinGeckoId: 'ethereum',
9292
color: '#28A0EF',
@@ -97,7 +97,7 @@ const nativeAssets: AssetMap = {
9797
FUSE: {
9898
name: 'Fuse Network',
9999
chain: ChainId.Fuse,
100-
type: 'native',
100+
type: AssetTypes.native,
101101
code: 'FUSE',
102102
coinGeckoId: 'fuse-network-token',
103103
color: '#46e8b6',
@@ -107,7 +107,7 @@ const nativeAssets: AssetMap = {
107107
LUNA: {
108108
name: 'Luna',
109109
chain: ChainId.Terra,
110-
type: 'native',
110+
type: AssetTypes.native,
111111
code: 'LUNA',
112112
coinGeckoId: 'terra-luna',
113113
color: '#008080',
@@ -117,7 +117,7 @@ const nativeAssets: AssetMap = {
117117
UST: {
118118
name: 'TerraUSD',
119119
chain: ChainId.Terra,
120-
type: 'native',
120+
type: AssetTypes.native,
121121
code: 'UST',
122122
decimals: 6,
123123
color: '#0083ff',
@@ -128,7 +128,7 @@ const nativeAssets: AssetMap = {
128128
AVAX: {
129129
name: 'Avalanche',
130130
chain: ChainId.Avalanche,
131-
type: 'native',
131+
type: AssetTypes.native,
132132
code: 'AVAX',
133133
coinGeckoId: 'avalanche-2',
134134
color: '#E84141',

src/assets/sendGasLimits.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,30 @@
1+
import { ChainId } from '../types'
2+
import { hasTokens } from '../chains'
3+
14
const sendGasLimits = {
25
BTC: 290,
36
NATIVE_EVM: 21000, // EVM -> ETH, RBTC, MATIC, BNB, AVAX, FUSE
47
ERC20_EVM: 90000, // EVM -> ETH, RBTC, MATIC, BNB, AVAX, FUSE
58
TERRA: 100000, // applies on both native and ERC2 Terra assets
6-
ARBETH: 620000,
9+
ARBETH: 620000, // for native asset is around ~420k and for ERC20 ~540k
710
NEAR: 10000000000000,
811
SOL: 1000000
912
}
1013

11-
export { sendGasLimits }
14+
const getSendGasLimitERC20 = (chainId: ChainId): number | null => {
15+
if (!hasTokens(chainId)) {
16+
throw new Error(`Chain '${chainId}' doesn't support tokens!`)
17+
}
18+
19+
switch (chainId) {
20+
case ChainId.Arbitrum:
21+
return sendGasLimits.ARBETH
22+
case ChainId.Terra:
23+
return sendGasLimits.TERRA
24+
default:
25+
// EVM standard gas limit
26+
return sendGasLimits.ERC20_EVM
27+
}
28+
}
29+
30+
export { sendGasLimits, getSendGasLimitERC20 }

src/chains.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const chains: { [key in ChainId]: Chain } = {
2828
safeConfirmations: 1,
2929
// 0,1 blocks per minute * 180 minutes (3 hours) -> 18 blocks wait period
3030
txFailureTimeout: 10800000, // 3 hours in ms
31+
evmCompatible: false,
32+
hasTokens: false,
3133
// TODO: include network types in validation
3234
isValidAddress: (address) => !!validateBitcoinAddress(address),
3335
formatAddress: (address) => address,
@@ -44,6 +46,8 @@ const chains: { [key in ChainId]: Chain } = {
4446
safeConfirmations: 1,
4547
// ~0,1 blocks per minute * 180 minutes (3 hours) -> 18 blocks wait period
4648
txFailureTimeout: 10800000, // 3 hours in ms
49+
evmCompatible: false,
50+
hasTokens: false,
4751
// TODO: include network types in validation
4852
isValidAddress: (address) => isValidBitcoinCashAddress(address),
4953
formatAddress: (address) => formatBitcoinCashAddress(address),
@@ -60,6 +64,8 @@ const chains: { [key in ChainId]: Chain } = {
6064
safeConfirmations: 3,
6165
// ~4 blocks per minute * 30 minutes -> 120 blocks wait period
6266
txFailureTimeout: 1800000, // in ms
67+
evmCompatible: true,
68+
hasTokens: true,
6369
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
6470
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
6571
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -75,6 +81,8 @@ const chains: { [key in ChainId]: Chain } = {
7581
safeConfirmations: 5,
7682
// ~3 blocks per minute * 30 minutes -> 90 blocks wait period
7783
txFailureTimeout: 1800000, // in ms
84+
evmCompatible: true,
85+
hasTokens: true,
7886
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
7987
formatAddress: (hexAddress: string, network?: string) =>
8088
toChecksumAddress(with0x(hexAddress), getRSKChainID(network)),
@@ -91,6 +99,8 @@ const chains: { [key in ChainId]: Chain } = {
9199
safeConfirmations: 5,
92100
// ~20 blocks per minute * 10 minutes -> 200 blocks wait period
93101
txFailureTimeout: 600000, // in ms
102+
evmCompatible: true,
103+
hasTokens: true,
94104
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
95105
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
96106
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -106,6 +116,8 @@ const chains: { [key in ChainId]: Chain } = {
106116
safeConfirmations: 10,
107117
// ~50 blocks per minute * 5 minutes -> 250 blocks wait period
108118
txFailureTimeout: 300000, // in ms
119+
evmCompatible: false,
120+
hasTokens: false,
109121
isValidAddress: (address) => isValidNearAddress(address),
110122
formatAddress: (address) => address,
111123
isValidTransactionHash: (hash: string) => isValidNearTx(hash),
@@ -121,6 +133,8 @@ const chains: { [key in ChainId]: Chain } = {
121133
safeConfirmations: 31,
122134
// ~120 blocks per minute * 5 minutes -> 600 blocks wait period
123135
txFailureTimeout: 300000, // in ms
136+
evmCompatible: false,
137+
hasTokens: false,
124138
isValidAddress: (address) => isValidSolanaAddress(address),
125139
formatAddress: (address) => address,
126140
isValidTransactionHash: (hash: string) => isValidSolanaTx(hash),
@@ -136,6 +150,8 @@ const chains: { [key in ChainId]: Chain } = {
136150
safeConfirmations: 1,
137151
// ~10 blocks per minute * 15 minutes -> 150 blocks wait period
138152
txFailureTimeout: 900000, // in ms
153+
evmCompatible: false,
154+
hasTokens: true,
139155
isValidAddress: (address) => isValidTerraAddress(address),
140156
formatAddress: (address) => address,
141157
isValidTransactionHash: (hash: string) => isValidTerraTx(hash),
@@ -151,6 +167,8 @@ const chains: { [key in ChainId]: Chain } = {
151167
safeConfirmations: 5,
152168
// ~30 blocks per minute * 10 minutes -> 300 blocks wait period
153169
txFailureTimeout: 600000, // in ms
170+
evmCompatible: true,
171+
hasTokens: true,
154172
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
155173
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
156174
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -166,6 +184,8 @@ const chains: { [key in ChainId]: Chain } = {
166184
safeConfirmations: 5,
167185
// ~15 blocks per minute * 10 minutes -> 150 blocks wait period
168186
txFailureTimeout: 600000, // in ms
187+
evmCompatible: true,
188+
hasTokens: true,
169189
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
170190
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
171191
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -181,6 +201,8 @@ const chains: { [key in ChainId]: Chain } = {
181201
safeConfirmations: 5,
182202
// ~12 blocks per minute * 15 minutes -> 180 blocks wait period
183203
txFailureTimeout: 900000, // in ms
204+
evmCompatible: true,
205+
hasTokens: true,
184206
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
185207
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
186208
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -196,6 +218,8 @@ const chains: { [key in ChainId]: Chain } = {
196218
safeConfirmations: 5,
197219
// ~15 blocks per minute * 10 minutes -> 150 blocks wait period
198220
txFailureTimeout: 600000, // in ms
221+
evmCompatible: true,
222+
hasTokens: true,
199223
isValidAddress: (hexAddress: string) => isValidAddress(with0x(hexAddress)),
200224
formatAddress: (hexAddress: string) => toChecksumAddress(with0x(hexAddress)),
201225
isValidTransactionHash: (hash: string) => isValidHex(hash),
@@ -204,15 +228,11 @@ const chains: { [key in ChainId]: Chain } = {
204228
}
205229

206230
function isEthereumChain(chain: ChainId) {
207-
return [
208-
ChainId.BinanceSmartChain,
209-
ChainId.Ethereum,
210-
ChainId.Rootstock,
211-
ChainId.Polygon,
212-
ChainId.Avalanche,
213-
ChainId.Arbitrum,
214-
ChainId.Fuse
215-
].includes(chain)
231+
return chains[chain].evmCompatible
216232
}
217233

218-
export { chains, isEthereumChain }
234+
function hasTokens(chain: ChainId) {
235+
return chains[chain].hasTokens
236+
}
237+
238+
export { chains, isEthereumChain, hasTokens }

src/index.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import BigNumber from 'bignumber.js'
2-
import { assets, testnetAssets, chainToTokenAddressMap, chainToTestnetTokenAddressMap } from './assets'
2+
import {
3+
assets,
4+
testnetAssets,
5+
chainToTokenAddressMap,
6+
chainToTestnetTokenAddressMap,
7+
getSendGasLimitERC20
8+
} from './assets'
39
import { chains, isEthereumChain } from './chains'
410
import { dappChains } from './dapps'
5-
import { Asset, ChainId } from './types'
11+
import { Asset, AssetType, AssetTypes, ChainId } from './types'
612

713
function unitToCurrency(asset: Asset, value: number | BigNumber): BigNumber {
814
const multiplier = new BigNumber(10).pow(asset.decimals)
@@ -19,11 +25,14 @@ export {
1925
chainToTokenAddressMap,
2026
testnetAssets,
2127
chainToTestnetTokenAddressMap,
28+
getSendGasLimitERC20,
2229
chains,
2330
dappChains,
2431
isEthereumChain,
2532
unitToCurrency,
2633
currencyToUnit,
2734
Asset,
35+
AssetType,
36+
AssetTypes,
2837
ChainId
2938
}

src/types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,20 @@ export interface Chain {
77
}
88
safeConfirmations: number
99
txFailureTimeout: number
10+
evmCompatible: boolean
11+
hasTokens: boolean
1012
isValidAddress: (address: string, network?: string) => boolean
1113
formatAddress: (address: string, network?: string) => string
1214
isValidTransactionHash: (hash: string) => boolean
1315
formatTransactionHash: (hash: string) => string
1416
}
1517

16-
export type AssetType = 'native' | 'erc20'
18+
export enum AssetTypes {
19+
native = 'native',
20+
erc20 = 'erc20'
21+
}
22+
23+
export type AssetType = AssetTypes.native | AssetTypes.erc20
1724

1825
export enum ChainId {
1926
Bitcoin = 'bitcoin',

0 commit comments

Comments
 (0)