@avalabs/evm-module 0.0.23 → 0.1.1
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.
- package/LICENSE +9 -0
- package/README.md +1 -1
- package/dist/index.cjs +6 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +5 -5
- package/dist/index.js.map +1 -1
- package/package.json +13 -8
- package/.turbo/turbo-build.log +0 -22
- package/.turbo/turbo-lint.log +0 -4
- package/.turbo/turbo-test.log +0 -119
- package/CHANGELOG.md +0 -174
- package/jest.config.js +0 -9
- package/src/constants.ts +0 -1
- package/src/contracts/openzeppelin/ERC1155.ts +0 -440
- package/src/contracts/openzeppelin/ERC20.ts +0 -330
- package/src/contracts/openzeppelin/ERC721.ts +0 -420
- package/src/contracts/openzeppelin/common.ts +0 -131
- package/src/contracts/openzeppelin/factories/ERC1155__factory.ts +0 -388
- package/src/contracts/openzeppelin/factories/ERC20__factory.ts +0 -353
- package/src/contracts/openzeppelin/factories/ERC721__factory.ts +0 -413
- package/src/contracts/openzeppelin/factories/index.ts +0 -6
- package/src/contracts/openzeppelin/index.ts +0 -10
- package/src/env.ts +0 -25
- package/src/handlers/eth-send-transaction/eth-send-transaction.test.ts +0 -572
- package/src/handlers/eth-send-transaction/eth-send-transaction.ts +0 -189
- package/src/handlers/eth-send-transaction/schema.test.ts +0 -240
- package/src/handlers/eth-send-transaction/schema.ts +0 -20
- package/src/handlers/eth-sign/eth-sign.test.ts +0 -359
- package/src/handlers/eth-sign/eth-sign.ts +0 -158
- package/src/handlers/eth-sign/schemas/eth-sign-typed-data.ts +0 -65
- package/src/handlers/eth-sign/schemas/eth-sign.ts +0 -9
- package/src/handlers/eth-sign/schemas/parse-request-params/fixture.ts +0 -47
- package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.test.ts +0 -284
- package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.ts +0 -94
- package/src/handlers/eth-sign/schemas/personal-sign.ts +0 -12
- package/src/handlers/eth-sign/schemas/shared.ts +0 -5
- package/src/handlers/eth-sign/utils/beautify-message/beautify-message.test.ts +0 -29
- package/src/handlers/eth-sign/utils/beautify-message/beautify-message.ts +0 -134
- package/src/handlers/eth-sign/utils/is-typed-data-valid.ts +0 -26
- package/src/handlers/eth-sign/utils/typeguards.ts +0 -10
- package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.test.ts +0 -90
- package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.ts +0 -23
- package/src/handlers/get-address/get-address.ts +0 -26
- package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +0 -77
- package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +0 -81
- package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.test.ts +0 -96
- package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.ts +0 -53
- package/src/handlers/get-balances/get-balances.test.ts +0 -248
- package/src/handlers/get-balances/get-balances.ts +0 -123
- package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +0 -71
- package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +0 -106
- package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.test.ts +0 -59
- package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.ts +0 -48
- package/src/handlers/get-network-fee/get-network-fee.test.ts +0 -43
- package/src/handlers/get-network-fee/get-network-fee.ts +0 -59
- package/src/handlers/get-tokens/get-tokens.test.ts +0 -100
- package/src/handlers/get-tokens/get-tokens.ts +0 -18
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.test.ts +0 -53
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.ts +0 -49
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.test.ts +0 -57
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.ts +0 -57
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.test.ts +0 -116
- package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/get-transaction-from-etherscan.ts +0 -73
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/convert-transaction.ts +0 -47
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-nft-metadata.ts +0 -35
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-sender-info.ts +0 -38
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tokens.ts +0 -106
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transaction-from-glacier.test.ts +0 -222
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-transactions-from-glacier.ts +0 -62
- package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tx-type.ts +0 -52
- package/src/handlers/get-transaction-history/get-transaction-history.test.ts +0 -57
- package/src/handlers/get-transaction-history/get-transaction-history.ts +0 -55
- package/src/handlers/get-transaction-history/utils/get-explorer-address-by-network.ts +0 -7
- package/src/handlers/get-transaction-history/utils/get-small-image-for-nft.ts +0 -16
- package/src/handlers/get-transaction-history/utils/ipfs-resolver-with-fallback.ts +0 -18
- package/src/handlers/get-transaction-history/utils/is-ethereum-chain-id.ts +0 -15
- package/src/handlers/get-transaction-history/utils/resolve.ts +0 -7
- package/src/index.ts +0 -3
- package/src/module.ts +0 -141
- package/src/services/glacier-service/glacier-service.ts +0 -238
- package/src/types.ts +0 -25
- package/src/utils/estimate-gas-limit.ts +0 -27
- package/src/utils/get-chain-id.ts +0 -12
- package/src/utils/get-nonce.ts +0 -11
- package/src/utils/get-provider.ts +0 -46
- package/src/utils/parse-erc20-transaction-type.ts +0 -35
- package/src/utils/process-transaction-simulation.test.ts +0 -105
- package/src/utils/process-transaction-simulation.ts +0 -293
- package/src/utils/scan-transaction.ts +0 -63
- package/tsconfig.jest.json +0 -7
- package/tsconfig.json +0 -14
- package/tsup.config.ts +0 -4
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import type { Network, RpcRequest } from '@avalabs/vm-module-types';
|
|
2
|
-
import { rpcErrors } from '@metamask/rpc-errors';
|
|
3
|
-
import { getProvider } from '../../utils/get-provider';
|
|
4
|
-
|
|
5
|
-
export const forwardToRpcNode = async (request: RpcRequest, network: Network) => {
|
|
6
|
-
try {
|
|
7
|
-
const provider = getProvider({
|
|
8
|
-
chainId: network.chainId,
|
|
9
|
-
chainName: network.chainName,
|
|
10
|
-
rpcUrl: network.rpcUrl,
|
|
11
|
-
multiContractAddress: network.utilityAddresses?.multicall,
|
|
12
|
-
pollingInterval: 1000,
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
const response = await provider.send(request.method, request.params as unknown[]);
|
|
16
|
-
return { result: response };
|
|
17
|
-
} catch (error) {
|
|
18
|
-
// extracting the error message based on the error object structure from ethers lib
|
|
19
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
-
const message = (error as any).info?.error?.message || (error as any).error?.message || (error as Error).message;
|
|
21
|
-
return { error: rpcErrors.internal(message) };
|
|
22
|
-
}
|
|
23
|
-
};
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { NetworkVMType, WalletType, type GetAddressParams, type GetAddressResponse } from '@avalabs/vm-module-types';
|
|
2
|
-
import { getAddressFromXPub, getEvmAddressFromPubKey } from '@avalabs/wallets-sdk';
|
|
3
|
-
import { rpcErrors } from '@metamask/rpc-errors';
|
|
4
|
-
|
|
5
|
-
type GetAddress = Omit<GetAddressParams, 'isTestnet' | 'xpubXP'>;
|
|
6
|
-
|
|
7
|
-
export const getAddress = async ({ accountIndex, xpub, walletType }: GetAddress): Promise<GetAddressResponse> => {
|
|
8
|
-
switch (walletType) {
|
|
9
|
-
case WalletType.Mnemonic:
|
|
10
|
-
case WalletType.Ledger:
|
|
11
|
-
case WalletType.Keystone: {
|
|
12
|
-
return {
|
|
13
|
-
[NetworkVMType.EVM]: getAddressFromXPub(xpub, accountIndex),
|
|
14
|
-
};
|
|
15
|
-
}
|
|
16
|
-
case WalletType.LedgerLive:
|
|
17
|
-
case WalletType.Seedless: {
|
|
18
|
-
const pubKeyBuffer = Buffer.from(xpub, 'hex');
|
|
19
|
-
return {
|
|
20
|
-
[NetworkVMType.EVM]: getEvmAddressFromPubKey(pubKeyBuffer),
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
default:
|
|
24
|
-
throw rpcErrors.invalidParams(`Unsupported wallet type: ${walletType}`);
|
|
25
|
-
}
|
|
26
|
-
};
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { getErc20Balances } from './get-erc20-balances';
|
|
2
|
-
import { ethers } from 'ethers';
|
|
3
|
-
import { TokenType } from '@avalabs/vm-module-types';
|
|
4
|
-
|
|
5
|
-
describe('get-erc20-balances', () => {
|
|
6
|
-
it('should return erc20 token balances', async () => {
|
|
7
|
-
jest.spyOn(ethers, 'Contract').mockImplementation(() => {
|
|
8
|
-
return {
|
|
9
|
-
balanceOf: jest.fn().mockResolvedValue(1000000000000000000n),
|
|
10
|
-
} as never;
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
const balance = getErc20Balances({
|
|
14
|
-
tokens: [
|
|
15
|
-
{
|
|
16
|
-
address: '0x123',
|
|
17
|
-
name: 'Ethereum',
|
|
18
|
-
symbol: 'ETH',
|
|
19
|
-
decimals: 18,
|
|
20
|
-
logoUri: 'https://example.com/logo.png',
|
|
21
|
-
type: TokenType.ERC20,
|
|
22
|
-
},
|
|
23
|
-
],
|
|
24
|
-
provider: {
|
|
25
|
-
getBalance: jest.fn().mockResolvedValue(1000000000000000000n),
|
|
26
|
-
} as never,
|
|
27
|
-
tokenService: {
|
|
28
|
-
getPricesByAddresses: jest.fn().mockResolvedValue({
|
|
29
|
-
'123': {
|
|
30
|
-
USD: {
|
|
31
|
-
price: 1000,
|
|
32
|
-
marketCap: 0,
|
|
33
|
-
vol24: 0,
|
|
34
|
-
change24: 0,
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
}),
|
|
38
|
-
} as never,
|
|
39
|
-
address: '0x123',
|
|
40
|
-
currency: 'USD',
|
|
41
|
-
network: {
|
|
42
|
-
pricingProviders: {
|
|
43
|
-
coingecko: {
|
|
44
|
-
nativeTokenId: '123',
|
|
45
|
-
assetPlatformId: '123',
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
networkToken: {
|
|
49
|
-
address: '0x123',
|
|
50
|
-
name: 'Ethereum',
|
|
51
|
-
symbol: 'ETH',
|
|
52
|
-
decimals: 18,
|
|
53
|
-
logoUri: 'https://example.com/logo.png',
|
|
54
|
-
},
|
|
55
|
-
} as never,
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
expect(balance).resolves.toEqual({
|
|
59
|
-
'0x123': {
|
|
60
|
-
address: '0x123',
|
|
61
|
-
name: 'Ethereum',
|
|
62
|
-
symbol: 'ETH',
|
|
63
|
-
decimals: 18,
|
|
64
|
-
logoUri: 'https://example.com/logo.png',
|
|
65
|
-
balance: 1000000000000000000n,
|
|
66
|
-
balanceCurrencyDisplayValue: '1000.00',
|
|
67
|
-
balanceDisplayValue: '1',
|
|
68
|
-
balanceInCurrency: 1000,
|
|
69
|
-
priceInCurrency: 1000,
|
|
70
|
-
type: 'ERC20',
|
|
71
|
-
change24: 0,
|
|
72
|
-
marketCap: 0,
|
|
73
|
-
vol24: 0,
|
|
74
|
-
},
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
});
|
|
@@ -1,81 +0,0 @@
|
|
|
1
|
-
import { TokenUnit } from '@avalabs/utils-sdk';
|
|
2
|
-
import { type ERC20Token, type Network, TokenType, type TokenWithBalanceEVM } from '@avalabs/vm-module-types';
|
|
3
|
-
import { ethers, type Provider } from 'ethers';
|
|
4
|
-
import ERC20 from '@openzeppelin/contracts/build/contracts/ERC20.json';
|
|
5
|
-
import type { TokenService } from '@internal/utils';
|
|
6
|
-
import { VsCurrencyType } from '@avalabs/coingecko-sdk';
|
|
7
|
-
|
|
8
|
-
export const getErc20Balances = async ({
|
|
9
|
-
provider,
|
|
10
|
-
tokenService,
|
|
11
|
-
address: userAddress,
|
|
12
|
-
currency,
|
|
13
|
-
tokens,
|
|
14
|
-
network,
|
|
15
|
-
}: {
|
|
16
|
-
provider: Provider;
|
|
17
|
-
tokenService: TokenService;
|
|
18
|
-
address: string;
|
|
19
|
-
currency: string;
|
|
20
|
-
tokens: ERC20Token[];
|
|
21
|
-
network: Network;
|
|
22
|
-
}): Promise<Record<string, TokenWithBalanceEVM>> => {
|
|
23
|
-
const coingeckoPlatformId = network.pricingProviders?.coingecko.assetPlatformId;
|
|
24
|
-
const coingeckoTokenId = network.pricingProviders?.coingecko.nativeTokenId;
|
|
25
|
-
const tokenAddresses = tokens.map((token) => token.address);
|
|
26
|
-
|
|
27
|
-
const tokensBalances = await Promise.allSettled(
|
|
28
|
-
tokens.map(async (token) => {
|
|
29
|
-
const contract = new ethers.Contract(token.address, ERC20.abi, provider);
|
|
30
|
-
const balanceBig: bigint = await contract.balanceOf?.(userAddress);
|
|
31
|
-
const balance = balanceBig || 0n;
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
...token,
|
|
35
|
-
balance,
|
|
36
|
-
};
|
|
37
|
-
}),
|
|
38
|
-
).then((res) => {
|
|
39
|
-
return res.reduce<(ERC20Token & { balance: bigint })[]>((acc, result) => {
|
|
40
|
-
return result.status === 'fulfilled' && result.value.balance !== 0n ? [...acc, result.value] : acc;
|
|
41
|
-
}, []);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
if (!tokensBalances.length) return {};
|
|
45
|
-
|
|
46
|
-
const simplePriceResponse =
|
|
47
|
-
(coingeckoPlatformId &&
|
|
48
|
-
(await tokenService.getPricesByAddresses(tokenAddresses, coingeckoPlatformId, currency as VsCurrencyType))) ||
|
|
49
|
-
{};
|
|
50
|
-
|
|
51
|
-
return tokensBalances.reduce(
|
|
52
|
-
(acc, token) => {
|
|
53
|
-
const priceInCurrency = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.price ?? undefined;
|
|
54
|
-
const marketCap = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.marketCap ?? undefined;
|
|
55
|
-
const vol24 = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.vol24 ?? undefined;
|
|
56
|
-
const change24 = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.change24 ?? undefined;
|
|
57
|
-
|
|
58
|
-
const balance = new TokenUnit(token.balance, token.decimals, token.symbol);
|
|
59
|
-
const balanceCurrencyDisplayValue = priceInCurrency ? balance.mul(priceInCurrency).toDisplay(2) : undefined;
|
|
60
|
-
const balanceInCurrency = balanceCurrencyDisplayValue ? Number(balanceCurrencyDisplayValue) : undefined;
|
|
61
|
-
const balanceDisplayValue = balance.toDisplay();
|
|
62
|
-
|
|
63
|
-
return {
|
|
64
|
-
...acc,
|
|
65
|
-
[token.address.toLowerCase()]: {
|
|
66
|
-
...token,
|
|
67
|
-
type: TokenType.ERC20,
|
|
68
|
-
balance: token.balance,
|
|
69
|
-
balanceDisplayValue,
|
|
70
|
-
balanceInCurrency,
|
|
71
|
-
balanceCurrencyDisplayValue,
|
|
72
|
-
priceInCurrency,
|
|
73
|
-
marketCap,
|
|
74
|
-
change24,
|
|
75
|
-
vol24,
|
|
76
|
-
},
|
|
77
|
-
};
|
|
78
|
-
},
|
|
79
|
-
{} as Record<string, TokenWithBalanceEVM>,
|
|
80
|
-
);
|
|
81
|
-
};
|
|
@@ -1,96 +0,0 @@
|
|
|
1
|
-
import { getNativeTokenBalances } from './get-native-token-balances';
|
|
2
|
-
|
|
3
|
-
describe('get-native-token-balances', () => {
|
|
4
|
-
it('should return native token balances', async () => {
|
|
5
|
-
const balance = getNativeTokenBalances({
|
|
6
|
-
provider: {
|
|
7
|
-
getBalance: jest.fn().mockResolvedValue(1000000000000000000n),
|
|
8
|
-
} as never,
|
|
9
|
-
tokenService: {
|
|
10
|
-
getSimplePrice: jest.fn().mockResolvedValue({
|
|
11
|
-
'123': {
|
|
12
|
-
USD: {
|
|
13
|
-
price: 1000,
|
|
14
|
-
marketCap: 0,
|
|
15
|
-
vol24: 0,
|
|
16
|
-
change24: 0,
|
|
17
|
-
},
|
|
18
|
-
},
|
|
19
|
-
}),
|
|
20
|
-
} as never,
|
|
21
|
-
address: '0x123',
|
|
22
|
-
currency: 'USD',
|
|
23
|
-
network: {
|
|
24
|
-
pricingProviders: {
|
|
25
|
-
coingecko: {
|
|
26
|
-
nativeTokenId: '123',
|
|
27
|
-
},
|
|
28
|
-
},
|
|
29
|
-
networkToken: {
|
|
30
|
-
address: '0x123',
|
|
31
|
-
name: 'Ethereum',
|
|
32
|
-
symbol: 'ETH',
|
|
33
|
-
decimals: 18,
|
|
34
|
-
logoUri: 'https://example.com/logo.png',
|
|
35
|
-
},
|
|
36
|
-
} as never,
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
expect(balance).resolves.toEqual({
|
|
40
|
-
address: '0x123',
|
|
41
|
-
name: 'Ethereum',
|
|
42
|
-
symbol: 'ETH',
|
|
43
|
-
decimals: 18,
|
|
44
|
-
logoUri: 'https://example.com/logo.png',
|
|
45
|
-
balance: 1000000000000000000n,
|
|
46
|
-
balanceCurrencyDisplayValue: '1000.00',
|
|
47
|
-
balanceDisplayValue: '1',
|
|
48
|
-
balanceInCurrency: 1000,
|
|
49
|
-
priceInCurrency: 1000,
|
|
50
|
-
coingeckoId: '123',
|
|
51
|
-
type: 'NATIVE',
|
|
52
|
-
marketCap: 0,
|
|
53
|
-
vol24: 0,
|
|
54
|
-
change24: 0,
|
|
55
|
-
});
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('should return native token object without balance data', async () => {
|
|
59
|
-
const balance = getNativeTokenBalances({
|
|
60
|
-
provider: {
|
|
61
|
-
getBalance: jest.fn().mockResolvedValue(0n),
|
|
62
|
-
} as never,
|
|
63
|
-
tokenService: {
|
|
64
|
-
getSimplePrice: jest.fn().mockResolvedValue({}),
|
|
65
|
-
} as never,
|
|
66
|
-
address: '0x123',
|
|
67
|
-
currency: 'USD',
|
|
68
|
-
network: {
|
|
69
|
-
pricingProviders: {
|
|
70
|
-
coingecko: {
|
|
71
|
-
nativeTokenId: '123',
|
|
72
|
-
},
|
|
73
|
-
},
|
|
74
|
-
networkToken: {
|
|
75
|
-
address: '0x123',
|
|
76
|
-
name: 'Ethereum',
|
|
77
|
-
symbol: 'ETH',
|
|
78
|
-
decimals: 18,
|
|
79
|
-
logoUri: 'https://example.com/logo.png',
|
|
80
|
-
},
|
|
81
|
-
} as never,
|
|
82
|
-
});
|
|
83
|
-
expect(balance).resolves.toEqual({
|
|
84
|
-
address: '0x123',
|
|
85
|
-
name: 'Ethereum',
|
|
86
|
-
symbol: 'ETH',
|
|
87
|
-
decimals: 18,
|
|
88
|
-
logoUri: 'https://example.com/logo.png',
|
|
89
|
-
balance: 0n,
|
|
90
|
-
balanceCurrencyDisplayValue: undefined,
|
|
91
|
-
balanceDisplayValue: '0',
|
|
92
|
-
coingeckoId: '123',
|
|
93
|
-
type: 'NATIVE',
|
|
94
|
-
});
|
|
95
|
-
});
|
|
96
|
-
});
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
import { TokenUnit } from '@avalabs/utils-sdk';
|
|
2
|
-
import { type Network, type NetworkTokenWithBalance, TokenType } from '@avalabs/vm-module-types';
|
|
3
|
-
import type { VsCurrencyType } from '@avalabs/coingecko-sdk';
|
|
4
|
-
import { TokenService } from '@internal/utils';
|
|
5
|
-
import type { Provider } from 'ethers';
|
|
6
|
-
|
|
7
|
-
export const getNativeTokenBalances = async ({
|
|
8
|
-
provider,
|
|
9
|
-
tokenService,
|
|
10
|
-
address,
|
|
11
|
-
currency,
|
|
12
|
-
network,
|
|
13
|
-
}: {
|
|
14
|
-
provider: Provider;
|
|
15
|
-
tokenService: TokenService;
|
|
16
|
-
address: string;
|
|
17
|
-
currency: string;
|
|
18
|
-
network: Network;
|
|
19
|
-
}): Promise<NetworkTokenWithBalance> => {
|
|
20
|
-
const coingeckoTokenId = network.pricingProviders?.coingecko.nativeTokenId;
|
|
21
|
-
const networkToken = network.networkToken;
|
|
22
|
-
const simplePriceResponse = coingeckoTokenId
|
|
23
|
-
? await tokenService.getSimplePrice({
|
|
24
|
-
coinIds: [coingeckoTokenId],
|
|
25
|
-
currencies: [currency] as VsCurrencyType[],
|
|
26
|
-
})
|
|
27
|
-
: {};
|
|
28
|
-
|
|
29
|
-
const priceInCurrency = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.price ?? undefined;
|
|
30
|
-
const marketCap = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.marketCap ?? undefined;
|
|
31
|
-
const vol24 = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.vol24 ?? undefined;
|
|
32
|
-
const change24 = simplePriceResponse?.[coingeckoTokenId ?? '']?.[currency]?.change24 ?? undefined;
|
|
33
|
-
|
|
34
|
-
const balance = await provider.getBalance(address);
|
|
35
|
-
const balanceUnit = new TokenUnit(balance, networkToken.decimals, networkToken.symbol);
|
|
36
|
-
const balanceDisplayValue = balanceUnit.toDisplay();
|
|
37
|
-
const balanceCurrencyDisplayValue = priceInCurrency ? balanceUnit.mul(priceInCurrency).toDisplay(2) : undefined;
|
|
38
|
-
const balanceInCurrency = balanceCurrencyDisplayValue ? Number(balanceCurrencyDisplayValue) : undefined;
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
...networkToken,
|
|
42
|
-
coingeckoId: coingeckoTokenId ?? '',
|
|
43
|
-
type: TokenType.NATIVE,
|
|
44
|
-
balance,
|
|
45
|
-
balanceDisplayValue,
|
|
46
|
-
balanceInCurrency,
|
|
47
|
-
balanceCurrencyDisplayValue,
|
|
48
|
-
priceInCurrency,
|
|
49
|
-
marketCap,
|
|
50
|
-
vol24,
|
|
51
|
-
change24,
|
|
52
|
-
};
|
|
53
|
-
};
|
|
@@ -1,248 +0,0 @@
|
|
|
1
|
-
import { NetworkVMType, TokenType } from '@avalabs/vm-module-types';
|
|
2
|
-
import { getBalances } from './get-balances';
|
|
3
|
-
import * as GlacierNativeToken from './glacier-balance-service/get-native-token-balances';
|
|
4
|
-
import * as GlacierERC20Token from './glacier-balance-service/get-erc20-balances';
|
|
5
|
-
import * as EvmNativeToken from './evm-balance-service/get-native-token-balances';
|
|
6
|
-
import * as EvmERC20Token from './evm-balance-service/get-erc20-balances';
|
|
7
|
-
import type { EvmGlacierService } from '../../services/glacier-service/glacier-service';
|
|
8
|
-
|
|
9
|
-
describe('get-balances', () => {
|
|
10
|
-
it('should get balances from glacier', async () => {
|
|
11
|
-
const mockGlacierService: EvmGlacierService = {
|
|
12
|
-
...expect.any(Object),
|
|
13
|
-
isNetworkSupported: () => true,
|
|
14
|
-
isHealthy: () => true,
|
|
15
|
-
};
|
|
16
|
-
jest.spyOn(GlacierNativeToken, 'getNativeTokenBalances').mockImplementationOnce(async () => {
|
|
17
|
-
return {
|
|
18
|
-
name: 'Ether',
|
|
19
|
-
symbol: 'ETH',
|
|
20
|
-
decimals: 18,
|
|
21
|
-
type: TokenType.NATIVE,
|
|
22
|
-
logoUri:
|
|
23
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
24
|
-
balance: 1n,
|
|
25
|
-
balanceDisplayValue: '1',
|
|
26
|
-
balanceInCurrency: 1,
|
|
27
|
-
balanceCurrencyDisplayValue: '1',
|
|
28
|
-
priceInCurrency: 1,
|
|
29
|
-
marketCap: 0,
|
|
30
|
-
vol24: 0,
|
|
31
|
-
change24: 0,
|
|
32
|
-
coingeckoId: '',
|
|
33
|
-
};
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
jest.spyOn(GlacierERC20Token, 'getErc20Balances').mockImplementationOnce(async () => {
|
|
37
|
-
return {
|
|
38
|
-
'0x123': {
|
|
39
|
-
chainId: 1,
|
|
40
|
-
address: '0x123',
|
|
41
|
-
name: 'DAI',
|
|
42
|
-
symbol: 'DAI',
|
|
43
|
-
decimals: 18,
|
|
44
|
-
logoUri: 'https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/dai.svg',
|
|
45
|
-
balance: 1n,
|
|
46
|
-
balanceCurrencyDisplayValue: '1',
|
|
47
|
-
balanceDisplayValue: '1',
|
|
48
|
-
balanceInCurrency: 1,
|
|
49
|
-
priceInCurrency: 1,
|
|
50
|
-
type: TokenType.ERC20,
|
|
51
|
-
change24: 0,
|
|
52
|
-
marketCap: 0,
|
|
53
|
-
vol24: 0,
|
|
54
|
-
},
|
|
55
|
-
};
|
|
56
|
-
});
|
|
57
|
-
const balances = await getBalances({
|
|
58
|
-
addresses: ['0x123'],
|
|
59
|
-
currency: 'USD',
|
|
60
|
-
network: {
|
|
61
|
-
chainId: 1,
|
|
62
|
-
chainName: 'Ethereum',
|
|
63
|
-
isTestnet: false,
|
|
64
|
-
networkToken: {
|
|
65
|
-
name: 'Ether',
|
|
66
|
-
decimals: 18,
|
|
67
|
-
symbol: 'ETH',
|
|
68
|
-
description:
|
|
69
|
-
'Ether is used to pay for transaction fees and computational services on Ethereum. Users can send Ether to other users, and developers can write smart contracts that receive, hold, and send Ether.',
|
|
70
|
-
logoUri:
|
|
71
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
72
|
-
},
|
|
73
|
-
pricingProviders: { coingecko: { nativeTokenId: 'ethereum', assetPlatformId: 'ethereum' } },
|
|
74
|
-
rpcUrl: 'https://proxy-api.avax.network/proxy/infura/mainnet',
|
|
75
|
-
utilityAddresses: { multicall: '0x5ba1e12693dc8f9c48aad8770482f4739beed696' },
|
|
76
|
-
vmName: NetworkVMType.EVM,
|
|
77
|
-
},
|
|
78
|
-
proxyApiUrl: 'proxyApiUrl',
|
|
79
|
-
glacierService: mockGlacierService,
|
|
80
|
-
});
|
|
81
|
-
expect(balances).toEqual({
|
|
82
|
-
'0x123': {
|
|
83
|
-
'0x123': {
|
|
84
|
-
address: '0x123',
|
|
85
|
-
balance: 1n,
|
|
86
|
-
balanceCurrencyDisplayValue: '1',
|
|
87
|
-
balanceDisplayValue: '1',
|
|
88
|
-
balanceInCurrency: 1,
|
|
89
|
-
chainId: 1,
|
|
90
|
-
change24: 0,
|
|
91
|
-
decimals: 18,
|
|
92
|
-
logoUri: 'https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/dai.svg',
|
|
93
|
-
marketCap: 0,
|
|
94
|
-
name: 'DAI',
|
|
95
|
-
priceInCurrency: 1,
|
|
96
|
-
symbol: 'DAI',
|
|
97
|
-
type: 'ERC20',
|
|
98
|
-
vol24: 0,
|
|
99
|
-
},
|
|
100
|
-
ETH: {
|
|
101
|
-
balance: 1n,
|
|
102
|
-
balanceCurrencyDisplayValue: '1',
|
|
103
|
-
balanceDisplayValue: '1',
|
|
104
|
-
balanceInCurrency: 1,
|
|
105
|
-
change24: 0,
|
|
106
|
-
coingeckoId: '',
|
|
107
|
-
decimals: 18,
|
|
108
|
-
logoUri:
|
|
109
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
110
|
-
marketCap: 0,
|
|
111
|
-
name: 'Ether',
|
|
112
|
-
priceInCurrency: 1,
|
|
113
|
-
symbol: 'ETH',
|
|
114
|
-
type: 'NATIVE',
|
|
115
|
-
vol24: 0,
|
|
116
|
-
},
|
|
117
|
-
},
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
|
|
121
|
-
it('should get balances from evm', async () => {
|
|
122
|
-
const mockGlacierService: EvmGlacierService = {
|
|
123
|
-
...expect.any(Object),
|
|
124
|
-
isNetworkSupported: () => false,
|
|
125
|
-
isHealthy: () => false,
|
|
126
|
-
};
|
|
127
|
-
global.fetch = jest.fn(() =>
|
|
128
|
-
Promise.resolve({
|
|
129
|
-
ok: true,
|
|
130
|
-
json: () =>
|
|
131
|
-
Promise.resolve([
|
|
132
|
-
{
|
|
133
|
-
chainId: 2,
|
|
134
|
-
address: '0x456',
|
|
135
|
-
name: 'DAI2',
|
|
136
|
-
symbol: 'DAI2',
|
|
137
|
-
decimals: 18,
|
|
138
|
-
logoUri: 'https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/dai.svg',
|
|
139
|
-
type: TokenType.ERC20,
|
|
140
|
-
},
|
|
141
|
-
]),
|
|
142
|
-
}),
|
|
143
|
-
) as jest.Mock;
|
|
144
|
-
jest.spyOn(EvmNativeToken, 'getNativeTokenBalances').mockImplementationOnce(async () => {
|
|
145
|
-
return {
|
|
146
|
-
name: 'Ether2',
|
|
147
|
-
symbol: 'ETH2',
|
|
148
|
-
decimals: 18,
|
|
149
|
-
type: TokenType.NATIVE,
|
|
150
|
-
logoUri:
|
|
151
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
152
|
-
balance: 1n,
|
|
153
|
-
balanceDisplayValue: '1',
|
|
154
|
-
balanceInCurrency: 1,
|
|
155
|
-
balanceCurrencyDisplayValue: '1',
|
|
156
|
-
priceInCurrency: 1,
|
|
157
|
-
marketCap: 0,
|
|
158
|
-
vol24: 0,
|
|
159
|
-
change24: 0,
|
|
160
|
-
coingeckoId: '',
|
|
161
|
-
};
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
jest.spyOn(EvmERC20Token, 'getErc20Balances').mockImplementationOnce(async () => {
|
|
165
|
-
return {
|
|
166
|
-
'0x456': {
|
|
167
|
-
chainId: 2,
|
|
168
|
-
address: '0x456',
|
|
169
|
-
name: 'DAI2',
|
|
170
|
-
symbol: 'DAI2',
|
|
171
|
-
decimals: 18,
|
|
172
|
-
logoUri: 'https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/dai.svg',
|
|
173
|
-
balance: 1n,
|
|
174
|
-
balanceCurrencyDisplayValue: '1',
|
|
175
|
-
balanceDisplayValue: '1',
|
|
176
|
-
balanceInCurrency: 1,
|
|
177
|
-
priceInCurrency: 1,
|
|
178
|
-
type: TokenType.ERC20,
|
|
179
|
-
change24: 0,
|
|
180
|
-
marketCap: 0,
|
|
181
|
-
vol24: 0,
|
|
182
|
-
},
|
|
183
|
-
};
|
|
184
|
-
});
|
|
185
|
-
const balances = await getBalances({
|
|
186
|
-
addresses: ['0x456'],
|
|
187
|
-
currency: 'USD',
|
|
188
|
-
network: {
|
|
189
|
-
chainId: 2,
|
|
190
|
-
chainName: '2',
|
|
191
|
-
isTestnet: false,
|
|
192
|
-
networkToken: {
|
|
193
|
-
name: 'Ether2',
|
|
194
|
-
decimals: 18,
|
|
195
|
-
symbol: 'ETH2',
|
|
196
|
-
description:
|
|
197
|
-
'Ether is used to pay for transaction fees and computational services on Ethereum. Users can send Ether to other users, and developers can write smart contracts that receive, hold, and send Ether.',
|
|
198
|
-
logoUri:
|
|
199
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
200
|
-
},
|
|
201
|
-
pricingProviders: { coingecko: { nativeTokenId: 'ethereum', assetPlatformId: 'ethereum' } },
|
|
202
|
-
rpcUrl: 'https://proxy-api.avax.network/proxy/infura/mainnet',
|
|
203
|
-
utilityAddresses: { multicall: '0x5ba1e12693dc8f9c48aad8770482f4739beed696' },
|
|
204
|
-
vmName: NetworkVMType.EVM,
|
|
205
|
-
},
|
|
206
|
-
proxyApiUrl: 'proxyApiUrl',
|
|
207
|
-
glacierService: mockGlacierService,
|
|
208
|
-
});
|
|
209
|
-
expect(balances).toEqual({
|
|
210
|
-
'0x456': {
|
|
211
|
-
'0x456': {
|
|
212
|
-
address: '0x456',
|
|
213
|
-
balance: 1n,
|
|
214
|
-
balanceCurrencyDisplayValue: '1',
|
|
215
|
-
balanceDisplayValue: '1',
|
|
216
|
-
balanceInCurrency: 1,
|
|
217
|
-
chainId: 2,
|
|
218
|
-
change24: 0,
|
|
219
|
-
decimals: 18,
|
|
220
|
-
logoUri: 'https://s3.us-east-2.amazonaws.com/nomics-api/static/images/currencies/dai.svg',
|
|
221
|
-
marketCap: 0,
|
|
222
|
-
name: 'DAI2',
|
|
223
|
-
priceInCurrency: 1,
|
|
224
|
-
symbol: 'DAI2',
|
|
225
|
-
type: 'ERC20',
|
|
226
|
-
vol24: 0,
|
|
227
|
-
},
|
|
228
|
-
ETH2: {
|
|
229
|
-
balance: 1n,
|
|
230
|
-
balanceCurrencyDisplayValue: '1',
|
|
231
|
-
balanceDisplayValue: '1',
|
|
232
|
-
balanceInCurrency: 1,
|
|
233
|
-
change24: 0,
|
|
234
|
-
coingeckoId: '',
|
|
235
|
-
decimals: 18,
|
|
236
|
-
logoUri:
|
|
237
|
-
'https://images.ctfassets.net/gcj8jwzm6086/6l56QLVZmvacuBfjHBTThP/791d743dd2c526692562780c2325fedf/eth-circle__1_.svg',
|
|
238
|
-
marketCap: 0,
|
|
239
|
-
name: 'Ether2',
|
|
240
|
-
priceInCurrency: 1,
|
|
241
|
-
symbol: 'ETH2',
|
|
242
|
-
type: 'NATIVE',
|
|
243
|
-
vol24: 0,
|
|
244
|
-
},
|
|
245
|
-
},
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
});
|