@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,62 +0,0 @@
1
- import type { NetworkToken, TransactionHistoryResponse } from '@avalabs/vm-module-types';
2
- import { convertTransaction } from './convert-transaction';
3
- import type { EvmGlacierService } from '../../../../services/glacier-service/glacier-service';
4
-
5
- export const getTransactionsFromGlacier = async ({
6
- chainId,
7
- explorerUrl,
8
- networkToken,
9
- address,
10
- nextPageToken,
11
- offset,
12
- glacierService,
13
- }: {
14
- chainId: number;
15
- explorerUrl: string;
16
- networkToken: NetworkToken;
17
- address: string;
18
- nextPageToken?: string;
19
- offset?: number;
20
- glacierService: EvmGlacierService;
21
- }): Promise<TransactionHistoryResponse> => {
22
- try {
23
- const response = await glacierService.listTransactions({
24
- chainId: chainId.toString(),
25
- address,
26
- pageToken: nextPageToken,
27
- pageSize: offset,
28
- });
29
-
30
- const convertedTxs = await Promise.all(
31
- response.transactions
32
- .filter(
33
- // Currently not showing failed tx
34
- (tranasaction) => tranasaction.nativeTransaction.txStatus === '1',
35
- )
36
- .map((transactions) =>
37
- convertTransaction({
38
- transactions,
39
- explorerUrl,
40
- networkToken,
41
- chainId,
42
- address,
43
- }).then((tx) => tx),
44
- ),
45
- );
46
-
47
- const transactions = convertedTxs.filter(
48
- // Filtering txs with 0 value since there is no change in balance
49
- (transaction) => transaction.tokens.find((token) => Number(token.amount) !== 0),
50
- );
51
-
52
- return {
53
- transactions,
54
- nextPageToken: response.nextPageToken,
55
- };
56
- } catch {
57
- return {
58
- transactions: [],
59
- nextPageToken: '',
60
- };
61
- }
62
- };
@@ -1,52 +0,0 @@
1
- import type { TransactionDetails } from '@avalabs/glacier-sdk';
2
- import { TokenType, TransactionType, type TxToken } from '@avalabs/vm-module-types';
3
- import startCase from 'lodash.startcase';
4
-
5
- export const getTxType = (
6
- { nativeTransaction, erc20Transfers, erc721Transfers }: TransactionDetails,
7
- userAddress: string,
8
- tokens: TxToken[],
9
- ): TransactionType => {
10
- const nativeOnly = !erc20Transfers && !erc721Transfers;
11
- const method = parseRawMethod(nativeTransaction.method?.methodName);
12
-
13
- const address = userAddress.toLowerCase();
14
-
15
- const isSwap = method.toLowerCase().includes('swap');
16
- const isNativeSend = nativeOnly && nativeTransaction.from.address.toLowerCase() === address;
17
- const isNativeReceive = nativeOnly && nativeTransaction.to.address.toLowerCase() === address;
18
- const isNFTPurchase = method === 'Market Buy Orders With Eth' || method === 'Buy NFT';
19
- const isApprove = method === 'Approve';
20
- const isTransfer = method.toLowerCase().includes('transfer');
21
- const isAirdrop = method.toLowerCase().includes('airdrop');
22
- const isUnwrap = method.toLowerCase().includes('unwrap');
23
- const isFillOrder = method === 'Fill Order';
24
- const isNFTSend =
25
- isTransfer && !!tokens[0] && isNFT(tokens[0].type) && tokens[0].from?.address.toLowerCase() === address;
26
- const isNFTReceive =
27
- isTransfer && !!tokens[0] && isNFT(tokens[0].type) && tokens[0].to?.address.toLowerCase() === address;
28
-
29
- if (isSwap) return TransactionType.SWAP;
30
- if (isNativeSend) return TransactionType.SEND;
31
- if (isNativeReceive) return TransactionType.RECEIVE;
32
- if (isNFTPurchase) return TransactionType.NFT_BUY;
33
- if (isApprove) return TransactionType.APPROVE;
34
- if (isNFTSend) return TransactionType.NFT_SEND;
35
- if (isNFTReceive) return TransactionType.NFT_RECEIVE;
36
- if (isTransfer) return TransactionType.TRANSFER;
37
- if (isAirdrop) return TransactionType.AIRDROP;
38
- if (isUnwrap) return TransactionType.UNWRAP;
39
- if (isFillOrder) return TransactionType.FILL_ORDER;
40
- return TransactionType.UNKNOWN;
41
- };
42
-
43
- function isNFT(tokenType: TokenType) {
44
- return tokenType === TokenType.ERC721 || tokenType === TokenType.ERC1155;
45
- }
46
-
47
- const parseRawMethod = (method = ''): string => {
48
- if (method.includes('(')) {
49
- return startCase(method.split('(', 1)[0]);
50
- }
51
- return method;
52
- };
@@ -1,57 +0,0 @@
1
- import { getTransactionHistory } from './get-transaction-history';
2
- import { getTransactionFromEtherscan } from './converters/etherscan-transaction-converter/get-transaction-from-etherscan';
3
- import { getTransactionsFromGlacier } from './converters/evm-transaction-converter/get-transactions-from-glacier';
4
- import type { EvmGlacierService } from '../../services/glacier-service/glacier-service';
5
-
6
- jest.mock('./converters/evm-transaction-converter/get-transactions-from-glacier', () => ({
7
- getTransactionsFromGlacier: jest.fn(),
8
- }));
9
-
10
- jest.mock('./converters/etherscan-transaction-converter/get-transaction-from-etherscan', () => ({
11
- getTransactionFromEtherscan: jest.fn(),
12
- }));
13
-
14
- describe('get-transaction-history', () => {
15
- it('should have called getTransactionFromEtherscan', async () => {
16
- await getTransactionHistory({
17
- glacierService: {} as EvmGlacierService,
18
- chainId: 1,
19
- isTestnet: false,
20
- networkToken: {
21
- name: 'networkToken',
22
- symbol: 'networkToken',
23
- decimals: 1,
24
- description: 'description',
25
- logoUri: 'logoUri',
26
- },
27
- explorerUrl: 'explorerUrl',
28
- address: 'address',
29
- nextPageToken: 'nextPageToken',
30
- offset: 1,
31
- });
32
- expect(getTransactionFromEtherscan).toHaveBeenCalled();
33
- });
34
- it('should have called getTransactionsFromGlacier', async () => {
35
- const mockGlacierService: EvmGlacierService = {
36
- ...expect.any(Object),
37
- isHealthy: jest.fn().mockReturnValue(true),
38
- };
39
- await getTransactionHistory({
40
- glacierService: mockGlacierService,
41
- chainId: 41334,
42
- isTestnet: false,
43
- networkToken: {
44
- name: 'networkToken',
45
- symbol: 'networkToken',
46
- decimals: 1,
47
- description: 'description',
48
- logoUri: 'logoUri',
49
- },
50
- explorerUrl: 'explorerUrl',
51
- address: 'address',
52
- nextPageToken: 'nextPageToken',
53
- offset: 1,
54
- });
55
- expect(getTransactionsFromGlacier).toHaveBeenCalled();
56
- });
57
- });
@@ -1,55 +0,0 @@
1
- import { getTransactionFromEtherscan } from './converters/etherscan-transaction-converter/get-transaction-from-etherscan';
2
- import { isEthereumChainId } from './utils/is-ethereum-chain-id';
3
- import type { NetworkToken, TransactionHistoryResponse } from '@avalabs/vm-module-types';
4
- import { getTransactionsFromGlacier } from './converters/evm-transaction-converter/get-transactions-from-glacier';
5
- import type { EvmGlacierService } from '../../services/glacier-service/glacier-service';
6
-
7
- export const getTransactionHistory = async ({
8
- chainId,
9
- isTestnet,
10
- networkToken,
11
- explorerUrl,
12
- address,
13
- nextPageToken,
14
- offset,
15
- glacierService,
16
- }: {
17
- chainId: number;
18
- isTestnet?: boolean;
19
- networkToken: NetworkToken;
20
- explorerUrl: string;
21
- address: string;
22
- nextPageToken?: string;
23
- offset?: number;
24
- glacierService: EvmGlacierService;
25
- }): Promise<TransactionHistoryResponse> => {
26
- if (isEthereumChainId(chainId)) {
27
- return getTransactionFromEtherscan({
28
- isTestnet,
29
- networkToken,
30
- explorerUrl,
31
- chainId,
32
- address,
33
- nextPageToken,
34
- offset,
35
- });
36
- }
37
-
38
- const isHealthy = glacierService.isHealthy();
39
- if (!isHealthy) {
40
- return {
41
- transactions: [],
42
- nextPageToken: '',
43
- };
44
- }
45
-
46
- return getTransactionsFromGlacier({
47
- networkToken,
48
- explorerUrl,
49
- chainId,
50
- address,
51
- nextPageToken,
52
- offset,
53
- glacierService,
54
- });
55
- };
@@ -1,7 +0,0 @@
1
- export function getExplorerAddressByNetwork(
2
- explorerUrl: string,
3
- hash: string,
4
- hashType: 'address' | 'tx' = 'tx',
5
- ): string {
6
- return `${explorerUrl}/${hashType}/${hash}`;
7
- }
@@ -1,16 +0,0 @@
1
- import { ipfsResolverWithFallback } from './ipfs-resolver-with-fallback';
2
-
3
- const COVALENT_IMG_SIZER = 'https://image-proxy.svc.prod.covalenthq.com/cdn-cgi/image';
4
-
5
- /**
6
- * Covalent has an on the fly image resizer, it resolves image urls then resizes the image.
7
- *
8
- * This allows us to request smaller images depending on the UI needs
9
- *
10
- * @param imgUrl the url of the image to convert to size
11
- * @returns The url to the image which is sized at the time of request
12
- */
13
- export function getSmallImageForNFT(imgUrl: string, imageSize: '256' | '512' | '1024' = '256') {
14
- const url = ipfsResolverWithFallback(imgUrl);
15
- return `${COVALENT_IMG_SIZER}/width=${imageSize},fit/${url}`;
16
- }
@@ -1,18 +0,0 @@
1
- import { ipfsResolver } from '@avalabs/utils-sdk';
2
-
3
- export const CLOUDFLARE_IPFS_URL = 'https://cloudflare-ipfs.com';
4
-
5
- export function ipfsResolverWithFallback(
6
- sourceUrl: string | undefined,
7
- desiredGatewayPrefix: string = CLOUDFLARE_IPFS_URL,
8
- ) {
9
- if (!sourceUrl) {
10
- return '';
11
- }
12
-
13
- try {
14
- return ipfsResolver(sourceUrl, desiredGatewayPrefix);
15
- } catch {
16
- return sourceUrl;
17
- }
18
- }
@@ -1,15 +0,0 @@
1
- enum ChainId {
2
- ETHEREUM_HOMESTEAD = 1,
3
- ETHEREUM_TEST_RINKEBY = 4,
4
- ETHEREUM_TEST_GOERLY = 5,
5
- ETHEREUM_TEST_SEPOLIA = 11155111,
6
- }
7
-
8
- export const isEthereumChainId = (chainId: number): boolean => {
9
- return (
10
- ChainId.ETHEREUM_HOMESTEAD === chainId ||
11
- ChainId.ETHEREUM_TEST_GOERLY === chainId ||
12
- ChainId.ETHEREUM_TEST_RINKEBY === chainId ||
13
- ChainId.ETHEREUM_TEST_SEPOLIA === chainId
14
- );
15
- };
@@ -1,7 +0,0 @@
1
- export async function resolve<T = unknown>(promise: Promise<T>) {
2
- try {
3
- return promise.then((res) => [res, null]).catch((err) => [null, err]);
4
- } catch (err) {
5
- return Promise.resolve([null, err]);
6
- }
7
- }
package/src/index.ts DELETED
@@ -1,3 +0,0 @@
1
- export * from './module';
2
- export * from './types';
3
- export * from './handlers/eth-sign/utils/typeguards';
package/src/module.ts DELETED
@@ -1,138 +0,0 @@
1
- import type {
2
- Module,
3
- Manifest,
4
- NetworkFees,
5
- GetTransactionHistory,
6
- RpcRequest,
7
- Environment,
8
- Network,
9
- ApprovalController,
10
- GetBalancesParams,
11
- GetBalancesResponse,
12
- } from '@avalabs/vm-module-types';
13
- import { rpcErrors } from '@metamask/rpc-errors';
14
- import { RpcMethod, parseManifest } from '@avalabs/vm-module-types';
15
- import { getTokens } from './handlers/get-tokens/get-tokens';
16
- import { getNetworkFee } from './handlers/get-network-fee/get-network-fee';
17
- import { getTransactionHistory } from './handlers/get-transaction-history/get-transaction-history';
18
- import ManifestJson from '../manifest.json';
19
- import { ethSendTransaction } from './handlers/eth-send-transaction/eth-send-transaction';
20
- import { getBalances } from './handlers/get-balances/get-balances';
21
- import { getEnv } from './env';
22
- import { EvmGlacierService } from './services/glacier-service/glacier-service';
23
- import { ethSign } from './handlers/eth-sign/eth-sign';
24
- import { forwardToRpcNode } from './handlers/forward-to-rpc-node/forward-to-rpc-node';
25
-
26
- export class EvmModule implements Module {
27
- #glacierService: EvmGlacierService;
28
- #proxyApiUrl: string;
29
- #approvalController: ApprovalController;
30
-
31
- constructor({
32
- approvalController,
33
- environment,
34
- }: {
35
- approvalController: ApprovalController;
36
- environment: Environment;
37
- }) {
38
- const { glacierApiUrl, proxyApiUrl } = getEnv(environment);
39
- this.#glacierService = new EvmGlacierService({ glacierApiUrl });
40
- this.#proxyApiUrl = proxyApiUrl;
41
- this.#approvalController = approvalController;
42
- }
43
-
44
- getAddress(): Promise<string> {
45
- return Promise.resolve('EVM address');
46
- }
47
-
48
- getBalances({
49
- addresses,
50
- network,
51
- currency,
52
- customTokens,
53
- storage,
54
- }: GetBalancesParams): Promise<GetBalancesResponse> {
55
- return getBalances({
56
- addresses,
57
- currency,
58
- network,
59
- proxyApiUrl: this.#proxyApiUrl,
60
- customTokens,
61
- glacierService: this.#glacierService,
62
- storage,
63
- });
64
- }
65
-
66
- getManifest(): Manifest | undefined {
67
- const result = parseManifest(ManifestJson);
68
- return result.success ? result.data : undefined;
69
- }
70
-
71
- getNetworkFee(network: Network): Promise<NetworkFees> {
72
- const { chainId, chainName, rpcUrl, utilityAddresses } = network;
73
- return getNetworkFee({
74
- chainId,
75
- chainName,
76
- rpcUrl,
77
- multiContractAddress: utilityAddresses?.multicall,
78
- });
79
- }
80
-
81
- getTransactionHistory(params: GetTransactionHistory) {
82
- const { network, address, nextPageToken, offset } = params;
83
- const { chainId, isTestnet, networkToken, explorerUrl = '' } = network;
84
-
85
- return getTransactionHistory({
86
- chainId,
87
- isTestnet,
88
- networkToken,
89
- explorerUrl,
90
- address,
91
- nextPageToken,
92
- offset,
93
- glacierService: this.#glacierService,
94
- });
95
- }
96
-
97
- getTokens(network: Network) {
98
- const { chainId } = network;
99
- return getTokens({ chainId, proxyApiUrl: this.#proxyApiUrl });
100
- }
101
-
102
- async onRpcRequest(request: RpcRequest, network: Network) {
103
- switch (request.method) {
104
- case RpcMethod.ETH_SEND_TRANSACTION:
105
- return ethSendTransaction({
106
- request,
107
- network,
108
- approvalController: this.#approvalController,
109
- proxyApiUrl: this.#proxyApiUrl,
110
- });
111
- case RpcMethod.PERSONAL_SIGN:
112
- case RpcMethod.ETH_SIGN:
113
- case RpcMethod.SIGN_TYPED_DATA:
114
- case RpcMethod.SIGN_TYPED_DATA_V1:
115
- case RpcMethod.SIGN_TYPED_DATA_V3:
116
- case RpcMethod.SIGN_TYPED_DATA_V4:
117
- return ethSign({
118
- request,
119
- network,
120
- approvalController: this.#approvalController,
121
- proxyApiUrl: this.#proxyApiUrl,
122
- });
123
- default:
124
- if (shouldForwardToRpcNode(request.method)) {
125
- return forwardToRpcNode(request, network);
126
- }
127
-
128
- return { error: rpcErrors.methodNotSupported(`Method ${request.method} not supported`) };
129
- }
130
- }
131
- }
132
-
133
- const shouldForwardToRpcNode = (method: RpcMethod) => {
134
- return (
135
- method.startsWith('eth_') ||
136
- ['web3_clientVersion', 'web3_sha3', 'net_version', 'net_peerCount', 'net_listening'].includes(method)
137
- );
138
- };
@@ -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
- }