@toruslabs/ethereum-controllers 4.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/dist/ethereumControllers.cjs.js +6153 -0
  2. package/dist/ethereumControllers.cjs.js.map +1 -0
  3. package/dist/ethereumControllers.esm.js +5570 -0
  4. package/dist/ethereumControllers.esm.js.map +1 -0
  5. package/dist/ethereumControllers.umd.min.js +3 -0
  6. package/dist/ethereumControllers.umd.min.js.LICENSE.txt +38 -0
  7. package/dist/ethereumControllers.umd.min.js.map +1 -0
  8. package/dist/types/Account/AccountTrackerController.d.ts +35 -0
  9. package/dist/types/Block/PollingBlockTracker.d.ts +14 -0
  10. package/dist/types/Currency/CurrencyController.d.ts +30 -0
  11. package/dist/types/Gas/GasFeeController.d.ts +64 -0
  12. package/dist/types/Gas/IGasFeeController.d.ts +49 -0
  13. package/dist/types/Gas/gasUtil.d.ts +21 -0
  14. package/dist/types/Keyring/KeyringController.d.ts +20 -0
  15. package/dist/types/Message/AbstractMessageController.d.ts +36 -0
  16. package/dist/types/Message/DecryptMessageController.d.ts +20 -0
  17. package/dist/types/Message/EncryptionPublicKeyController.d.ts +20 -0
  18. package/dist/types/Message/MessageController.d.ts +20 -0
  19. package/dist/types/Message/PersonalMessageController.d.ts +20 -0
  20. package/dist/types/Message/TypedMessageController.d.ts +21 -0
  21. package/dist/types/Message/utils.d.ts +10 -0
  22. package/dist/types/Network/NetworkController.d.ts +40 -0
  23. package/dist/types/Network/createEthereumMiddleware.d.ts +66 -0
  24. package/dist/types/Network/createJsonRpcClient.d.ts +9 -0
  25. package/dist/types/Nfts/INftsController.d.ts +10 -0
  26. package/dist/types/Nfts/NftHandler.d.ts +35 -0
  27. package/dist/types/Nfts/NftsController.d.ts +40 -0
  28. package/dist/types/Preferences/PreferencesController.d.ts +53 -0
  29. package/dist/types/Tokens/ITokensController.d.ts +10 -0
  30. package/dist/types/Tokens/TokenHandler.d.ts +20 -0
  31. package/dist/types/Tokens/TokenRatesController.d.ts +42 -0
  32. package/dist/types/Tokens/TokensController.d.ts +42 -0
  33. package/dist/types/Transaction/NonceTracker.d.ts +37 -0
  34. package/dist/types/Transaction/PendingTransactionTracker.d.ts +32 -0
  35. package/dist/types/Transaction/TransactionController.d.ts +67 -0
  36. package/dist/types/Transaction/TransactionGasUtil.d.ts +21 -0
  37. package/dist/types/Transaction/TransactionStateHistoryHelper.d.ts +16 -0
  38. package/dist/types/Transaction/TransactionStateManager.d.ts +30 -0
  39. package/dist/types/Transaction/TransactionUtils.d.ts +70 -0
  40. package/dist/types/index.d.ts +43 -0
  41. package/dist/types/utils/abiDecoder.d.ts +17 -0
  42. package/dist/types/utils/abis.d.ts +84 -0
  43. package/dist/types/utils/constants.d.ts +81 -0
  44. package/dist/types/utils/contractAddresses.d.ts +1 -0
  45. package/dist/types/utils/conversionUtils.d.ts +42 -0
  46. package/dist/types/utils/helpers.d.ts +24 -0
  47. package/dist/types/utils/interfaces.d.ts +384 -0
  48. package/package.json +71 -0
  49. package/src/Account/AccountTrackerController.ts +157 -0
  50. package/src/Block/PollingBlockTracker.ts +89 -0
  51. package/src/Currency/CurrencyController.ts +117 -0
  52. package/src/Gas/GasFeeController.ts +254 -0
  53. package/src/Gas/IGasFeeController.ts +56 -0
  54. package/src/Gas/gasUtil.ts +163 -0
  55. package/src/Keyring/KeyringController.ts +118 -0
  56. package/src/Message/AbstractMessageController.ts +136 -0
  57. package/src/Message/DecryptMessageController.ts +81 -0
  58. package/src/Message/EncryptionPublicKeyController.ts +83 -0
  59. package/src/Message/MessageController.ts +74 -0
  60. package/src/Message/PersonalMessageController.ts +74 -0
  61. package/src/Message/TypedMessageController.ts +112 -0
  62. package/src/Message/utils.ts +107 -0
  63. package/src/Network/NetworkController.ts +184 -0
  64. package/src/Network/createEthereumMiddleware.ts +307 -0
  65. package/src/Network/createJsonRpcClient.ts +59 -0
  66. package/src/Nfts/INftsController.ts +13 -0
  67. package/src/Nfts/NftHandler.ts +191 -0
  68. package/src/Nfts/NftsController.ts +230 -0
  69. package/src/Preferences/PreferencesController.ts +409 -0
  70. package/src/Tokens/ITokensController.ts +13 -0
  71. package/src/Tokens/TokenHandler.ts +60 -0
  72. package/src/Tokens/TokenRatesController.ts +134 -0
  73. package/src/Tokens/TokensController.ts +278 -0
  74. package/src/Transaction/NonceTracker.ts +152 -0
  75. package/src/Transaction/PendingTransactionTracker.ts +235 -0
  76. package/src/Transaction/TransactionController.ts +558 -0
  77. package/src/Transaction/TransactionGasUtil.ts +74 -0
  78. package/src/Transaction/TransactionStateHistoryHelper.ts +41 -0
  79. package/src/Transaction/TransactionStateManager.ts +315 -0
  80. package/src/Transaction/TransactionUtils.ts +333 -0
  81. package/src/index.ts +45 -0
  82. package/src/utils/abiDecoder.ts +195 -0
  83. package/src/utils/abis.ts +677 -0
  84. package/src/utils/constants.ts +379 -0
  85. package/src/utils/contractAddresses.ts +21 -0
  86. package/src/utils/conversionUtils.ts +269 -0
  87. package/src/utils/helpers.ts +177 -0
  88. package/src/utils/interfaces.ts +454 -0
@@ -0,0 +1,379 @@
1
+ import { EthereumProviderConfig } from "./interfaces";
2
+
3
+ export const LOCALHOST = "localhost";
4
+
5
+ export const CONTRACT_TYPE_ETH = "eth";
6
+ export const CONTRACT_TYPE_ERC20 = "erc20";
7
+ export const CONTRACT_TYPE_ERC721 = "erc721";
8
+ export const CONTRACT_TYPE_ERC1155 = "erc1155";
9
+
10
+ export const ERC1155_INTERFACE_ID = "0xd9b67a26";
11
+ export const ERC721_INTERFACE_ID = "0x80ac58cd";
12
+ export const ERC721_METADATA_INTERFACE_ID = "0x5b5e139f";
13
+ export const ERC721_ENUMERABLE_INTERFACE_ID = "0x780e9d63";
14
+
15
+ export const MAINNET_CHAIN_ID = "0x1";
16
+ export const POLYGON_CHAIN_ID = "0x89";
17
+ export const BSC_MAINNET_CHAIN_ID = "0x38";
18
+ export const AVALANCHE_MAINNET_CHAIN_ID = "0xa86a";
19
+ export const XDAI_CHAIN_ID = "0x64";
20
+ export const ARBITRUM_MAINNET_CHAIN_ID = "0xa4b1";
21
+ export const OPTIMISM_MAINNET_CHAIN_ID = "0xa";
22
+ export const CELO_MAINNET_CHAIN_ID = "0xa4ec";
23
+
24
+ export const GOERLI_CHAIN_ID = "0x5";
25
+ export const SEPOLIA_CHAIN_ID = "0xaa36a7";
26
+ export const POLYGON_MUMBAI_CHAIN_ID = "0x13881";
27
+ export const BSC_TESTNET_CHAIN_ID = "0x61";
28
+ export const AVALANCHE_TESTNET_CHAIN_ID = "0xa869";
29
+ export const ARBITRUM_TESTNET_CHAIN_ID = "0x66eeb";
30
+ export const OPTIMISM_TESTNET_CHAIN_ID = "0x1a4";
31
+
32
+ export const SUPPORTED_NETWORKS: Record<string, EthereumProviderConfig> = {
33
+ [MAINNET_CHAIN_ID]: {
34
+ blockExplorerUrl: "https://etherscan.io",
35
+ chainId: MAINNET_CHAIN_ID,
36
+ displayName: "Main Ethereum Network",
37
+ logo: "eth.svg",
38
+ rpcTarget: `https://mainnet.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
39
+ ticker: "ETH",
40
+ tickerName: "Ethereum",
41
+ },
42
+ [POLYGON_CHAIN_ID]: {
43
+ blockExplorerUrl: "https://polygonscan.com",
44
+ chainId: POLYGON_CHAIN_ID,
45
+ displayName: "Polygon Mainnet",
46
+ logo: "matic-network-logo.svg",
47
+ rpcTarget: `https://polygon-mainnet.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
48
+ ticker: "MATIC",
49
+ tickerName: "Matic Network Token",
50
+ },
51
+ [BSC_MAINNET_CHAIN_ID]: {
52
+ blockExplorerUrl: "https://bscscan.com",
53
+ chainId: BSC_MAINNET_CHAIN_ID,
54
+ displayName: "Binance Smart Chain Mainnet",
55
+ logo: "bnb.png",
56
+ rpcTarget: `https://bsc-dataseed.binance.org`,
57
+ ticker: "BNB",
58
+ tickerName: "Binance Coin",
59
+ },
60
+ [AVALANCHE_MAINNET_CHAIN_ID]: {
61
+ blockExplorerUrl: "https://snowtrace.io",
62
+ chainId: AVALANCHE_MAINNET_CHAIN_ID,
63
+ displayName: "Avalanche Mainnet C-Chain",
64
+ logo: "avax.svg",
65
+ rpcTarget: `https://api.avax.network/ext/bc/C/rpc`,
66
+ ticker: "AVAX",
67
+ tickerName: "Avalanche",
68
+ },
69
+ [OPTIMISM_MAINNET_CHAIN_ID]: {
70
+ blockExplorerUrl: "https://optimistic.etherscan.io",
71
+ chainId: OPTIMISM_MAINNET_CHAIN_ID,
72
+ displayName: "Optimism",
73
+ logo: "optimism.svg",
74
+ rpcTarget: `https://optimism-mainnet.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
75
+ ticker: "ETH",
76
+ tickerName: "Ethereum",
77
+ },
78
+ [CELO_MAINNET_CHAIN_ID]: {
79
+ blockExplorerUrl: "https://explorer.celo.org",
80
+ chainId: CELO_MAINNET_CHAIN_ID,
81
+ displayName: "Celo Mainnet",
82
+ logo: "celo.svg",
83
+ rpcTarget: `https://celo-mainnet.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
84
+ ticker: "CELO",
85
+ tickerName: "Celo",
86
+ },
87
+ [ARBITRUM_MAINNET_CHAIN_ID]: {
88
+ blockExplorerUrl: "https://arbiscan.io",
89
+ chainId: ARBITRUM_MAINNET_CHAIN_ID,
90
+ displayName: "Arbitrum One",
91
+ logo: "eth.svg",
92
+ rpcTarget: `https://arbitrum-mainnet.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
93
+ ticker: "ETH",
94
+ tickerName: "Ethereum",
95
+ },
96
+ [XDAI_CHAIN_ID]: {
97
+ blockExplorerUrl: "https://blockscout.com/poa/xdai",
98
+ chainId: XDAI_CHAIN_ID,
99
+ displayName: "xDai",
100
+ logo: "xdai.svg",
101
+ rpcTarget: `https://rpc.gnosischain.com`,
102
+ ticker: "DAI",
103
+ tickerName: "xDai Network Token",
104
+ },
105
+ [GOERLI_CHAIN_ID]: {
106
+ blockExplorerUrl: "https://goerli.etherscan.io",
107
+ chainId: GOERLI_CHAIN_ID,
108
+ displayName: "Goerli Test Network",
109
+ logo: "eth.svg",
110
+ rpcTarget: `https://goerli.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
111
+ ticker: "ETH",
112
+ tickerName: "Ethereum",
113
+ isTestNet: true,
114
+ },
115
+ [SEPOLIA_CHAIN_ID]: {
116
+ blockExplorerUrl: "https://sepolia.etherscan.io",
117
+ chainId: SEPOLIA_CHAIN_ID,
118
+ displayName: "Sepolia Test Network",
119
+ logo: "eth.svg",
120
+ rpcTarget: `https://sepolia.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
121
+ ticker: "ETH",
122
+ tickerName: "Ethereum",
123
+ isTestNet: true,
124
+ },
125
+ [POLYGON_MUMBAI_CHAIN_ID]: {
126
+ blockExplorerUrl: "https://mumbai.polygonscan.com",
127
+ chainId: POLYGON_MUMBAI_CHAIN_ID,
128
+ displayName: "Polygon Mumbai",
129
+ logo: "matic-network-logo.svg",
130
+ rpcTarget: `https://polygon-mumbai.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
131
+ ticker: "MATIC",
132
+ tickerName: "Matic Network Token",
133
+ isTestNet: true,
134
+ },
135
+ [BSC_TESTNET_CHAIN_ID]: {
136
+ blockExplorerUrl: "https://testnet.bscscan.com",
137
+ chainId: BSC_TESTNET_CHAIN_ID,
138
+ displayName: "Binance Smart Chain Testnet",
139
+ logo: "bnb.png",
140
+ rpcTarget: `https://data-seed-prebsc-1-s1.binance.org:8545`,
141
+ ticker: "BNB",
142
+ tickerName: "Binance Coin",
143
+ isTestNet: true,
144
+ },
145
+ [AVALANCHE_TESTNET_CHAIN_ID]: {
146
+ blockExplorerUrl: "https://testnet.snowtrace.io",
147
+ chainId: AVALANCHE_TESTNET_CHAIN_ID,
148
+ displayName: "Avalanche Testnet C-Chain",
149
+ logo: "avax.png",
150
+ rpcTarget: `https://api.avax-test.network/ext/bc/C/rpc`,
151
+ ticker: "AVAX",
152
+ tickerName: "Avalanche",
153
+ isTestNet: true,
154
+ },
155
+ [ARBITRUM_TESTNET_CHAIN_ID]: {
156
+ blockExplorerUrl: "https://testnet.arbiscan.io",
157
+ chainId: ARBITRUM_TESTNET_CHAIN_ID,
158
+ displayName: "Arbitrum Goerli",
159
+ logo: "eth.svg",
160
+ rpcTarget: `https://arbitrum-rinkeby.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
161
+ ticker: "ETH",
162
+ tickerName: "Ethereum",
163
+ isTestNet: true,
164
+ },
165
+ [OPTIMISM_TESTNET_CHAIN_ID]: {
166
+ blockExplorerUrl: "https://goerli-optimism.etherscan.io",
167
+ chainId: OPTIMISM_TESTNET_CHAIN_ID,
168
+ displayName: "Optimism Goerli",
169
+ logo: "optimism.svg",
170
+ rpcTarget: `https://optimism-goerli.infura.io/v3/${process.env.VITE_APP_INFURA_PROJECT_KEY}`,
171
+ ticker: "ETH",
172
+ tickerName: "Ethereum",
173
+ isTestNet: true,
174
+ },
175
+ };
176
+
177
+ export const METHOD_TYPES = {
178
+ GET_ACCOUNTS: "eth_accounts",
179
+ ETH_TRANSACTION: "eth_sendTransaction",
180
+ ETH_REQUEST_ACCOUNTS: "eth_requestAccounts",
181
+ ETH_SEND_RAW_TRANSACTION: "eth_sendRawTransaction",
182
+ ETH_SIGN: "eth_sign",
183
+ ETH_SIGN_TYPED_DATA: "eth_signTypedData",
184
+ ETH_SIGN_TYPED_DATA_V3: "eth_signTypedData_v3",
185
+ ETH_SIGN_TYPED_DATA_V4: "eth_signTypedData_v4",
186
+ PERSONAL_SIGN: "personal_sign",
187
+ ETH_GET_TRANSACTION_COUNT: "eth_getTransactionCount",
188
+ ETH_GET_TRANSACTION_BY_HASH: "eth_getTransactionByHash",
189
+ ETH_GET_ENCRYPTION_PUBLIC_KEY: "eth_getEncryptionPublicKey",
190
+ ETH_DECRYPT: "eth_decrypt",
191
+ ETH_GET_TRANSACTION_RECEIPT: "eth_getTransactionReceipt",
192
+ WATCH_ASSET: "wallet_watchAsset",
193
+ ETH_GET_BLOCK_BY_HASH: "eth_getBlockByHash",
194
+ ETH_GET_CODE: "eth_getCode",
195
+ ETH_GET_GAS_PRICE: "eth_gasPrice",
196
+ } as const;
197
+
198
+ export const TRANSACTION_ENVELOPE_TYPES = {
199
+ LEGACY: "0x0",
200
+ ACCESS_LIST: "0x1",
201
+ FEE_MARKET: "0x2",
202
+ } as const;
203
+
204
+ export const GAS_ESTIMATE_TYPES = {
205
+ // Fee Market describes the way gas is set after the london hardfork, and was
206
+ // defined by EIP-1559.
207
+ FEE_MARKET: "fee-market",
208
+ // Legacy describes gasPrice estimates from before london hardfork, when the
209
+ // user is connected to mainnet and are presented with fast/average/slow
210
+ // estimate levels to choose from.
211
+ LEGACY: "legacy",
212
+ // EthGasPrice describes a gasPrice estimate received from eth_gasPrice. Post
213
+ // london this value should only be used for legacy type transactions when on
214
+ // networks that support EIP-1559. This type of estimate is the most accurate
215
+ // to display on custom networks that don't support EIP-1559.
216
+ ETH_GASPRICE: "eth_gasPrice",
217
+ // NoEstimate describes the state of the controller before receiving its first
218
+ // estimate.
219
+ NONE: "none",
220
+ } as const;
221
+
222
+ // https://help.optimism.io/hc/en-us/articles/4411895794715-Transaction-fees
223
+ export const CHAIN_ID_TO_GAS_LIMIT_BUFFER_MAP: Record<string, number> = {
224
+ [OPTIMISM_MAINNET_CHAIN_ID]: 1,
225
+ [OPTIMISM_TESTNET_CHAIN_ID]: 1,
226
+ };
227
+
228
+ export const OLD_ERC721_LIST: Record<string, { name?: string; logo?: string; erc20?: boolean; symbol?: string; decimals?: number }> = {
229
+ "0x06012c8cf97bead5deae237070f9587f8e7a266d": {
230
+ name: "Cryptokitties",
231
+ logo: "dapp-cryptokitty.svg",
232
+ erc20: true,
233
+ symbol: "CK",
234
+ decimals: 0,
235
+ },
236
+ };
237
+
238
+ export const TEST_CHAINS = [GOERLI_CHAIN_ID, SEPOLIA_CHAIN_ID];
239
+
240
+ export const ETHERSCAN_SUPPORTED_CHAINS = [
241
+ MAINNET_CHAIN_ID,
242
+ GOERLI_CHAIN_ID,
243
+ SEPOLIA_CHAIN_ID,
244
+ BSC_MAINNET_CHAIN_ID,
245
+ BSC_TESTNET_CHAIN_ID,
246
+ OPTIMISM_MAINNET_CHAIN_ID,
247
+ OPTIMISM_TESTNET_CHAIN_ID,
248
+ AVALANCHE_MAINNET_CHAIN_ID,
249
+ AVALANCHE_TESTNET_CHAIN_ID,
250
+ ARBITRUM_MAINNET_CHAIN_ID,
251
+ ARBITRUM_TESTNET_CHAIN_ID,
252
+ POLYGON_CHAIN_ID,
253
+ POLYGON_MUMBAI_CHAIN_ID,
254
+ CELO_MAINNET_CHAIN_ID,
255
+ ];
256
+
257
+ export const SIMPLEHASH_SUPPORTED_CHAINS = [
258
+ MAINNET_CHAIN_ID,
259
+ POLYGON_CHAIN_ID,
260
+ POLYGON_MUMBAI_CHAIN_ID,
261
+ BSC_MAINNET_CHAIN_ID,
262
+ BSC_TESTNET_CHAIN_ID,
263
+ ARBITRUM_MAINNET_CHAIN_ID,
264
+ ARBITRUM_TESTNET_CHAIN_ID,
265
+ OPTIMISM_MAINNET_CHAIN_ID,
266
+ OPTIMISM_TESTNET_CHAIN_ID,
267
+ CELO_MAINNET_CHAIN_ID,
268
+ AVALANCHE_MAINNET_CHAIN_ID,
269
+ AVALANCHE_TESTNET_CHAIN_ID,
270
+ GOERLI_CHAIN_ID,
271
+ ];
272
+
273
+ export const COINGECKO_SUPPORTED_CURRENCIES = new Set([
274
+ "btc",
275
+ "eth",
276
+ "ltc",
277
+ "bch",
278
+ "bnb",
279
+ "eos",
280
+ "xrp",
281
+ "xlm",
282
+ "link",
283
+ "dot",
284
+ "yfi",
285
+ "usd",
286
+ "aed",
287
+ "ars",
288
+ "aud",
289
+ "bdt",
290
+ "bhd",
291
+ "bmd",
292
+ "brl",
293
+ "cad",
294
+ "chf",
295
+ "clp",
296
+ "cny",
297
+ "czk",
298
+ "dkk",
299
+ "eur",
300
+ "gbp",
301
+ "hkd",
302
+ "huf",
303
+ "idr",
304
+ "ils",
305
+ "inr",
306
+ "jpy",
307
+ "krw",
308
+ "kwd",
309
+ "lkr",
310
+ "mmk",
311
+ "mxn",
312
+ "myr",
313
+ "ngn",
314
+ "nok",
315
+ "nzd",
316
+ "php",
317
+ "pkr",
318
+ "pln",
319
+ "rub",
320
+ "sar",
321
+ "sek",
322
+ "sgd",
323
+ "thb",
324
+ "try",
325
+ "twd",
326
+ "uah",
327
+ "vef",
328
+ "vnd",
329
+ "zar",
330
+ "xdr",
331
+ "xag",
332
+ "xau",
333
+ "bits",
334
+ "sats",
335
+ ]);
336
+
337
+ export const COINGECKO_PLATFORMS_CHAIN_CODE_MAP: Record<string, { platform: string; currency: string }> = {
338
+ [POLYGON_CHAIN_ID]: {
339
+ platform: "polygon-pos",
340
+ currency: "matic",
341
+ },
342
+ [BSC_MAINNET_CHAIN_ID]: {
343
+ platform: "binance-smart-chain",
344
+ currency: "bnb",
345
+ },
346
+ [MAINNET_CHAIN_ID]: {
347
+ platform: "ethereum",
348
+ currency: "eth",
349
+ },
350
+ [ARBITRUM_MAINNET_CHAIN_ID]: {
351
+ platform: "arbitrum-one",
352
+ currency: "eth",
353
+ },
354
+ [OPTIMISM_MAINNET_CHAIN_ID]: {
355
+ platform: "optimistic-ethereum",
356
+ currency: "eth",
357
+ },
358
+ [CELO_MAINNET_CHAIN_ID]: {
359
+ platform: "celo",
360
+ currency: "celo",
361
+ },
362
+ [XDAI_CHAIN_ID]: {
363
+ platform: "xdai",
364
+ currency: "xDAI",
365
+ },
366
+ [AVALANCHE_MAINNET_CHAIN_ID]: {
367
+ platform: "avalanche",
368
+ currency: "avax",
369
+ },
370
+ };
371
+
372
+ export const MessageStatus = {
373
+ UNAPPROVED: "unapproved",
374
+ SIGNED: "signed",
375
+ PENDING: "pending",
376
+ APPROVED: "approved",
377
+ REJECTED: "rejected",
378
+ FAILED: "failed",
379
+ } as const;
@@ -0,0 +1,21 @@
1
+ import {
2
+ ARBITRUM_MAINNET_CHAIN_ID,
3
+ AVALANCHE_MAINNET_CHAIN_ID,
4
+ BSC_MAINNET_CHAIN_ID,
5
+ GOERLI_CHAIN_ID,
6
+ MAINNET_CHAIN_ID,
7
+ OPTIMISM_MAINNET_CHAIN_ID,
8
+ POLYGON_CHAIN_ID,
9
+ } from "../utils/constants";
10
+
11
+ export const SINGLE_CALL_BALANCES_ADDRESSES: Record<string, string> = {
12
+ [MAINNET_CHAIN_ID]: "0xb1f8e55c7f64d203c1400b9d8555d050f94adf39",
13
+ [GOERLI_CHAIN_ID]: "0x9788C4E93f9002a7ad8e72633b11E8d1ecd51f9b",
14
+ // [SEPOLIA_CHAIN_ID]: SINGLE_CALL_BALANCES_ADDRESS_SEPOLIA,
15
+ // [FANTOM_CHAIN_ID]: "0x07f697424ABe762bB808c109860c04eA488ff92B",
16
+ [BSC_MAINNET_CHAIN_ID]: "0x2352c63A83f9Fd126af8676146721Fa00924d7e4",
17
+ [OPTIMISM_MAINNET_CHAIN_ID]: "0xB1c568e9C3E6bdaf755A60c7418C269eb11524FC",
18
+ [POLYGON_CHAIN_ID]: "0x2352c63A83f9Fd126af8676146721Fa00924d7e4",
19
+ [AVALANCHE_MAINNET_CHAIN_ID]: "0xD023D153a0DFa485130ECFdE2FAA7e612EF94818",
20
+ [ARBITRUM_MAINNET_CHAIN_ID]: "0x151E24A486D7258dd7C33Fb67E4bB01919B7B32c",
21
+ };
@@ -0,0 +1,269 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-nocheck
3
+ /* Currency Conversion Utility
4
+ * This utility function can be used for converting currency related values within metamask.
5
+ * The caller should be able to pass it a value, along with information about the value's
6
+ * numeric base, denomination and currency, and the desired numeric base, denomination and
7
+ * currency. It should return a single value.
8
+ *
9
+ * @param {(number | string | BN)} value - The value to convert.
10
+ * @param {Object} [options] - Options to specify details of the conversion
11
+ * @param {string} [options.fromCurrency = 'ETH' | 'USD'] - The currency of the passed value
12
+ * @param {string} [options.toCurrency = 'ETH' | 'USD'] - The desired currency of the result
13
+ * @param {string} [options.fromNumericBase = 'hex' | 'dec' | 'BN'] - The numeric basic of the passed value.
14
+ * @param {string} [options.toNumericBase = 'hex' | 'dec' | 'BN'] - The desired numeric basic of the result.
15
+ * @param {string} [options.fromDenomination = 'WEI'] - The denomination of the passed value
16
+ * @param {string} [options.numberOfDecimals] - The desired number of decimals in the result
17
+ * @param {string} [options.roundDown] - The desired number of decimals to round down to
18
+ * @param {number} [options.conversionRate] - The rate to use to make the fromCurrency -> toCurrency conversion
19
+ * @returns {(number | string | BN)}
20
+ *
21
+ * The utility passes value along with the options as a single object to the `converter` function.
22
+ * `converter` conditional modifies the supplied `value` property, depending
23
+ * on the accompanying options.
24
+ */
25
+
26
+ import { stripHexPrefix } from "@ethereumjs/util";
27
+ import BigNumber from "bignumber.js";
28
+ import BN from "bn.js";
29
+
30
+ // Big Number Constants
31
+ const BIG_NUMBER_WEI_MULTIPLIER = new BigNumber("1000000000000000000");
32
+ const BIG_NUMBER_GWEI_MULTIPLIER = new BigNumber("1000000000");
33
+ const BIG_NUMBER_ETH_MULTIPLIER = new BigNumber("1");
34
+
35
+ type NumericBase = "hex" | "dec" | "BN";
36
+
37
+ type EthDenomination = "WEI" | "GWEI" | "ETH";
38
+
39
+ type ConverterOptions = {
40
+ value: BigNumber | string;
41
+ fromNumericBase: NumericBase;
42
+ fromDenomination: EthDenomination;
43
+ fromCurrency: string;
44
+ toNumericBase: NumericBase;
45
+ toDenomination: EthDenomination;
46
+ toCurrency: string;
47
+ numberOfDecimals: number;
48
+ conversionRate: number;
49
+ invertConversionRate: boolean;
50
+ roundDown?: number;
51
+ };
52
+
53
+ // Setter Maps
54
+ const toBigNumber = {
55
+ hex: (n) => new BigNumber(stripHexPrefix(n), 16),
56
+ dec: (n) => new BigNumber(String(n), 10),
57
+ BN: (n) => new BigNumber(n.toString(16), 16),
58
+ };
59
+ const toNormalizedDenomination = {
60
+ WEI: (bigNumber) => bigNumber.div(BIG_NUMBER_WEI_MULTIPLIER),
61
+ GWEI: (bigNumber) => bigNumber.div(BIG_NUMBER_GWEI_MULTIPLIER),
62
+ ETH: (bigNumber) => bigNumber.div(BIG_NUMBER_ETH_MULTIPLIER),
63
+ };
64
+ const toSpecifiedDenomination = {
65
+ WEI: (bigNumber) => bigNumber.times(BIG_NUMBER_WEI_MULTIPLIER).dp(0, BigNumber.ROUND_HALF_UP),
66
+ GWEI: (bigNumber) => bigNumber.times(BIG_NUMBER_GWEI_MULTIPLIER).dp(9, BigNumber.ROUND_HALF_UP),
67
+ ETH: (bigNumber) => bigNumber.times(BIG_NUMBER_ETH_MULTIPLIER).dp(9, BigNumber.ROUND_HALF_UP),
68
+ };
69
+ const baseChange = {
70
+ hex: (n) => n.toString(16),
71
+ dec: (n) => new BigNumber(n).toString(10),
72
+ BN: (n) => new BN(n.toString(16)),
73
+ };
74
+
75
+ // Utility function for checking base types
76
+ const isValidBase = (base) => Number.isInteger(base) && base > 1;
77
+
78
+ /**
79
+ * Utility method to convert a value between denominations, formats and currencies.
80
+ */
81
+ const converter = ({
82
+ value,
83
+ fromNumericBase,
84
+ fromDenomination,
85
+ fromCurrency,
86
+ toNumericBase,
87
+ toDenomination,
88
+ toCurrency,
89
+ numberOfDecimals,
90
+ conversionRate,
91
+ invertConversionRate,
92
+ roundDown,
93
+ }: ConverterOptions) => {
94
+ let convertedValue = fromNumericBase ? toBigNumber[fromNumericBase](value) : value;
95
+
96
+ if (fromDenomination) {
97
+ convertedValue = toNormalizedDenomination[fromDenomination](convertedValue);
98
+ }
99
+
100
+ if (fromCurrency !== toCurrency) {
101
+ if (conversionRate === null || conversionRate === undefined) {
102
+ throw new Error(`Converting from ${fromCurrency} to ${toCurrency} requires a conversionRate, but one was not provided`);
103
+ }
104
+ let rate = toBigNumber.dec(conversionRate);
105
+ if (invertConversionRate) {
106
+ rate = new BigNumber(1).div(conversionRate);
107
+ }
108
+ convertedValue = convertedValue.times(rate);
109
+ }
110
+
111
+ if (toDenomination) {
112
+ convertedValue = toSpecifiedDenomination[toDenomination](convertedValue);
113
+ }
114
+
115
+ if (numberOfDecimals) {
116
+ convertedValue = convertedValue.dp(numberOfDecimals, BigNumber.ROUND_HALF_DOWN);
117
+ }
118
+
119
+ if (roundDown) {
120
+ convertedValue = convertedValue.dp(roundDown, BigNumber.ROUND_DOWN);
121
+ }
122
+
123
+ if (toNumericBase) {
124
+ convertedValue = baseChange[toNumericBase](convertedValue);
125
+ }
126
+ return convertedValue;
127
+ };
128
+
129
+ export const conversionUtil = (
130
+ value: BigNumber | string,
131
+ {
132
+ fromCurrency = null,
133
+ toCurrency = fromCurrency,
134
+ fromNumericBase,
135
+ toNumericBase,
136
+ fromDenomination,
137
+ toDenomination,
138
+ numberOfDecimals,
139
+ conversionRate,
140
+ invertConversionRate,
141
+ }: Omit<ConverterOptions, "value">
142
+ ) => {
143
+ if (fromCurrency !== toCurrency && !conversionRate) {
144
+ return 0;
145
+ }
146
+ return converter({
147
+ fromCurrency,
148
+ toCurrency,
149
+ fromNumericBase,
150
+ toNumericBase,
151
+ fromDenomination,
152
+ toDenomination,
153
+ numberOfDecimals,
154
+ conversionRate,
155
+ invertConversionRate,
156
+ value,
157
+ });
158
+ };
159
+
160
+ export const getBigNumber = (value, base) => {
161
+ if (!isValidBase(base)) {
162
+ throw new Error("Must specificy valid base");
163
+ }
164
+
165
+ // We don't include 'number' here, because BigNumber will throw if passed
166
+ // a number primitive it considers unsafe.
167
+ if (typeof value === "string" || value instanceof BigNumber) {
168
+ return new BigNumber(value, base);
169
+ }
170
+
171
+ return new BigNumber(String(value), base);
172
+ };
173
+
174
+ export const addCurrencies = (a, b, options: Record<string, unknown> = {}) => {
175
+ const { aBase, bBase, ...conversionOptions } = options;
176
+
177
+ if (!isValidBase(aBase) || !isValidBase(bBase)) {
178
+ throw new Error("Must specify valid aBase and bBase");
179
+ }
180
+
181
+ const value = getBigNumber(a, aBase).plus(getBigNumber(b, bBase));
182
+
183
+ return converter({
184
+ value,
185
+ ...conversionOptions,
186
+ } as ConverterOptions);
187
+ };
188
+
189
+ export const subtractCurrencies = (a, b, options: Record<string, unknown> = {}) => {
190
+ const { aBase, bBase, ...conversionOptions } = options;
191
+
192
+ if (!isValidBase(aBase) || !isValidBase(bBase)) {
193
+ throw new Error("Must specify valid aBase and bBase");
194
+ }
195
+
196
+ const value = getBigNumber(a, aBase).minus(getBigNumber(b, bBase));
197
+
198
+ return converter({
199
+ value,
200
+ ...conversionOptions,
201
+ } as ConverterOptions);
202
+ };
203
+
204
+ export const multiplyCurrencies = (a, b, options: Record<string, unknown> = {}) => {
205
+ const { multiplicandBase, multiplierBase, ...conversionOptions } = options;
206
+
207
+ if (!isValidBase(multiplicandBase) || !isValidBase(multiplierBase)) {
208
+ throw new Error("Must specify valid multiplicandBase and multiplierBase");
209
+ }
210
+
211
+ const value = getBigNumber(a, multiplicandBase).times(getBigNumber(b, multiplierBase));
212
+
213
+ return converter({
214
+ value,
215
+ ...conversionOptions,
216
+ } as ConverterOptions);
217
+ };
218
+
219
+ export const conversionGreaterThan = ({ ...firstProps }: ConverterOptions, { ...secondProps }: ConverterOptions) => {
220
+ const firstValue = converter({ ...firstProps });
221
+ const secondValue = converter({ ...secondProps });
222
+
223
+ return firstValue.gt(secondValue);
224
+ };
225
+
226
+ export const conversionLessThan = ({ ...firstProps }: ConverterOptions, { ...secondProps }: ConverterOptions) => {
227
+ const firstValue = converter({ ...firstProps });
228
+ const secondValue = converter({ ...secondProps });
229
+
230
+ return firstValue.lt(secondValue);
231
+ };
232
+
233
+ export const conversionMax = ({ ...firstProps }, { ...secondProps }) => {
234
+ const firstIsGreater = conversionGreaterThan({ ...firstProps }, { ...secondProps });
235
+
236
+ return firstIsGreater ? firstProps.value : secondProps.value;
237
+ };
238
+
239
+ export const conversionGTE = ({ ...firstProps }, { ...secondProps }) => {
240
+ const firstValue = converter({ ...firstProps } as ConverterOptions);
241
+ const secondValue = converter({ ...secondProps } as ConverterOptions);
242
+ return firstValue.isGreaterThanOrEqualTo(secondValue);
243
+ };
244
+
245
+ export const conversionLTE = ({ ...firstProps }, { ...secondProps }) => {
246
+ const firstValue = converter({ ...firstProps } as ConverterOptions);
247
+ const secondValue = converter({ ...secondProps } as ConverterOptions);
248
+ return firstValue.isLessThanOrEqualTo(secondValue);
249
+ };
250
+
251
+ export const toNegative = (n, options = {}) => multiplyCurrencies(n, -1, options);
252
+
253
+ export const decGWEIToHexWEI = (decGWEI: BigNumber): BigNumber => {
254
+ return conversionUtil(decGWEI, {
255
+ fromNumericBase: "dec",
256
+ toNumericBase: "hex",
257
+ fromDenomination: "GWEI",
258
+ toDenomination: "WEI",
259
+ } as ConverterOptions);
260
+ };
261
+
262
+ export const hexWEIToDecGWEI = (decGWEI: BigNumber | string): BigNumber => {
263
+ return conversionUtil(decGWEI, {
264
+ fromNumericBase: "hex",
265
+ toNumericBase: "dec",
266
+ fromDenomination: "WEI",
267
+ toDenomination: "GWEI",
268
+ } as ConverterOptions);
269
+ };