@avalabs/evm-module 0.0.21 → 0.0.23

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 (31) hide show
  1. package/.turbo/turbo-build.log +10 -10
  2. package/.turbo/turbo-lint.log +1 -1
  3. package/.turbo/turbo-test.log +8 -5
  4. package/CHANGELOG.md +15 -0
  5. package/dist/index.cjs +19 -21
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +2 -2
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.js +18 -19
  10. package/dist/index.js.map +1 -1
  11. package/manifest.json +10 -1
  12. package/package.json +9 -8
  13. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.test.ts +90 -0
  14. package/src/handlers/forward-to-rpc-node/forward-to-rpc-node.ts +23 -0
  15. package/src/handlers/get-address/get-address.ts +26 -0
  16. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +3 -4
  17. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +13 -17
  18. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.test.ts +4 -5
  19. package/src/handlers/get-balances/evm-balance-service/get-native-token-balances.ts +7 -10
  20. package/src/handlers/get-balances/get-balances.test.ts +10 -11
  21. package/src/handlers/get-balances/get-balances.ts +5 -3
  22. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +1 -2
  23. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +10 -13
  24. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.test.ts +2 -3
  25. package/src/handlers/get-balances/glacier-balance-service/get-native-token-balances.ts +10 -9
  26. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.test.ts +53 -0
  27. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-erc20.ts +3 -5
  28. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.test.ts +57 -0
  29. package/src/handlers/get-transaction-history/converters/etherscan-transaction-converter/convert-transaction-normal.ts +3 -4
  30. package/src/handlers/get-transaction-history/converters/evm-transaction-converter/get-tokens.ts +5 -6
  31. package/src/module.ts +17 -2
@@ -0,0 +1,53 @@
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
+ });
@@ -1,8 +1,7 @@
1
1
  import type { Erc20Tx } from '@avalabs/etherscan-sdk';
2
2
  import { TokenType, TransactionType, type Transaction } from '@avalabs/vm-module-types';
3
- import { balanceToDisplayValue } from '@avalabs/utils-sdk';
3
+ import { TokenUnit } from '@avalabs/utils-sdk';
4
4
  import { getExplorerAddressByNetwork } from '../../utils/get-explorer-address-by-network';
5
- import { BN } from 'bn.js';
6
5
 
7
6
  export function convertTransactionERC20({
8
7
  tx,
@@ -17,9 +16,8 @@ export function convertTransactionERC20({
17
16
  }): Transaction {
18
17
  const isSender = tx.from.toLowerCase() === address.toLowerCase();
19
18
  const timestamp = parseInt(tx.timeStamp) * 1000;
20
- const decimals = parseInt(tx.tokenDecimal);
21
- const amountBN = new BN(tx.value);
22
- const amountDisplayValue = balanceToDisplayValue(amountBN, decimals);
19
+ const amount = new TokenUnit(tx.value, Number(tx.tokenDecimal), tx.tokenSymbol);
20
+ const amountDisplayValue = amount.toDisplay();
23
21
  const { from, to, gasPrice, gasUsed, hash, tokenDecimal, tokenName, tokenSymbol } = tx;
24
22
  const txType = isSender ? TransactionType.SEND : TransactionType.RECEIVE;
25
23
  const explorerLink = getExplorerAddressByNetwork(explorerUrl, hash);
@@ -0,0 +1,57 @@
1
+ import { TokenType, TransactionType } from '@avalabs/vm-module-types';
2
+ import { convertTransactionNormal } from './convert-transaction-normal';
3
+ import type { NormalTx } from '@avalabs/etherscan-sdk';
4
+
5
+ describe('convertTransactionNormal ', () => {
6
+ it('correctly converts normal transaction data', () => {
7
+ const tx = {
8
+ from: '0xSenderAddress',
9
+ to: '0xReceiverAddress',
10
+ timeStamp: '1625794800', // Unix timestamp in seconds
11
+ value: '1000000000000000000', // 1 token in wei
12
+ gasPrice: '20000000000', // in wei
13
+ gasUsed: '21000',
14
+ hash: '0xTransactionHash',
15
+ input: '0x', // Empty input for a non-contract call
16
+ } as NormalTx;
17
+
18
+ const networkToken = {
19
+ name: 'Ethereum',
20
+ symbol: 'ETH',
21
+ decimals: 18,
22
+ };
23
+
24
+ const address = '0xSenderAddress';
25
+ const explorerUrl = 'https://explorer.example.com';
26
+ const chainId = 1;
27
+
28
+ const expected = {
29
+ isIncoming: false,
30
+ isOutgoing: true,
31
+ isContractCall: false,
32
+ timestamp: 1625794800000, // Converted timestamp in milliseconds
33
+ hash: '0xTransactionHash',
34
+ isSender: true,
35
+ from: '0xSenderAddress',
36
+ to: '0xReceiverAddress',
37
+ tokens: [
38
+ {
39
+ decimal: '18',
40
+ name: 'Ethereum',
41
+ symbol: 'ETH',
42
+ amount: '1',
43
+ type: TokenType.NATIVE,
44
+ },
45
+ ],
46
+ gasUsed: '21000',
47
+ gasPrice: '20000000000',
48
+ chainId: '1',
49
+ txType: TransactionType.SEND,
50
+ explorerLink: 'https://explorer.example.com/tx/0xTransactionHash', // Assuming this is how the explorer link is formatted
51
+ };
52
+
53
+ const result = convertTransactionNormal({ tx, networkToken, chainId, explorerUrl, address });
54
+
55
+ expect(result).toEqual(expected);
56
+ });
57
+ });
@@ -1,7 +1,6 @@
1
1
  import type { NormalTx } from '@avalabs/etherscan-sdk';
2
2
  import { TokenType, TransactionType, type NetworkToken, type Transaction } from '@avalabs/vm-module-types';
3
- import { balanceToDisplayValue } from '@avalabs/utils-sdk';
4
- import { BN } from 'bn.js';
3
+ import { TokenUnit } from '@avalabs/utils-sdk';
5
4
  import { getExplorerAddressByNetwork } from '../../utils/get-explorer-address-by-network';
6
5
 
7
6
  export const convertTransactionNormal = ({
@@ -20,8 +19,8 @@ export const convertTransactionNormal = ({
20
19
  const isSender = tx.from.toLowerCase() === address.toLowerCase();
21
20
  const timestamp = parseInt(tx.timeStamp) * 1000;
22
21
  const decimals = networkToken.decimals;
23
- const amountBN = new BN(tx.value);
24
- const amountDisplayValue = balanceToDisplayValue(amountBN, decimals);
22
+ const amount = new TokenUnit(tx.value, networkToken.decimals, networkToken.symbol);
23
+ const amountDisplayValue = amount.toDisplay();
25
24
  const txType = isSender ? TransactionType.SEND : TransactionType.RECEIVE;
26
25
 
27
26
  const { from, to, gasPrice, gasUsed, hash } = tx;
@@ -1,6 +1,5 @@
1
1
  import type { TransactionDetails } from '@avalabs/glacier-sdk';
2
- import { balanceToDisplayValue } from '@avalabs/utils-sdk';
3
- import { BN } from 'bn.js';
2
+ import { TokenUnit } from '@avalabs/utils-sdk';
4
3
  import type { TxToken, NetworkToken } from '@avalabs/vm-module-types';
5
4
  import { TokenType } from '@avalabs/vm-module-types';
6
5
  import { resolve } from '../../utils/resolve';
@@ -16,8 +15,8 @@ export const getTokens = async (
16
15
 
17
16
  if (nativeTransaction.value !== '0') {
18
17
  const decimal = networkToken.decimals;
19
- const amountBN = new BN(nativeTransaction.value);
20
- const amountDisplayValue = balanceToDisplayValue(amountBN, decimal);
18
+ const amount = new TokenUnit(nativeTransaction.value, networkToken.decimals, networkToken.symbol);
19
+ const amountDisplayValue = amount.toDisplay();
21
20
  result.push({
22
21
  decimal: decimal.toString(),
23
22
  name: networkToken.name,
@@ -31,8 +30,8 @@ export const getTokens = async (
31
30
 
32
31
  erc20Transfers?.forEach((erc20Transfer) => {
33
32
  const decimals = erc20Transfer.erc20Token.decimals;
34
- const amountBN = new BN(erc20Transfer.value);
35
- const amountDisplayValue = balanceToDisplayValue(amountBN, decimals);
33
+ const amount = new TokenUnit(erc20Transfer.value, decimals, erc20Transfer.erc20Token.symbol);
34
+ const amountDisplayValue = amount.toDisplay();
36
35
 
37
36
  result.push({
38
37
  decimal: decimals.toString(),
package/src/module.ts CHANGED
@@ -9,6 +9,8 @@ import type {
9
9
  ApprovalController,
10
10
  GetBalancesParams,
11
11
  GetBalancesResponse,
12
+ GetAddressParams,
13
+ GetAddressResponse,
12
14
  } from '@avalabs/vm-module-types';
13
15
  import { rpcErrors } from '@metamask/rpc-errors';
14
16
  import { RpcMethod, parseManifest } from '@avalabs/vm-module-types';
@@ -21,6 +23,8 @@ import { getBalances } from './handlers/get-balances/get-balances';
21
23
  import { getEnv } from './env';
22
24
  import { EvmGlacierService } from './services/glacier-service/glacier-service';
23
25
  import { ethSign } from './handlers/eth-sign/eth-sign';
26
+ import { forwardToRpcNode } from './handlers/forward-to-rpc-node/forward-to-rpc-node';
27
+ import { getAddress } from './handlers/get-address/get-address';
24
28
 
25
29
  export class EvmModule implements Module {
26
30
  #glacierService: EvmGlacierService;
@@ -40,8 +44,8 @@ export class EvmModule implements Module {
40
44
  this.#approvalController = approvalController;
41
45
  }
42
46
 
43
- getAddress(): Promise<string> {
44
- return Promise.resolve('EVM address');
47
+ getAddress({ accountIndex, xpub, walletType }: GetAddressParams): Promise<GetAddressResponse> {
48
+ return getAddress({ accountIndex, xpub, walletType });
45
49
  }
46
50
 
47
51
  getBalances({
@@ -120,7 +124,18 @@ export class EvmModule implements Module {
120
124
  proxyApiUrl: this.#proxyApiUrl,
121
125
  });
122
126
  default:
127
+ if (shouldForwardToRpcNode(request.method)) {
128
+ return forwardToRpcNode(request, network);
129
+ }
130
+
123
131
  return { error: rpcErrors.methodNotSupported(`Method ${request.method} not supported`) };
124
132
  }
125
133
  }
126
134
  }
135
+
136
+ const shouldForwardToRpcNode = (method: RpcMethod) => {
137
+ return (
138
+ method.startsWith('eth_') ||
139
+ ['web3_clientVersion', 'web3_sha3', 'net_version', 'net_peerCount', 'net_listening'].includes(method)
140
+ );
141
+ };