@avalabs/evm-module 0.0.23 → 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 (87) hide show
  1. package/LICENSE +9 -0
  2. package/package.json +7 -2
  3. package/.turbo/turbo-build.log +0 -22
  4. package/.turbo/turbo-lint.log +0 -4
  5. package/.turbo/turbo-test.log +0 -119
  6. package/CHANGELOG.md +0 -174
  7. package/jest.config.js +0 -9
  8. package/src/constants.ts +0 -1
  9. package/src/contracts/openzeppelin/ERC1155.ts +0 -440
  10. package/src/contracts/openzeppelin/ERC20.ts +0 -330
  11. package/src/contracts/openzeppelin/ERC721.ts +0 -420
  12. package/src/contracts/openzeppelin/common.ts +0 -131
  13. package/src/contracts/openzeppelin/factories/ERC1155__factory.ts +0 -388
  14. package/src/contracts/openzeppelin/factories/ERC20__factory.ts +0 -353
  15. package/src/contracts/openzeppelin/factories/ERC721__factory.ts +0 -413
  16. package/src/contracts/openzeppelin/factories/index.ts +0 -6
  17. package/src/contracts/openzeppelin/index.ts +0 -10
  18. package/src/env.ts +0 -25
  19. package/src/handlers/eth-send-transaction/eth-send-transaction.test.ts +0 -572
  20. package/src/handlers/eth-send-transaction/eth-send-transaction.ts +0 -189
  21. package/src/handlers/eth-send-transaction/schema.test.ts +0 -240
  22. package/src/handlers/eth-send-transaction/schema.ts +0 -20
  23. package/src/handlers/eth-sign/eth-sign.test.ts +0 -359
  24. package/src/handlers/eth-sign/eth-sign.ts +0 -158
  25. package/src/handlers/eth-sign/schemas/eth-sign-typed-data.ts +0 -65
  26. package/src/handlers/eth-sign/schemas/eth-sign.ts +0 -9
  27. package/src/handlers/eth-sign/schemas/parse-request-params/fixture.ts +0 -47
  28. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.test.ts +0 -284
  29. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.ts +0 -94
  30. package/src/handlers/eth-sign/schemas/personal-sign.ts +0 -12
  31. package/src/handlers/eth-sign/schemas/shared.ts +0 -5
  32. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.test.ts +0 -29
  33. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.ts +0 -134
  34. package/src/handlers/eth-sign/utils/is-typed-data-valid.ts +0 -26
  35. package/src/handlers/eth-sign/utils/typeguards.ts +0 -10
  36. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.test.ts +0 -90
  37. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.ts +0 -23
  38. package/src/handlers/get-address/get-address.ts +0 -26
  39. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +0 -77
  40. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +0 -81
  41. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.test.ts +0 -96
  42. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.ts +0 -53
  43. package/src/handlers/get-balances/get-balances.test.ts +0 -248
  44. package/src/handlers/get-balances/get-balances.ts +0 -123
  45. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +0 -71
  46. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +0 -106
  47. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.test.ts +0 -59
  48. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.ts +0 -48
  49. package/src/handlers/get-network-fee/get-network-fee.test.ts +0 -43
  50. package/src/handlers/get-network-fee/get-network-fee.ts +0 -59
  51. package/src/handlers/get-tokens/get-tokens.test.ts +0 -100
  52. package/src/handlers/get-tokens/get-tokens.ts +0 -18
  53. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.test.ts +0 -53
  54. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.ts +0 -49
  55. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.test.ts +0 -57
  56. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.ts +0 -57
  57. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.test.ts +0 -116
  58. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.ts +0 -73
  59. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/convert-transaction.ts +0 -47
  60. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-nft-metadata.ts +0 -35
  61. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-sender-info.ts +0 -38
  62. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tokens.ts +0 -106
  63. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transaction-from-glacier.test.ts +0 -222
  64. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transactions-from-glacier.ts +0 -62
  65. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tx-type.ts +0 -52
  66. package/src/handlers/get-transaction-history/get-transaction-history.test.ts +0 -57
  67. package/src/handlers/get-transaction-history/get-transaction-history.ts +0 -55
  68. package/src/handlers/get-transaction-history/utils/get-explorer-address-by-network.ts +0 -7
  69. package/src/handlers/get-transaction-history/utils/get-small-image-for-nft.ts +0 -16
  70. package/src/handlers/get-transaction-history/utils/ipfs-resolver-with-fallback.ts +0 -18
  71. package/src/handlers/get-transaction-history/utils/is-ethereum-chain-id.ts +0 -15
  72. package/src/handlers/get-transaction-history/utils/resolve.ts +0 -7
  73. package/src/index.ts +0 -3
  74. package/src/module.ts +0 -141
  75. package/src/services/glacier-service/glacier-service.ts +0 -238
  76. package/src/types.ts +0 -25
  77. package/src/utils/estimate-gas-limit.ts +0 -27
  78. package/src/utils/get-chain-id.ts +0 -12
  79. package/src/utils/get-nonce.ts +0 -11
  80. package/src/utils/get-provider.ts +0 -46
  81. package/src/utils/parse-erc20-transaction-type.ts +0 -35
  82. package/src/utils/process-transaction-simulation.test.ts +0 -105
  83. package/src/utils/process-transaction-simulation.ts +0 -293
  84. package/src/utils/scan-transaction.ts +0 -63
  85. package/tsconfig.jest.json +0 -7
  86. package/tsconfig.json +0 -14
  87. package/tsup.config.ts +0 -4
@@ -1,123 +0,0 @@
1
- import {
2
- type GetBalancesParams,
3
- type Storage,
4
- TokenType,
5
- type NetworkContractToken,
6
- type ERC20Token,
7
- type TokenWithBalanceEVM,
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
- type GetEvmBalancesResponse = Record<string, Record<string, TokenWithBalanceEVM>>;
19
-
20
- export const getBalances = async ({
21
- addresses,
22
- currency,
23
- network,
24
- proxyApiUrl,
25
- customTokens = [],
26
- storage,
27
- glacierService,
28
- }: GetBalancesParams & {
29
- proxyApiUrl: string;
30
- glacierService: EvmGlacierService;
31
- storage?: Storage;
32
- }): Promise<GetEvmBalancesResponse> => {
33
- const chainId = network.chainId;
34
- const isNetworkSupported = await glacierService.isNetworkSupported(network.chainId);
35
- const isHealthy = glacierService.isHealthy();
36
-
37
- let balances = [];
38
- if (isHealthy && isNetworkSupported) {
39
- balances = await Promise.allSettled(
40
- addresses.map(async (address) => {
41
- const nativeToken = await getNativeTokenBalancesFromGlacier({
42
- address,
43
- currency,
44
- chainId,
45
- glacierService,
46
- coingeckoId: network.pricingProviders?.coingecko.nativeTokenId ?? '',
47
- });
48
-
49
- const erc20Tokens = await getErc20BalancesFromGlacier({
50
- customTokens: customTokens.filter(isERC20Token),
51
- glacierService,
52
- currency,
53
- chainId,
54
- address,
55
- });
56
-
57
- return {
58
- address,
59
- balances: {
60
- [nativeToken.symbol]: nativeToken,
61
- ...erc20Tokens,
62
- },
63
- };
64
- }),
65
- );
66
- } else {
67
- const tokenService = new TokenService({ storage, proxyApiUrl });
68
- const tokens = await getTokens({ chainId: Number(chainId), proxyApiUrl });
69
- const allTokens = [...tokens, ...customTokens];
70
- const provider = getProvider({
71
- chainId,
72
- chainName: network.chainName,
73
- rpcUrl: network.rpcUrl,
74
- multiContractAddress: network.utilityAddresses?.multicall,
75
- });
76
-
77
- balances = await Promise.allSettled(
78
- addresses.map(async (address) => {
79
- const nativeToken = await getNativeTokenBalances({
80
- network,
81
- tokenService,
82
- address,
83
- currency,
84
- provider,
85
- });
86
-
87
- const erc20Tokens = await getErc20Balances({
88
- provider,
89
- network,
90
- tokenService,
91
- address,
92
- currency,
93
- tokens: allTokens.filter(isERC20Token),
94
- });
95
-
96
- return {
97
- address,
98
- balances: {
99
- [nativeToken.symbol]: nativeToken,
100
- ...erc20Tokens,
101
- },
102
- };
103
- }),
104
- );
105
- }
106
-
107
- const filterBalances = balances.reduce((acc, result) => {
108
- if (result.status === 'rejected') {
109
- return acc;
110
- }
111
-
112
- return {
113
- ...acc,
114
- [result.value.address]: result.value.balances,
115
- };
116
- }, {} as GetEvmBalancesResponse);
117
-
118
- return filterBalances;
119
- };
120
-
121
- function isERC20Token(token: NetworkContractToken): token is ERC20Token {
122
- return token.type === TokenType.ERC20;
123
- }
@@ -1,71 +0,0 @@
1
- import { getErc20Balances } from './get-erc20-balances';
2
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
3
-
4
- describe('get-erc20-balances', () => {
5
- it('should return erc20 token balances', async () => {
6
- const mockGlacierService: EvmGlacierService = {
7
- ...expect.any(Object),
8
- listErc20Balances: jest.fn().mockResolvedValue({
9
- erc20TokenBalances: [
10
- {
11
- address: '0x123',
12
- name: 'Ethereum',
13
- symbol: 'ETH',
14
- decimals: 18,
15
- logoUri: 'https://example.com/logo.png',
16
- ercType: 'ERC20',
17
- price: {
18
- currency: 'USD',
19
- value: 1000,
20
- },
21
- chainId: '123',
22
- balance: '1000000000000000000',
23
- balanceValue: {
24
- currency: 'USD',
25
- value: 1000,
26
- },
27
- },
28
- ],
29
- }),
30
- };
31
- const balance = getErc20Balances({
32
- glacierService: mockGlacierService,
33
- currency: 'USD',
34
- chainId: 123,
35
- address: '0x123',
36
- customTokens: [],
37
- });
38
-
39
- expect(balance).resolves.toEqual({
40
- '0x123': {
41
- chainId: 123,
42
- address: '0x123',
43
- name: 'Ethereum',
44
- symbol: 'ETH',
45
- decimals: 18,
46
- logoUri: 'https://example.com/logo.png',
47
- balance: 1000000000000000000n,
48
- balanceCurrencyDisplayValue: '1000',
49
- balanceDisplayValue: '1',
50
- balanceInCurrency: 1000,
51
- priceInCurrency: 1000,
52
- type: 'ERC20',
53
- },
54
- });
55
- });
56
-
57
- it('should return native token object without balance data', async () => {
58
- const mockGlacierService: EvmGlacierService = {
59
- ...expect.any(Object),
60
- listErc20Balances: jest.fn().mockRejectedValue(new Error('Failed to get erc20 balance')),
61
- };
62
- const balance = getErc20Balances({
63
- glacierService: mockGlacierService,
64
- currency: 'USD',
65
- chainId: 123,
66
- address: '0x123',
67
- customTokens: [],
68
- });
69
- expect(balance).rejects.toEqual(new Error('Failed to get erc20 balance'));
70
- });
71
- });
@@ -1,106 +0,0 @@
1
- import { TokenUnit } from '@avalabs/utils-sdk';
2
- import {
3
- type ERC20Token,
4
- TokenType,
5
- type TokenWithBalanceEVM,
6
- type TokenWithBalanceERC20,
7
- } from '@avalabs/vm-module-types';
8
- import { CurrencyCode, Erc20TokenBalance } from '@avalabs/glacier-sdk';
9
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
10
- import { DEFAULT_DECIMALS } from '../../../constants';
11
-
12
- export const getErc20Balances = async ({
13
- glacierService,
14
- currency,
15
- chainId,
16
- address,
17
- customTokens,
18
- }: {
19
- glacierService: EvmGlacierService;
20
- address: string;
21
- currency: string;
22
- chainId: number;
23
- customTokens: ERC20Token[];
24
- }): Promise<Record<string, TokenWithBalanceEVM>> => {
25
- const tokensWithBalance: TokenWithBalanceERC20[] = [];
26
- /**
27
- * Load all pages to make sure we have all the tokens with balances
28
- */
29
- let nextPageToken: string | undefined;
30
- do {
31
- const response = await glacierService.listErc20Balances({
32
- chainId: chainId.toString(),
33
- address,
34
- currency: currency.toLocaleLowerCase() as CurrencyCode,
35
- // glacier has a cap on page size of 100
36
- pageSize: 100,
37
- pageToken: nextPageToken,
38
- });
39
-
40
- tokensWithBalance.push(
41
- ...convertErc20TokenWithBalanceToTokenWithBalance(response.erc20TokenBalances, Number(chainId)),
42
- );
43
- nextPageToken = response.nextPageToken;
44
- } while (nextPageToken);
45
-
46
- /**
47
- * Glacier doesnt return tokens without balances so we need to polyfill that list
48
- * from our own list of tokens. We just set the balance to 0, these zero balance
49
- * tokens are only used for swap, bridge and tx parsing.
50
- */
51
- return [
52
- ...convertErc20TokenToTokenWithBalance(customTokens),
53
- ...tokensWithBalance, // this needs to be second in the list so it overwrites its zero balance counterpart if there is one
54
- ].reduce(
55
- (acc, token) => {
56
- return { ...acc, [token.address.toLowerCase()]: token };
57
- },
58
- {} as Record<string, TokenWithBalanceEVM>,
59
- );
60
- };
61
-
62
- const convertErc20TokenToTokenWithBalance = (tokens: ERC20Token[]): TokenWithBalanceERC20[] => {
63
- return tokens.map((token) => {
64
- return {
65
- ...token,
66
- decimals: token.decimals ?? DEFAULT_DECIMALS,
67
- type: TokenType.ERC20,
68
- balance: 0n,
69
- balanceInCurrency: 0,
70
- balanceDisplayValue: '0',
71
- balanceCurrencyDisplayValue: '0',
72
- priceInCurrency: 0,
73
- marketCap: 0,
74
- change24: 0,
75
- vol24: 0,
76
- };
77
- });
78
- };
79
-
80
- const convertErc20TokenWithBalanceToTokenWithBalance = (
81
- tokenBalances: Erc20TokenBalance[],
82
- chainId: number,
83
- ): TokenWithBalanceERC20[] => {
84
- return tokenBalances.map((token: Erc20TokenBalance): TokenWithBalanceERC20 => {
85
- const balance = new TokenUnit(token.balance, token.decimals, token.symbol);
86
- const balanceDisplayValue = balance.toDisplay();
87
- const balanceCurrencyDisplayValue = token.balanceValue?.value.toString();
88
- const priceInCurrency = token.price?.value;
89
- const balanceInCurrency = priceInCurrency ? Number(balance.mul(priceInCurrency).toDisplay(2)) : undefined;
90
-
91
- return {
92
- chainId,
93
- address: token.address,
94
- name: token.name,
95
- symbol: token.symbol,
96
- decimals: token.decimals,
97
- logoUri: token.logoUri,
98
- balance: balance.toSubUnit(),
99
- balanceCurrencyDisplayValue,
100
- balanceDisplayValue,
101
- balanceInCurrency,
102
- priceInCurrency,
103
- type: TokenType.ERC20,
104
- };
105
- });
106
- };
@@ -1,59 +0,0 @@
1
- import { getNativeTokenBalances } from './get-native-token-balances';
2
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
3
-
4
- describe('get-native-token-balances', () => {
5
- it('should return native token balances', async () => {
6
- const mockGlacierService: EvmGlacierService = {
7
- ...expect.any(Object),
8
- getNativeBalance: jest.fn().mockResolvedValue({
9
- nativeTokenBalance: {
10
- balance: 1000000000000000000n,
11
- decimals: 18,
12
- name: 'Ethereum',
13
- symbol: 'ETH',
14
- price: {
15
- value: 1000,
16
- },
17
- logoUri: 'https://example.com/logo.png',
18
- },
19
- }),
20
- };
21
-
22
- const balance = getNativeTokenBalances({
23
- address: '0x123',
24
- currency: 'USD',
25
- chainId: 123,
26
- glacierService: mockGlacierService,
27
- coingeckoId: '',
28
- });
29
-
30
- expect(balance).resolves.toEqual({
31
- name: 'Ethereum',
32
- symbol: 'ETH',
33
- decimals: 18,
34
- type: 'NATIVE',
35
- logoUri: 'https://example.com/logo.png',
36
- balance: 1000000000000000000n,
37
- balanceDisplayValue: '1',
38
- balanceInCurrency: 1000,
39
- balanceCurrencyDisplayValue: '1000.00',
40
- priceInCurrency: 1000,
41
- coingeckoId: '',
42
- });
43
- });
44
-
45
- it('should return native token object without balance data', async () => {
46
- const mockGlacierService: EvmGlacierService = {
47
- ...expect.any(Object),
48
- getNativeBalance: jest.fn().mockRejectedValue(new Error('Failed to get native balance')),
49
- };
50
- const balance = getNativeTokenBalances({
51
- address: '0x123',
52
- currency: 'USD',
53
- chainId: 123,
54
- glacierService: mockGlacierService,
55
- coingeckoId: '',
56
- });
57
- expect(balance).rejects.toEqual(new Error('Failed to get native balance'));
58
- });
59
- });
@@ -1,48 +0,0 @@
1
- import type { CurrencyCode } from '@avalabs/glacier-sdk';
2
- import { TokenType, type NetworkTokenWithBalance } from '@avalabs/vm-module-types';
3
- import { TokenUnit } from '@avalabs/utils-sdk';
4
- import type { EvmGlacierService } from '../../../services/glacier-service/glacier-service';
5
-
6
- export const getNativeTokenBalances = async ({
7
- address,
8
- currency,
9
- chainId,
10
- glacierService,
11
- coingeckoId,
12
- }: {
13
- chainId: number;
14
- address: string;
15
- currency: string;
16
- coingeckoId: string;
17
- glacierService: EvmGlacierService;
18
- }): Promise<NetworkTokenWithBalance> => {
19
- const nativeBalance = await glacierService.getNativeBalance({
20
- chainId: chainId.toString(),
21
- address,
22
- currency: currency.toLocaleLowerCase() as CurrencyCode,
23
- });
24
- const nativeTokenBalance = nativeBalance.nativeTokenBalance;
25
- const balanceTokenUnit = new TokenUnit(
26
- nativeTokenBalance.balance,
27
- nativeTokenBalance.decimals,
28
- nativeTokenBalance.symbol,
29
- );
30
- const balanceDisplayValue = balanceTokenUnit.toDisplay();
31
- const priceInCurrency = nativeTokenBalance.price?.value;
32
- const balanceCurrencyDisplayValue = priceInCurrency ? balanceTokenUnit.mul(priceInCurrency).toDisplay(2) : undefined;
33
- const balanceInCurrency = balanceCurrencyDisplayValue ? Number(balanceCurrencyDisplayValue) : undefined;
34
-
35
- return {
36
- name: nativeTokenBalance.name,
37
- symbol: nativeTokenBalance.symbol,
38
- decimals: nativeTokenBalance.decimals,
39
- type: TokenType.NATIVE,
40
- logoUri: nativeTokenBalance.logoUri,
41
- balance: balanceTokenUnit.toSubUnit(),
42
- balanceDisplayValue,
43
- balanceInCurrency,
44
- balanceCurrencyDisplayValue,
45
- priceInCurrency,
46
- coingeckoId,
47
- };
48
- };
@@ -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,53 +0,0 @@
1
- import { TokenType, TransactionType } from '@avalabs/vm-module-types';
2
- import { convertTransactionERC20 } from './convert-transaction-erc20';
3
- import type { Erc20Tx } from '@avalabs/etherscan-sdk';
4
-
5
- describe('convertTransactionERC20 ', () => {
6
- it('should correctly convert ERC20 transaction data', () => {
7
- const tx = {
8
- from: '0xSenderAddress',
9
- to: '0xReceiverAddress',
10
- timeStamp: '1625794800', // Unix timestamp in seconds
11
- value: '1000000000000000000', // in wei
12
- gasPrice: '20000000000', // in wei
13
- gasUsed: '21000',
14
- hash: '0xTransactionHash',
15
- tokenDecimal: '18',
16
- tokenName: 'MyToken',
17
- tokenSymbol: 'MTK',
18
- } as Erc20Tx;
19
-
20
- const address = '0xSenderAddress';
21
- const explorerUrl = 'https://explorer.example.com';
22
- const chainId = 1;
23
-
24
- const expected = {
25
- isIncoming: false,
26
- isOutgoing: true,
27
- isContractCall: false,
28
- timestamp: 1625794800000, // Converted timestamp in milliseconds
29
- hash: '0xTransactionHash',
30
- isSender: true,
31
- from: '0xSenderAddress',
32
- to: '0xReceiverAddress',
33
- tokens: [
34
- {
35
- decimal: '18',
36
- name: 'MyToken',
37
- symbol: 'MTK',
38
- type: TokenType.ERC20,
39
- amount: '1',
40
- },
41
- ],
42
- gasUsed: '21000',
43
- gasPrice: '20000000000',
44
- chainId: '1',
45
- txType: TransactionType.SEND,
46
- explorerLink: 'https://explorer.example.com/tx/0xTransactionHash', // Assuming this is how the explorer link is formatted
47
- };
48
-
49
- const result = convertTransactionERC20({ tx, address, explorerUrl, chainId });
50
-
51
- expect(result).toEqual(expected);
52
- });
53
- });