@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,238 +0,0 @@
1
- import {
2
- CurrencyCode,
3
- Erc1155Token,
4
- Erc721Token,
5
- type GetNativeBalanceResponse,
6
- Glacier,
7
- type ListErc1155BalancesResponse,
8
- type ListErc20BalancesResponse,
9
- type ListErc721BalancesResponse,
10
- } from '@avalabs/glacier-sdk';
11
-
12
- class GlacierUnhealthyError extends Error {
13
- override message = 'Glacier is unhealthy. Try again later.';
14
- }
15
-
16
- export class EvmGlacierService {
17
- glacierSdk: Glacier;
18
- isGlacierHealthy = true;
19
- supportedChainIds: string[] = [];
20
-
21
- constructor({ glacierApiUrl }: { glacierApiUrl: string }) {
22
- this.glacierSdk = new Glacier({ BASE: glacierApiUrl });
23
- /**
24
- * This is for performance, basically we just cache the health of glacier every 5 seconds and
25
- * go off of that instead of every request
26
- */
27
- this.getSupportedChainIds().catch(() => {
28
- // Noop. It will be retried by .isSupportedNetwork calls upon unlocking if necessary.
29
- });
30
- }
31
-
32
- isHealthy = (): boolean => this.isGlacierHealthy;
33
-
34
- setGlacierToUnhealthy(): void {
35
- this.isGlacierHealthy = false;
36
- setTimeout(
37
- () => {
38
- this.isGlacierHealthy = true;
39
- },
40
- 5 * 60 * 1000,
41
- ); // 5 minutes
42
- }
43
-
44
- async isNetworkSupported(chainId: number): Promise<boolean> {
45
- const chainIds = await this.getSupportedChainIds();
46
- return chainIds.some((id) => id === chainId.toString());
47
- }
48
-
49
- async getSupportedChainIds(): Promise<string[]> {
50
- if (this.supportedChainIds.length) {
51
- return this.supportedChainIds;
52
- }
53
-
54
- try {
55
- const supportedChains = await this.glacierSdk.evmChains.supportedChains({});
56
- this.supportedChainIds = supportedChains.chains.map((chain) => chain.chainId);
57
- return this.supportedChainIds;
58
- } catch {
59
- return [];
60
- }
61
- }
62
-
63
- async reindexNft({
64
- address,
65
- chainId,
66
- tokenId,
67
- }: {
68
- address: string;
69
- chainId: string;
70
- tokenId: string;
71
- }): Promise<void> {
72
- try {
73
- await this.glacierSdk.nfTs.reindexNft({
74
- address,
75
- chainId,
76
- tokenId,
77
- });
78
- } catch (error) {
79
- if (error instanceof GlacierUnhealthyError) {
80
- this.setGlacierToUnhealthy();
81
- }
82
- throw error;
83
- }
84
- }
85
-
86
- async getTokenDetails({
87
- address,
88
- chainId,
89
- tokenId,
90
- }: {
91
- address: string;
92
- chainId: string;
93
- tokenId: string;
94
- }): Promise<Erc721Token | Erc1155Token> {
95
- try {
96
- return this.glacierSdk.nfTs.getTokenDetails({
97
- address,
98
- chainId,
99
- tokenId,
100
- });
101
- } catch (error) {
102
- if (error instanceof GlacierUnhealthyError) {
103
- this.setGlacierToUnhealthy();
104
- }
105
- throw error;
106
- }
107
- }
108
-
109
- async getNativeBalance({
110
- chainId,
111
- address,
112
- currency,
113
- }: {
114
- chainId: string;
115
- address: string;
116
- currency: CurrencyCode;
117
- }): Promise<GetNativeBalanceResponse> {
118
- try {
119
- return this.glacierSdk.evmBalances.getNativeBalance({
120
- chainId,
121
- address,
122
- currency: currency.toLocaleLowerCase() as CurrencyCode,
123
- });
124
- } catch (error) {
125
- if (error instanceof GlacierUnhealthyError) {
126
- this.setGlacierToUnhealthy();
127
- }
128
- throw error;
129
- }
130
- }
131
-
132
- async listErc721Balances({
133
- chainId,
134
- address,
135
- pageSize,
136
- pageToken,
137
- }: {
138
- chainId: string;
139
- address: string;
140
- pageSize: number;
141
- pageToken?: string;
142
- }): Promise<ListErc721BalancesResponse> {
143
- try {
144
- return this.glacierSdk.evmBalances.listErc721Balances({
145
- chainId,
146
- address,
147
- pageSize,
148
- pageToken,
149
- });
150
- } catch (error) {
151
- if (error instanceof GlacierUnhealthyError) {
152
- this.setGlacierToUnhealthy();
153
- }
154
- throw error;
155
- }
156
- }
157
-
158
- async listErc1155Balances({
159
- chainId,
160
- address,
161
- pageSize,
162
- pageToken,
163
- }: {
164
- chainId: string;
165
- address: string;
166
- pageSize: number;
167
- pageToken?: string;
168
- }): Promise<ListErc1155BalancesResponse> {
169
- try {
170
- return this.glacierSdk.evmBalances.listErc1155Balances({
171
- chainId,
172
- address,
173
- pageSize,
174
- pageToken,
175
- });
176
- } catch (error) {
177
- if (error instanceof GlacierUnhealthyError) {
178
- this.setGlacierToUnhealthy();
179
- }
180
- throw error;
181
- }
182
- }
183
-
184
- async listErc20Balances({
185
- chainId,
186
- address,
187
- currency,
188
- pageSize,
189
- pageToken,
190
- }: {
191
- chainId: string;
192
- address: string;
193
- currency: CurrencyCode;
194
- pageSize: number;
195
- pageToken?: string;
196
- }): Promise<ListErc20BalancesResponse> {
197
- try {
198
- return this.glacierSdk.evmBalances.listErc20Balances({
199
- chainId,
200
- address,
201
- currency: currency.toLocaleLowerCase() as CurrencyCode,
202
- pageSize,
203
- pageToken,
204
- });
205
- } catch (error) {
206
- if (error instanceof GlacierUnhealthyError) {
207
- this.setGlacierToUnhealthy();
208
- }
209
- throw error;
210
- }
211
- }
212
-
213
- async listTransactions({
214
- chainId,
215
- address,
216
- pageToken,
217
- pageSize,
218
- }: {
219
- chainId: string;
220
- address: string;
221
- pageToken?: string;
222
- pageSize?: number;
223
- }) {
224
- try {
225
- return this.glacierSdk.evmTransactions.listTransactions({
226
- chainId,
227
- address,
228
- pageToken,
229
- pageSize,
230
- });
231
- } catch (error) {
232
- if (error instanceof GlacierUnhealthyError) {
233
- this.setGlacierToUnhealthy();
234
- }
235
- throw error;
236
- }
237
- }
238
- }
package/src/types.ts DELETED
@@ -1,25 +0,0 @@
1
- import { TransactionType } from '@avalabs/vm-module-types';
2
-
3
- export const NonContractCallTypes = [TransactionType.SEND, TransactionType.RECEIVE, TransactionType.TRANSFER];
4
-
5
- export type TransactionParams = {
6
- from: string;
7
- to: string;
8
- data?: string;
9
- value?: string;
10
- gas?: string;
11
- gasPrice?: string;
12
- maxFeePerGas?: string;
13
- maxPriorityFeePerGas?: string;
14
- nonce?: string;
15
- chainId?: string;
16
- };
17
-
18
- export enum ERC20TransactionType {
19
- TOTAL_SUPPLY = 'totalSupply',
20
- BALANCE_OF = 'balanceOf',
21
- TRANSFER = 'transfer',
22
- TRANSFER_FROM = 'transferFrom',
23
- APPROVE = 'approve',
24
- ALLOWANCE = 'allowance',
25
- }
@@ -1,27 +0,0 @@
1
- import { JsonRpcBatchInternal } from '@avalabs/wallets-sdk';
2
- import type { BigNumberish } from 'ethers';
3
-
4
- export const estimateGasLimit = async ({
5
- transactionParams: { from, to, data, value },
6
- provider,
7
- }: {
8
- transactionParams: {
9
- from: string;
10
- to: string;
11
- data?: string;
12
- value?: BigNumberish;
13
- };
14
- provider: JsonRpcBatchInternal;
15
- }): Promise<number> => {
16
- const nonce = await provider.getTransactionCount(from);
17
-
18
- return Number(
19
- await provider.estimateGas({
20
- from,
21
- to,
22
- nonce,
23
- data,
24
- value,
25
- }),
26
- );
27
- };
@@ -1,12 +0,0 @@
1
- import type { Caip2ChainId } from '@avalabs/vm-module-types';
2
- import { rpcErrors } from '@metamask/rpc-errors';
3
-
4
- export const getChainId = (caip2ChainId: Caip2ChainId): number => {
5
- const chainId = caip2ChainId.split(':')[1];
6
-
7
- if (!chainId || isNaN(Number(chainId))) {
8
- throw rpcErrors.invalidParams('Invalid chainId');
9
- }
10
-
11
- return Number(chainId);
12
- };
@@ -1,11 +0,0 @@
1
- import { JsonRpcBatchInternal } from '@avalabs/wallets-sdk';
2
-
3
- export const getNonce = async ({
4
- from,
5
- provider,
6
- }: {
7
- from: string;
8
- provider: JsonRpcBatchInternal;
9
- }): Promise<number> => {
10
- return provider.getTransactionCount(from);
11
- };
@@ -1,46 +0,0 @@
1
- import { JsonRpcBatchInternal } from '@avalabs/wallets-sdk';
2
- import { Network as EVMNetwork } from 'ethers';
3
-
4
- // this key is only needed in development to bypass rate limit
5
- // it should never be used in production
6
- const GLACIER_API_KEY = process.env.GLACIER_API_KEY;
7
-
8
- type ProviderParams = {
9
- chainId: number;
10
- chainName: string;
11
- rpcUrl: string;
12
- multiContractAddress?: string;
13
- pollingInterval?: number;
14
- };
15
-
16
- export const getProvider = (params: ProviderParams): JsonRpcBatchInternal => {
17
- const { chainId, chainName, rpcUrl, multiContractAddress, pollingInterval = 2000 } = params;
18
-
19
- const provider = new JsonRpcBatchInternal(
20
- { maxCalls: 40, multiContractAddress },
21
- addGlacierAPIKeyIfNeeded(rpcUrl, GLACIER_API_KEY),
22
- new EVMNetwork(chainName, chainId),
23
- );
24
-
25
- provider.pollingInterval = pollingInterval;
26
-
27
- return provider;
28
- };
29
-
30
- // RPC urls returned in the token list are always using the production URL
31
- const knownHosts = ['glacier-api.avax.network', 'proxy-api.avax.network'];
32
-
33
- /**
34
- * Glacier needs an API key for development, this adds the key if needed.
35
- */
36
- export function addGlacierAPIKeyIfNeeded(url: string, glacierApiKey?: string): string {
37
- const urlObj = new URL(url);
38
-
39
- if (glacierApiKey && knownHosts.includes(urlObj.hostname)) {
40
- const search_params = urlObj.searchParams; // copy, does not update the URL
41
- search_params.set('token', glacierApiKey);
42
- urlObj.search = search_params.toString();
43
- return urlObj.toString();
44
- }
45
- return url;
46
- }
@@ -1,35 +0,0 @@
1
- import { Interface } from 'ethers';
2
- import ERC20 from '@openzeppelin/contracts/build/contracts/ERC20.json';
3
- import { ERC20TransactionType } from '../types';
4
-
5
- export const parseERC20TransactionType = (transaction: {
6
- data?: string;
7
- value?: string;
8
- }): ERC20TransactionType | undefined => {
9
- if (!transaction.data) {
10
- return undefined;
11
- }
12
-
13
- try {
14
- const contractInterface = new Interface(ERC20.abi);
15
-
16
- const description = contractInterface.parseTransaction({
17
- data: transaction.data,
18
- value: transaction.value,
19
- });
20
-
21
- const functionName = description?.name ?? description?.fragment?.name;
22
-
23
- if (functionName && isERC20TransactionType(functionName)) {
24
- return functionName;
25
- }
26
-
27
- return undefined;
28
- } catch (e) {
29
- return undefined;
30
- }
31
- };
32
-
33
- function isERC20TransactionType(value: string): value is ERC20TransactionType {
34
- return Object.values(ERC20TransactionType).includes(value as ERC20TransactionType);
35
- }
@@ -1,105 +0,0 @@
1
- import Blockaid from '@blockaid/client';
2
- import { processBalanceChange } from './process-transaction-simulation';
3
-
4
- jest.mock('@blockaid/client', () => {
5
- return jest.fn().mockImplementation(() => {
6
- return {};
7
- });
8
- });
9
-
10
- describe('processBalanceChange', () => {
11
- it('should sort asset diffs correctly within ins and outs', () => {
12
- const assetDiffs: Blockaid.AssetDiff[] = [
13
- {
14
- asset: {
15
- type: 'ERC20',
16
- address: '0xTokenAddress1',
17
- name: 'TokenName1',
18
- symbol: 'TKN1',
19
- decimals: 18,
20
- logo_url: 'logo_url1',
21
- },
22
- in: [
23
- { value: '1', usd_price: '1', raw_value: '0x1' },
24
- { value: '2', usd_price: '2', raw_value: '0x2' },
25
- ],
26
- out: [
27
- { value: '3', usd_price: '3', raw_value: '0x3' },
28
- { value: '1', usd_price: '1', raw_value: '0x1' },
29
- ],
30
- },
31
- {
32
- asset: {
33
- type: 'ERC20',
34
- address: '0xTokenAddress2',
35
- name: 'TokenName2',
36
- symbol: 'TKN2',
37
- decimals: 18,
38
- logo_url: 'logo_url2',
39
- },
40
- in: [{ value: '4', usd_price: '4', raw_value: '0x4' }],
41
- out: [{ value: '5', usd_price: '5', raw_value: '0x5' }],
42
- },
43
- ];
44
-
45
- const result = processBalanceChange(assetDiffs);
46
-
47
- // Verify sorting logic for 'ins'
48
- expect(result?.ins).toEqual([
49
- {
50
- token: {
51
- type: 'ERC20',
52
- address: '0xTokenAddress2',
53
- name: 'TokenName2',
54
- symbol: 'TKN2',
55
- decimals: 18,
56
- logoUri: 'logo_url2',
57
- },
58
- items: [{ displayValue: '4', usdPrice: '4' }],
59
- },
60
- {
61
- token: {
62
- type: 'ERC20',
63
- address: '0xTokenAddress1',
64
- name: 'TokenName1',
65
- symbol: 'TKN1',
66
- decimals: 18,
67
- logoUri: 'logo_url1',
68
- },
69
- items: [
70
- { displayValue: '1', usdPrice: '1' },
71
- { displayValue: '2', usdPrice: '2' },
72
- ],
73
- },
74
- ]);
75
-
76
- // Verify sorting logic for 'outs'
77
- expect(result?.outs).toEqual([
78
- {
79
- token: {
80
- type: 'ERC20',
81
- address: '0xTokenAddress2',
82
- name: 'TokenName2',
83
- symbol: 'TKN2',
84
- decimals: 18,
85
- logoUri: 'logo_url2',
86
- },
87
- items: [{ displayValue: '5', usdPrice: '5' }],
88
- },
89
- {
90
- token: {
91
- type: 'ERC20',
92
- address: '0xTokenAddress1',
93
- name: 'TokenName1',
94
- symbol: 'TKN1',
95
- decimals: 18,
96
- logoUri: 'logo_url1',
97
- },
98
- items: [
99
- { displayValue: '3', usdPrice: '3' },
100
- { displayValue: '1', usdPrice: '1' },
101
- ],
102
- },
103
- ]);
104
- });
105
- });