@avalabs/evm-module 0.0.22 → 0.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 (90) hide show
  1. package/LICENSE +9 -0
  2. package/dist/index.cjs +18 -20
  3. package/dist/index.cjs.map +1 -1
  4. package/dist/index.d.cts +2 -2
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.js +16 -17
  7. package/dist/index.js.map +1 -1
  8. package/package.json +14 -8
  9. package/.turbo/turbo-build.log +0 -22
  10. package/.turbo/turbo-lint.log +0 -4
  11. package/.turbo/turbo-test.log +0 -117
  12. package/CHANGELOG.md +0 -166
  13. package/jest.config.js +0 -9
  14. package/src/constants.ts +0 -1
  15. package/src/contracts/openzeppelin/ERC1155.ts +0 -440
  16. package/src/contracts/openzeppelin/ERC20.ts +0 -330
  17. package/src/contracts/openzeppelin/ERC721.ts +0 -420
  18. package/src/contracts/openzeppelin/common.ts +0 -131
  19. package/src/contracts/openzeppelin/factories/ERC1155__factory.ts +0 -388
  20. package/src/contracts/openzeppelin/factories/ERC20__factory.ts +0 -353
  21. package/src/contracts/openzeppelin/factories/ERC721__factory.ts +0 -413
  22. package/src/contracts/openzeppelin/factories/index.ts +0 -6
  23. package/src/contracts/openzeppelin/index.ts +0 -10
  24. package/src/env.ts +0 -25
  25. package/src/handlers/eth-send-transaction/eth-send-transaction.test.ts +0 -572
  26. package/src/handlers/eth-send-transaction/eth-send-transaction.ts +0 -189
  27. package/src/handlers/eth-send-transaction/schema.test.ts +0 -240
  28. package/src/handlers/eth-send-transaction/schema.ts +0 -20
  29. package/src/handlers/eth-sign/eth-sign.test.ts +0 -359
  30. package/src/handlers/eth-sign/eth-sign.ts +0 -158
  31. package/src/handlers/eth-sign/schemas/eth-sign-typed-data.ts +0 -65
  32. package/src/handlers/eth-sign/schemas/eth-sign.ts +0 -9
  33. package/src/handlers/eth-sign/schemas/parse-request-params/fixture.ts +0 -47
  34. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.test.ts +0 -284
  35. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.ts +0 -94
  36. package/src/handlers/eth-sign/schemas/personal-sign.ts +0 -12
  37. package/src/handlers/eth-sign/schemas/shared.ts +0 -5
  38. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.test.ts +0 -29
  39. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.ts +0 -134
  40. package/src/handlers/eth-sign/utils/is-typed-data-valid.ts +0 -26
  41. package/src/handlers/eth-sign/utils/typeguards.ts +0 -10
  42. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.test.ts +0 -90
  43. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.ts +0 -23
  44. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +0 -78
  45. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +0 -85
  46. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.test.ts +0 -97
  47. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.ts +0 -56
  48. package/src/handlers/get-balances/get-balances.test.ts +0 -249
  49. package/src/handlers/get-balances/get-balances.ts +0 -121
  50. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +0 -72
  51. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +0 -109
  52. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.test.ts +0 -60
  53. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.ts +0 -47
  54. package/src/handlers/get-network-fee/get-network-fee.test.ts +0 -43
  55. package/src/handlers/get-network-fee/get-network-fee.ts +0 -59
  56. package/src/handlers/get-tokens/get-tokens.test.ts +0 -100
  57. package/src/handlers/get-tokens/get-tokens.ts +0 -18
  58. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.ts +0 -51
  59. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.ts +0 -58
  60. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.test.ts +0 -116
  61. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.ts +0 -73
  62. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/convert-transaction.ts +0 -47
  63. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-nft-metadata.ts +0 -35
  64. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-sender-info.ts +0 -38
  65. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tokens.ts +0 -107
  66. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transaction-from-glacier.test.ts +0 -222
  67. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transactions-from-glacier.ts +0 -62
  68. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tx-type.ts +0 -52
  69. package/src/handlers/get-transaction-history/get-transaction-history.test.ts +0 -57
  70. package/src/handlers/get-transaction-history/get-transaction-history.ts +0 -55
  71. package/src/handlers/get-transaction-history/utils/get-explorer-address-by-network.ts +0 -7
  72. package/src/handlers/get-transaction-history/utils/get-small-image-for-nft.ts +0 -16
  73. package/src/handlers/get-transaction-history/utils/ipfs-resolver-with-fallback.ts +0 -18
  74. package/src/handlers/get-transaction-history/utils/is-ethereum-chain-id.ts +0 -15
  75. package/src/handlers/get-transaction-history/utils/resolve.ts +0 -7
  76. package/src/index.ts +0 -3
  77. package/src/module.ts +0 -138
  78. package/src/services/glacier-service/glacier-service.ts +0 -238
  79. package/src/types.ts +0 -25
  80. package/src/utils/estimate-gas-limit.ts +0 -27
  81. package/src/utils/get-chain-id.ts +0 -12
  82. package/src/utils/get-nonce.ts +0 -11
  83. package/src/utils/get-provider.ts +0 -46
  84. package/src/utils/parse-erc20-transaction-type.ts +0 -35
  85. package/src/utils/process-transaction-simulation.test.ts +0 -105
  86. package/src/utils/process-transaction-simulation.ts +0 -293
  87. package/src/utils/scan-transaction.ts +0 -63
  88. package/tsconfig.jest.json +0 -7
  89. package/tsconfig.json +0 -14
  90. package/tsup.config.ts +0 -4
@@ -1,121 +0,0 @@
1
- import {
2
- type GetBalancesResponse,
3
- type GetBalancesParams,
4
- type Storage,
5
- TokenType,
6
- type NetworkContractToken,
7
- type ERC20Token,
8
- } from '@avalabs/vm-module-types';
9
- import { getErc20Balances } from './evm-balance-service/get-erc20-balances';
10
- import { TokenService } from '@internal/utils';
11
- import { getProvider } from '../../utils/get-provider';
12
- import { getTokens } from '../get-tokens/get-tokens';
13
- import { getNativeTokenBalances } from './evm-balance-service/get-native-token-balances';
14
- import { getNativeTokenBalances as getNativeTokenBalancesFromGlacier } from './glacier-balance-service/get-native-token-balances';
15
- import { getErc20Balances as getErc20BalancesFromGlacier } from './glacier-balance-service/get-erc20-balances';
16
- import type { EvmGlacierService } from '../../services/glacier-service/glacier-service';
17
-
18
- export const getBalances = async ({
19
- addresses,
20
- currency,
21
- network,
22
- proxyApiUrl,
23
- customTokens = [],
24
- storage,
25
- glacierService,
26
- }: GetBalancesParams & {
27
- proxyApiUrl: string;
28
- glacierService: EvmGlacierService;
29
- storage?: Storage;
30
- }): Promise<GetBalancesResponse> => {
31
- const chainId = network.chainId;
32
- const isNetworkSupported = await glacierService.isNetworkSupported(network.chainId);
33
- const isHealthy = glacierService.isHealthy();
34
-
35
- let balances = [];
36
- if (isHealthy && isNetworkSupported) {
37
- balances = await Promise.allSettled(
38
- addresses.map(async (address) => {
39
- const nativeToken = await getNativeTokenBalancesFromGlacier({
40
- address,
41
- currency,
42
- chainId,
43
- glacierService,
44
- coingeckoId: network.pricingProviders?.coingecko.nativeTokenId ?? '',
45
- });
46
-
47
- const erc20Tokens = await getErc20BalancesFromGlacier({
48
- customTokens: customTokens.filter(isERC20Token),
49
- glacierService,
50
- currency,
51
- chainId,
52
- address,
53
- });
54
-
55
- return {
56
- address,
57
- balances: {
58
- [nativeToken.symbol]: nativeToken,
59
- ...erc20Tokens,
60
- },
61
- };
62
- }),
63
- );
64
- } else {
65
- const tokenService = new TokenService({ storage, proxyApiUrl });
66
- const tokens = await getTokens({ chainId: Number(chainId), proxyApiUrl });
67
- const allTokens = [...tokens, ...customTokens];
68
- const provider = getProvider({
69
- chainId,
70
- chainName: network.chainName,
71
- rpcUrl: network.rpcUrl,
72
- multiContractAddress: network.utilityAddresses?.multicall,
73
- });
74
-
75
- balances = await Promise.allSettled(
76
- addresses.map(async (address) => {
77
- const nativeToken = await getNativeTokenBalances({
78
- network,
79
- tokenService,
80
- address,
81
- currency,
82
- provider,
83
- });
84
-
85
- const erc20Tokens = await getErc20Balances({
86
- provider,
87
- network,
88
- tokenService,
89
- address,
90
- currency,
91
- tokens: allTokens.filter(isERC20Token),
92
- });
93
-
94
- return {
95
- address,
96
- balances: {
97
- [nativeToken.symbol]: nativeToken,
98
- ...erc20Tokens,
99
- },
100
- };
101
- }),
102
- );
103
- }
104
-
105
- const filterBalances = balances.reduce((acc, result) => {
106
- if (result.status === 'rejected') {
107
- return acc;
108
- }
109
-
110
- return {
111
- ...acc,
112
- [result.value.address]: result.value.balances,
113
- };
114
- }, {} as GetBalancesResponse);
115
-
116
- return filterBalances;
117
- };
118
-
119
- function isERC20Token(token: NetworkContractToken): token is ERC20Token {
120
- return token.type === TokenType.ERC20;
121
- }
@@ -1,72 +0,0 @@
1
- import { BN } from 'bn.js';
2
- import { getErc20Balances } from './get-erc20-balances';
3
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
4
-
5
- describe('get-erc20-balances', () => {
6
- it('should return erc20 token balances', async () => {
7
- const mockGlacierService: EvmGlacierService = {
8
- ...expect.any(Object),
9
- listErc20Balances: jest.fn().mockResolvedValue({
10
- erc20TokenBalances: [
11
- {
12
- address: '0x123',
13
- name: 'Ethereum',
14
- symbol: 'ETH',
15
- decimals: 18,
16
- logoUri: 'https://example.com/logo.png',
17
- ercType: 'ERC20',
18
- price: {
19
- currency: 'USD',
20
- value: 1000,
21
- },
22
- chainId: '123',
23
- balance: '1000000000000000000',
24
- balanceValue: {
25
- currency: 'USD',
26
- value: 1000,
27
- },
28
- },
29
- ],
30
- }),
31
- };
32
- const balance = getErc20Balances({
33
- glacierService: mockGlacierService,
34
- currency: 'USD',
35
- chainId: 123,
36
- address: '0x123',
37
- customTokens: [],
38
- });
39
-
40
- expect(balance).resolves.toEqual({
41
- '0x123': {
42
- chainId: 123,
43
- address: '0x123',
44
- name: 'Ethereum',
45
- symbol: 'ETH',
46
- decimals: 18,
47
- logoUri: 'https://example.com/logo.png',
48
- balance: new BN('1000000000000000000'),
49
- balanceCurrencyDisplayValue: '1000',
50
- balanceDisplayValue: '1',
51
- balanceInCurrency: 1000,
52
- priceInCurrency: 1000,
53
- type: 'ERC20',
54
- },
55
- });
56
- });
57
-
58
- it('should return native token object without balance data', async () => {
59
- const mockGlacierService: EvmGlacierService = {
60
- ...expect.any(Object),
61
- listErc20Balances: jest.fn().mockRejectedValue(new Error('Failed to get erc20 balance')),
62
- };
63
- const balance = getErc20Balances({
64
- glacierService: mockGlacierService,
65
- currency: 'USD',
66
- chainId: 123,
67
- address: '0x123',
68
- customTokens: [],
69
- });
70
- expect(balance).rejects.toEqual(new Error('Failed to get erc20 balance'));
71
- });
72
- });
@@ -1,109 +0,0 @@
1
- import { balanceToDisplayValue, bnToBig } from '@avalabs/utils-sdk';
2
- import {
3
- TokenType,
4
- type TokenWithBalanceERC20,
5
- type TokenWithBalance,
6
- type ERC20Token,
7
- } from '@avalabs/vm-module-types';
8
- import { CurrencyCode, Erc20TokenBalance } from '@avalabs/glacier-sdk';
9
- import BN from 'bn.js';
10
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
11
- import { DEFAULT_DECIMALS } from '../../../constants';
12
-
13
- export const getErc20Balances = async ({
14
- glacierService,
15
- currency,
16
- chainId,
17
- address,
18
- customTokens,
19
- }: {
20
- glacierService: EvmGlacierService;
21
- address: string;
22
- currency: string;
23
- chainId: number;
24
- customTokens: ERC20Token[];
25
- }): Promise<Record<string, TokenWithBalance>> => {
26
- const tokensWithBalance: TokenWithBalanceERC20[] = [];
27
- /**
28
- * Load all pages to make sure we have all the tokens with balances
29
- */
30
- let nextPageToken: string | undefined;
31
- do {
32
- const response = await glacierService.listErc20Balances({
33
- chainId: chainId.toString(),
34
- address,
35
- currency: currency.toLocaleLowerCase() as CurrencyCode,
36
- // glacier has a cap on page size of 100
37
- pageSize: 100,
38
- pageToken: nextPageToken,
39
- });
40
-
41
- tokensWithBalance.push(
42
- ...convertErc20TokenWithBalanceToTokenWithBalance(response.erc20TokenBalances, Number(chainId)),
43
- );
44
- nextPageToken = response.nextPageToken;
45
- } while (nextPageToken);
46
-
47
- /**
48
- * Glacier doesnt return tokens without balances so we need to polyfill that list
49
- * from our own list of tokens. We just set the balance to 0, these zero balance
50
- * tokens are only used for swap, bridge and tx parsing.
51
- */
52
- return [
53
- ...convertErc20TokenToTokenWithBalance(customTokens),
54
- ...tokensWithBalance, // this needs to be second in the list so it overwrites its zero balance counterpart if there is one
55
- ].reduce(
56
- (acc, token) => {
57
- return { ...acc, [token.address.toLowerCase()]: token };
58
- },
59
- {} as Record<string, TokenWithBalance>,
60
- );
61
- };
62
-
63
- const convertErc20TokenToTokenWithBalance = (tokens: ERC20Token[]): TokenWithBalanceERC20[] => {
64
- return tokens.map((token) => {
65
- return {
66
- ...token,
67
- decimals: token.decimals ?? DEFAULT_DECIMALS,
68
- type: TokenType.ERC20,
69
- balance: new BN(0),
70
- balanceInCurrency: 0,
71
- balanceDisplayValue: '0',
72
- balanceCurrencyDisplayValue: '0',
73
- priceInCurrency: 0,
74
- marketCap: 0,
75
- change24: 0,
76
- vol24: 0,
77
- };
78
- });
79
- };
80
-
81
- const convertErc20TokenWithBalanceToTokenWithBalance = (
82
- tokenBalances: Erc20TokenBalance[],
83
- chainId: number,
84
- ): TokenWithBalanceERC20[] => {
85
- return tokenBalances.map((token: Erc20TokenBalance): TokenWithBalanceERC20 => {
86
- const balance = new BN(token.balance);
87
- const balanceDisplayValue = balanceToDisplayValue(balance, token.decimals);
88
- const balanceCurrencyDisplayValue = token.balanceValue?.value.toString();
89
- const priceInCurrency = token.price?.value;
90
- const balanceInCurrency = priceInCurrency
91
- ? bnToBig(balance, token.decimals).mul(priceInCurrency).toNumber()
92
- : undefined;
93
-
94
- return {
95
- chainId,
96
- address: token.address,
97
- name: token.name,
98
- symbol: token.symbol,
99
- decimals: token.decimals,
100
- logoUri: token.logoUri,
101
- balance,
102
- balanceCurrencyDisplayValue,
103
- balanceDisplayValue,
104
- balanceInCurrency,
105
- priceInCurrency,
106
- type: TokenType.ERC20,
107
- };
108
- });
109
- };
@@ -1,60 +0,0 @@
1
- import { BN } from 'bn.js';
2
- import { getNativeTokenBalances } from './get-native-token-balances';
3
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
4
-
5
- describe('get-native-token-balances', () => {
6
- it('should return native token balances', async () => {
7
- const mockGlacierService: EvmGlacierService = {
8
- ...expect.any(Object),
9
- getNativeBalance: jest.fn().mockResolvedValue({
10
- nativeTokenBalance: {
11
- balance: new BN('1000000000000000000'),
12
- decimals: 18,
13
- name: 'Ethereum',
14
- symbol: 'ETH',
15
- price: {
16
- value: 1000,
17
- },
18
- logoUri: 'https://example.com/logo.png',
19
- },
20
- }),
21
- };
22
-
23
- const balance = getNativeTokenBalances({
24
- address: '0x123',
25
- currency: 'USD',
26
- chainId: 123,
27
- glacierService: mockGlacierService,
28
- coingeckoId: '',
29
- });
30
-
31
- expect(balance).resolves.toEqual({
32
- name: 'Ethereum',
33
- symbol: 'ETH',
34
- decimals: 18,
35
- type: 'NATIVE',
36
- logoUri: 'https://example.com/logo.png',
37
- balance: new BN('1000000000000000000'),
38
- balanceDisplayValue: '1',
39
- balanceInCurrency: 1000,
40
- balanceCurrencyDisplayValue: '1000.00',
41
- priceInCurrency: 1000,
42
- coingeckoId: '',
43
- });
44
- });
45
-
46
- it('should return native token object without balance data', async () => {
47
- const mockGlacierService: EvmGlacierService = {
48
- ...expect.any(Object),
49
- getNativeBalance: jest.fn().mockRejectedValue(new Error('Failed to get native balance')),
50
- };
51
- const balance = getNativeTokenBalances({
52
- address: '0x123',
53
- currency: 'USD',
54
- chainId: 123,
55
- glacierService: mockGlacierService,
56
- coingeckoId: '',
57
- });
58
- expect(balance).rejects.toEqual(new Error('Failed to get native balance'));
59
- });
60
- });
@@ -1,47 +0,0 @@
1
- import type { CurrencyCode } from '@avalabs/glacier-sdk';
2
- import { TokenType, type NetworkTokenWithBalance } from '@avalabs/vm-module-types';
3
- import { balanceToDisplayValue, bnToBig } from '@avalabs/utils-sdk';
4
- import { BN } from 'bn.js';
5
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
6
-
7
- export const getNativeTokenBalances = async ({
8
- address,
9
- currency,
10
- chainId,
11
- glacierService,
12
- coingeckoId,
13
- }: {
14
- chainId: number;
15
- address: string;
16
- currency: string;
17
- coingeckoId: string;
18
- glacierService: EvmGlacierService;
19
- }): Promise<NetworkTokenWithBalance> => {
20
- const nativeBalance = await glacierService.getNativeBalance({
21
- chainId: chainId.toString(),
22
- address,
23
- currency: currency.toLocaleLowerCase() as CurrencyCode,
24
- });
25
- const nativeTokenBalance = nativeBalance.nativeTokenBalance;
26
- const balance = new BN(nativeTokenBalance.balance);
27
- const balanceDisplayValue = balanceToDisplayValue(balance, nativeTokenBalance.decimals);
28
- const priceInCurrency = nativeTokenBalance.price?.value;
29
- const balanceInCurrency = priceInCurrency
30
- ? bnToBig(balance, nativeTokenBalance.decimals).mul(priceInCurrency).toNumber()
31
- : undefined;
32
- const balanceCurrencyDisplayValue = balanceInCurrency?.toFixed(2);
33
-
34
- return {
35
- name: nativeTokenBalance.name,
36
- symbol: nativeTokenBalance.symbol,
37
- decimals: nativeTokenBalance.decimals,
38
- type: TokenType.NATIVE,
39
- logoUri: nativeTokenBalance.logoUri,
40
- balance,
41
- balanceDisplayValue,
42
- balanceInCurrency,
43
- balanceCurrencyDisplayValue,
44
- priceInCurrency,
45
- coingeckoId,
46
- };
47
- };
@@ -1,43 +0,0 @@
1
- import { getNetworkFee } from './get-network-fee';
2
- import { FeeData, JsonRpcProvider } from 'ethers';
3
-
4
- const params = {
5
- chainId: 1,
6
- chainName: 'Ethereum Mainnet',
7
- rpcUrl: 'https://mainnet.infura.io/v3/1234567890',
8
- };
9
-
10
- describe('get-network-fee', () => {
11
- it("should throw error if provider.getFeeData() doesn't return eip-1559 compatible fees", async () => {
12
- jest.spyOn(JsonRpcProvider.prototype, 'getFeeData').mockImplementationOnce(async () => {
13
- return {
14
- maxFeePerGas: null,
15
- } as FeeData;
16
- });
17
- await expect(getNetworkFee(params)).rejects.toThrow('Pre-EIP-1559 networks are not supported');
18
- });
19
-
20
- it('should return correct network fees based on maxFeePerGas returned from provider.getFeeData', async () => {
21
- jest.spyOn(JsonRpcProvider.prototype, 'getFeeData').mockImplementationOnce(async () => {
22
- return {
23
- maxFeePerGas: 1000000000n, //1 gWei
24
- } as FeeData;
25
- });
26
- await expect(getNetworkFee(params)).resolves.toEqual({
27
- baseFee: 1000000000n,
28
- low: {
29
- maxFeePerGas: 1500000000n,
30
- maxPriorityFeePerGas: 500000000n,
31
- },
32
- medium: {
33
- maxFeePerGas: 3000000000n,
34
- maxPriorityFeePerGas: 2000000000n,
35
- },
36
- high: {
37
- maxFeePerGas: 4000000000n,
38
- maxPriorityFeePerGas: 3000000000n,
39
- },
40
- isFixedFee: false,
41
- });
42
- });
43
- });
@@ -1,59 +0,0 @@
1
- import type { NetworkFees } from '@avalabs/vm-module-types';
2
- import { getProvider } from '../../utils/get-provider';
3
- import { rpcErrors } from '@metamask/rpc-errors';
4
-
5
- const DEFAULT_PRESETS = {
6
- LOW: 1n,
7
- MEDIUM: 4n,
8
- HIGH: 6n,
9
- };
10
-
11
- const BASE_PRIORITY_FEE_WEI = 500000000n; //0.5 GWei
12
-
13
- /**
14
- * Returns {@link NetworkFees} based on {@link DEFAULT_PRESETS} multipliers.
15
- * @throws Error if provider does not support eip-1559
16
- */
17
- export async function getNetworkFee({
18
- chainId,
19
- chainName,
20
- rpcUrl,
21
- multiContractAddress,
22
- }: {
23
- chainId: number;
24
- chainName: string;
25
- rpcUrl: string;
26
- multiContractAddress?: string;
27
- }): Promise<NetworkFees> {
28
- const provider = getProvider({
29
- chainId,
30
- chainName,
31
- rpcUrl,
32
- multiContractAddress,
33
- });
34
-
35
- const { maxFeePerGas: maxFeePerGasInWei } = await provider.getFeeData();
36
- if (!maxFeePerGasInWei) {
37
- throw rpcErrors.internal('Pre-EIP-1559 networks are not supported');
38
- }
39
-
40
- const lowMaxTip = BASE_PRIORITY_FEE_WEI * DEFAULT_PRESETS.LOW;
41
- const mediumMaxTip = BASE_PRIORITY_FEE_WEI * DEFAULT_PRESETS.MEDIUM;
42
- const highMaxTip = BASE_PRIORITY_FEE_WEI * DEFAULT_PRESETS.HIGH;
43
- return {
44
- baseFee: maxFeePerGasInWei,
45
- low: {
46
- maxFeePerGas: maxFeePerGasInWei + lowMaxTip,
47
- maxPriorityFeePerGas: lowMaxTip,
48
- },
49
- medium: {
50
- maxFeePerGas: maxFeePerGasInWei + mediumMaxTip,
51
- maxPriorityFeePerGas: mediumMaxTip,
52
- },
53
- high: {
54
- maxFeePerGas: maxFeePerGasInWei + highMaxTip,
55
- maxPriorityFeePerGas: highMaxTip,
56
- },
57
- isFixedFee: false,
58
- };
59
- }
@@ -1,100 +0,0 @@
1
- import { getTokens } from './get-tokens';
2
-
3
- const PROXY_API_URL = 'https://proxy-api.avax.network';
4
-
5
- global.fetch = jest.fn();
6
-
7
- describe('get-tokens', () => {
8
- beforeAll(() => {
9
- jest.clearAllMocks();
10
- });
11
-
12
- it('should return an empty array if chain id is not for a valid network', async () => {
13
- (global.fetch as jest.Mock).mockResolvedValueOnce({
14
- ok: true,
15
- json: async () => [],
16
- });
17
-
18
- const result = await getTokens({ chainId: 0, proxyApiUrl: PROXY_API_URL });
19
-
20
- expect(result).toEqual([]);
21
- });
22
-
23
- it('should return correct contract tokens for Avalanche C-Chain', async () => {
24
- (global.fetch as jest.Mock).mockResolvedValueOnce({
25
- ok: true,
26
- json: async () => [
27
- {
28
- address: '0xB31f66AA3C1e785363F0875A1B74E27b85FD66c7',
29
- name: 'Wrapped AVAX',
30
- symbol: 'WAVAX',
31
- type: 'ERC20',
32
- chainId: 43114,
33
- decimals: 18,
34
- },
35
- {
36
- address: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E',
37
- name: 'USD Coin',
38
- symbol: 'USDC',
39
- type: 'ERC20',
40
- chainId: 43114,
41
- decimals: 6,
42
- },
43
- ],
44
- });
45
-
46
- const result = await getTokens({ chainId: 43114, proxyApiUrl: PROXY_API_URL });
47
-
48
- expect(result.length).toBeGreaterThan(0);
49
- expect(result.find((token) => token.symbol === 'WAVAX')).toBeDefined();
50
- expect(result.find((token) => token.symbol === 'USDC')).toBeDefined();
51
- });
52
-
53
- it('should return correct contract tokens for Ethereum', async () => {
54
- (global.fetch as jest.Mock).mockResolvedValueOnce({
55
- ok: true,
56
- json: async () => [
57
- {
58
- address: '0xdAC17F958D2ee523a2206206994597C13D831ec7',
59
- name: 'Tether USD',
60
- symbol: 'USDT',
61
- type: 'ERC20',
62
- chainId: 1,
63
- decimals: 6,
64
- },
65
- {
66
- address: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
67
- name: 'USDCoin',
68
- symbol: 'USDC',
69
- type: 'ERC20',
70
- chainId: 1,
71
- decimals: 6,
72
- },
73
- {
74
- address: '0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599',
75
- name: 'Wrapped BTC',
76
- symbol: 'WBTC',
77
- type: 'ERC20',
78
- chainId: 1,
79
- decimals: 8,
80
- },
81
- {
82
- address: '0x514910771AF9Ca656af840dff83E8264EcF986CA',
83
- name: 'ChainLink Token',
84
- symbol: 'LINK',
85
- type: 'ERC20',
86
- chainId: 1,
87
- decimals: 18,
88
- },
89
- ],
90
- });
91
-
92
- const result = await getTokens({ chainId: 1, proxyApiUrl: PROXY_API_URL });
93
-
94
- expect(result.length).toBeGreaterThan(0);
95
- expect(result.find((token) => token.symbol === 'USDT')).toBeDefined();
96
- expect(result.find((token) => token.symbol === 'USDC')).toBeDefined();
97
- expect(result.find((token) => token.symbol === 'WBTC')).toBeDefined();
98
- expect(result.find((token) => token.symbol === 'LINK')).toBeDefined();
99
- });
100
- });
@@ -1,18 +0,0 @@
1
- import type { NetworkContractToken } from '@avalabs/vm-module-types';
2
- import { rpcErrors } from '@metamask/rpc-errors';
3
-
4
- export async function getTokens({
5
- chainId,
6
- proxyApiUrl,
7
- }: {
8
- chainId: number;
9
- proxyApiUrl: string;
10
- }): Promise<NetworkContractToken[]> {
11
- const response = await fetch(`${proxyApiUrl}/tokens?evmChainId=${chainId}`);
12
-
13
- if (!response.ok) {
14
- throw rpcErrors.internal(`Failed to fetch tokens for chainId ${chainId}`);
15
- }
16
-
17
- return response.json();
18
- }
@@ -1,51 +0,0 @@
1
- import type { Erc20Tx } from '@avalabs/etherscan-sdk';
2
- import { TokenType, TransactionType, type Transaction } from '@avalabs/vm-module-types';
3
- import { balanceToDisplayValue } from '@avalabs/utils-sdk';
4
- import { getExplorerAddressByNetwork } from '../../utils/get-explorer-address-by-network';
5
- import { BN } from 'bn.js';
6
-
7
- export function convertTransactionERC20({
8
- tx,
9
- address,
10
- explorerUrl,
11
- chainId,
12
- }: {
13
- tx: Erc20Tx;
14
- address: string;
15
- chainId: number;
16
- explorerUrl: string;
17
- }): Transaction {
18
- const isSender = tx.from.toLowerCase() === address.toLowerCase();
19
- const timestamp = parseInt(tx.timeStamp) * 1000;
20
- const decimals = parseInt(tx.tokenDecimal);
21
- const amountBN = new BN(tx.value);
22
- const amountDisplayValue = balanceToDisplayValue(amountBN, decimals);
23
- const { from, to, gasPrice, gasUsed, hash, tokenDecimal, tokenName, tokenSymbol } = tx;
24
- const txType = isSender ? TransactionType.SEND : TransactionType.RECEIVE;
25
- const explorerLink = getExplorerAddressByNetwork(explorerUrl, hash);
26
-
27
- return {
28
- isIncoming: !isSender,
29
- isOutgoing: isSender,
30
- isContractCall: false,
31
- timestamp,
32
- hash,
33
- isSender,
34
- from,
35
- to,
36
- tokens: [
37
- {
38
- decimal: tokenDecimal,
39
- name: tokenName,
40
- symbol: tokenSymbol,
41
- type: TokenType.ERC20,
42
- amount: amountDisplayValue,
43
- },
44
- ],
45
- gasUsed,
46
- gasPrice,
47
- chainId: chainId.toString(),
48
- txType,
49
- explorerLink,
50
- };
51
- }