@avalabs/evm-module 0.0.15 → 0.0.17

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 (43) hide show
  1. package/.turbo/turbo-build.log +10 -10
  2. package/.turbo/turbo-lint.log +1 -1
  3. package/.turbo/turbo-test.log +35 -15
  4. package/CHANGELOG.md +17 -0
  5. package/dist/index.cjs +31 -9
  6. package/dist/index.cjs.map +1 -1
  7. package/dist/index.d.cts +13 -2
  8. package/dist/index.d.ts +13 -2
  9. package/dist/index.js +28 -10
  10. package/dist/index.js.map +1 -1
  11. package/manifest.json +1 -1
  12. package/package.json +6 -5
  13. package/src/constants.ts +1 -0
  14. package/src/handlers/eth-send-transaction/eth-send-transaction.test.ts +233 -2
  15. package/src/handlers/eth-send-transaction/eth-send-transaction.ts +27 -8
  16. package/src/handlers/eth-sign/eth-sign.test.ts +320 -0
  17. package/src/handlers/eth-sign/eth-sign.ts +158 -0
  18. package/src/handlers/eth-sign/schemas/eth-sign-typed-data.ts +65 -0
  19. package/src/handlers/eth-sign/schemas/eth-sign.ts +9 -0
  20. package/src/handlers/eth-sign/schemas/parse-request-params/fixture.ts +47 -0
  21. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.test.ts +284 -0
  22. package/src/handlers/eth-sign/schemas/parse-request-params/parse-request-params.ts +94 -0
  23. package/src/handlers/eth-sign/schemas/personal-sign.ts +12 -0
  24. package/src/handlers/eth-sign/schemas/shared.ts +5 -0
  25. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.test.ts +29 -0
  26. package/src/handlers/eth-sign/utils/beautify-message/beautify-message.ts +134 -0
  27. package/src/handlers/eth-sign/utils/is-typed-data-valid.ts +26 -0
  28. package/src/handlers/eth-sign/utils/typeguards.ts +10 -0
  29. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +2 -2
  30. package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +4 -6
  31. package/src/handlers/get-balances/get-balances.test.ts +0 -5
  32. package/src/handlers/get-balances/get-balances.ts +14 -3
  33. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +0 -1
  34. package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +10 -7
  35. package/src/handlers/get-tokens/get-tokens.test.ts +6 -6
  36. package/src/index.ts +1 -0
  37. package/src/module.ts +14 -0
  38. package/src/types.ts +9 -0
  39. package/src/utils/parse-erc20-transaction-type.ts +35 -0
  40. package/src/utils/process-transaction-simulation.test.ts +105 -0
  41. package/src/utils/process-transaction-simulation.ts +294 -0
  42. package/src/utils/scan-transaction.ts +63 -0
  43. package/tsconfig.json +6 -1
package/manifest.json CHANGED
@@ -32,7 +32,7 @@
32
32
  "permissions": {
33
33
  "rpc": {
34
34
  "dapps": true,
35
- "methods": ["eth_sendTransaction", "eth_*", "getTransactionHistory"]
35
+ "methods": ["eth_sendTransaction", "eth_*", "personal_sign"]
36
36
  }
37
37
  },
38
38
  "manifestVersion": "0.0"
package/package.json CHANGED
@@ -1,23 +1,23 @@
1
1
  {
2
2
  "name": "@avalabs/evm-module",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "main": "dist/index.cjs",
5
5
  "module": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "type": "module",
8
8
  "dependencies": {
9
- "@avalabs/vm-module-types": "0.0.15",
9
+ "@avalabs/vm-module-types": "0.0.17",
10
10
  "@zodios/core": "10.9.6",
11
11
  "@avalabs/coingecko-sdk": "v2.8.0-alpha.193",
12
12
  "@avalabs/utils-sdk": "2.8.0-alpha.193",
13
13
  "@avalabs/etherscan-sdk": "v2.8.0-alpha.193",
14
14
  "@avalabs/glacier-sdk": "v2.8.0-alpha.193",
15
15
  "@avalabs/wallets-sdk": "v2.8.0-alpha.193",
16
+ "@blockaid/client": "0.11.0",
16
17
  "bn.js": "5.2.1",
17
18
  "lodash.startcase": "4.4.0",
18
19
  "@metamask/rpc-errors": "6.3.0",
19
- "zod": "3.23.8",
20
- "@internal/utils": "0.0.3"
20
+ "zod": "3.23.8"
21
21
  },
22
22
  "devDependencies": {
23
23
  "typechain": "8.3.1",
@@ -31,7 +31,8 @@
31
31
  "tsup": "7.2.0",
32
32
  "ethers": "6.8.1",
33
33
  "@internal/tsup-config": "0.0.1",
34
- "eslint-config-custom": "0.0.3"
34
+ "eslint-config-custom": "0.0.1",
35
+ "@internal/utils": "0.0.3"
35
36
  },
36
37
  "peerDependencies": {
37
38
  "ethers": "^6.8.1"
@@ -0,0 +1 @@
1
+ export const DEFAULT_DECIMALS = 18;
@@ -3,16 +3,33 @@ import { parseRequestParams } from './schema';
3
3
  import { estimateGasLimit } from '../../utils/estimate-gas-limit';
4
4
  import { getNonce } from '../../utils/get-nonce';
5
5
  import { rpcErrors } from '@metamask/rpc-errors';
6
- import { RpcMethod, type ApprovalController, type Network } from '@avalabs/vm-module-types';
6
+ import { AlertType, RpcMethod, TokenType, type ApprovalController, type Network } from '@avalabs/vm-module-types';
7
7
  import { ZodError } from 'zod';
8
8
  import { getProvider } from '../../utils/get-provider';
9
+ import Blockaid from '@blockaid/client';
9
10
 
10
11
  const mockGetProvider = getProvider as jest.MockedFunction<typeof getProvider>;
11
12
 
13
+ const PROXY_API_URL = 'https://proxy-api.avax.network';
14
+
12
15
  jest.mock('./schema');
13
16
  jest.mock('../../utils/estimate-gas-limit');
14
17
  jest.mock('../../utils/get-nonce');
15
18
  jest.mock('../../utils/get-provider');
19
+ jest.mock('@blockaid/client', () => {
20
+ return jest.fn().mockImplementation(() => {
21
+ return {
22
+ evm: {
23
+ transaction: {
24
+ scan: jest.fn().mockResolvedValue({ validation: { result_type: 'Benign' } }),
25
+ },
26
+ jsonRpc: {
27
+ scan: jest.fn(),
28
+ },
29
+ },
30
+ };
31
+ });
32
+ });
16
33
 
17
34
  const mockOnTransactionConfirmed = jest.fn();
18
35
  const mockOnTransactionReverted = jest.fn();
@@ -64,6 +81,7 @@ const testRequestParams = () => ({
64
81
  },
65
82
  network: testNetwork,
66
83
  approvalController: mockApprovalController,
84
+ proxyApiUrl: PROXY_API_URL,
67
85
  });
68
86
 
69
87
  const displayData = {
@@ -80,10 +98,13 @@ const displayData = {
80
98
  data: '0xdata',
81
99
  },
82
100
  networkFeeSelector: true,
101
+ alert: undefined,
102
+ tokenApprovals: undefined,
103
+ balanceChange: undefined,
83
104
  };
84
105
 
85
106
  const signingData = {
86
- type: 'evm_transaction',
107
+ type: 'eth_sendTransaction',
87
108
  account: '0xfrom',
88
109
  chainId: 1,
89
110
  data: {
@@ -221,6 +242,149 @@ describe('eth_sendTransaction handler', () => {
221
242
  });
222
243
  });
223
244
 
245
+ it('should add alert object with Warning type to displayData when validation result is Warning', async () => {
246
+ testWithValidationResultType('Warning');
247
+ });
248
+
249
+ it('should add alert object with Warning type to displayData when validation result is Error', async () => {
250
+ testWithValidationResultType('Error');
251
+ });
252
+
253
+ it('should add alert object with Danger type to displayData when validation result is Malicious', async () => {
254
+ testWithValidationResultType('Malicious');
255
+ });
256
+
257
+ it('should process transaction and add token approvals and balance changes to displayData', async () => {
258
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
259
+ (Blockaid as any).mockImplementation(() => ({
260
+ evm: {
261
+ transaction: {
262
+ scan: jest.fn().mockResolvedValue({
263
+ validation: { result_type: 'Benign' },
264
+ simulation: {
265
+ status: 'Success',
266
+ account_summary: {
267
+ exposures: [
268
+ {
269
+ asset: {
270
+ type: TokenType.ERC20,
271
+ address: '0xTokenAddress',
272
+ name: 'TokenName',
273
+ symbol: 'TKN',
274
+ decimals: 18,
275
+ logo_url: 'logo_url',
276
+ },
277
+ spenders: {
278
+ '0xSpenderAddress': {
279
+ exposure: [{ raw_value: '1', usd_price: '1' }],
280
+ },
281
+ },
282
+ },
283
+ ],
284
+ assets_diffs: [
285
+ {
286
+ asset: {
287
+ name: 'TokenName',
288
+ symbol: 'TKN',
289
+ decimals: 18,
290
+ logo_url: 'logo_url',
291
+ type: TokenType.ERC20,
292
+ address: '0xTokenAddress',
293
+ },
294
+ in: [{ value: '1', usd_price: '1' }],
295
+ out: [{ value: '1', usd_price: '1' }],
296
+ },
297
+ ],
298
+ },
299
+ },
300
+ }),
301
+ },
302
+ },
303
+ }));
304
+
305
+ mockParseRequestParams.mockReturnValue({
306
+ success: true,
307
+ data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12', gas: '0x5208' }],
308
+ });
309
+
310
+ const requestParams = testRequestParams();
311
+
312
+ await ethSendTransaction(requestParams);
313
+
314
+ expect(mockGetProvider).toHaveBeenCalledWith({
315
+ chainId: 1,
316
+ chainName: 'chainName',
317
+ rpcUrl: 'rpcUrl',
318
+ multiContractAddress: 'multiContractAddress',
319
+ pollingInterval: 1000,
320
+ });
321
+
322
+ expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
323
+ request: requestParams.request,
324
+ displayData: {
325
+ ...displayData,
326
+ tokenApprovals: {
327
+ isEditable: true,
328
+ approvals: [
329
+ {
330
+ token: {
331
+ type: TokenType.ERC20,
332
+ address: '0xTokenAddress',
333
+ name: 'TokenName',
334
+ symbol: 'TKN',
335
+ decimals: 18,
336
+ logoUri: 'logo_url',
337
+ },
338
+ spenderAddress: '0xSpenderAddress',
339
+ value: '1',
340
+ usdPrice: '1',
341
+ logoUri: 'logo_url',
342
+ },
343
+ ],
344
+ },
345
+ balanceChange: {
346
+ ins: [
347
+ {
348
+ token: {
349
+ type: TokenType.ERC20,
350
+ address: '0xTokenAddress',
351
+ name: 'TokenName',
352
+ symbol: 'TKN',
353
+ decimals: 18,
354
+ logoUri: 'logo_url',
355
+ },
356
+ items: [
357
+ {
358
+ displayValue: '1',
359
+ usdPrice: '1',
360
+ },
361
+ ],
362
+ },
363
+ ],
364
+ outs: [
365
+ {
366
+ token: {
367
+ type: TokenType.ERC20,
368
+ address: '0xTokenAddress',
369
+ name: 'TokenName',
370
+ symbol: 'TKN',
371
+ decimals: 18,
372
+ logoUri: 'logo_url',
373
+ },
374
+ items: [
375
+ {
376
+ displayValue: '1',
377
+ usdPrice: '1',
378
+ },
379
+ ],
380
+ },
381
+ ],
382
+ },
383
+ },
384
+ signingData,
385
+ });
386
+ });
387
+
224
388
  it('should return error if gas limit calculation fails', async () => {
225
389
  mockParseRequestParams.mockReturnValue({
226
390
  success: true,
@@ -331,3 +495,70 @@ describe('eth_sendTransaction handler', () => {
331
495
  });
332
496
  });
333
497
  });
498
+
499
+ const testWithValidationResultType = async (resultType: 'Warning' | 'Error' | 'Malicious') => {
500
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
501
+ (Blockaid as any).mockImplementation(() => ({
502
+ evm: {
503
+ transaction: {
504
+ scan: jest.fn().mockResolvedValue({
505
+ validation: { result_type: resultType },
506
+ simulation: { status: 'Success', account_summary: { exposures: [], assets_diffs: [] } },
507
+ }),
508
+ },
509
+ },
510
+ }));
511
+
512
+ mockParseRequestParams.mockReturnValue({
513
+ success: true,
514
+ data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12', gas: '0x5208' }],
515
+ });
516
+
517
+ const requestParams = testRequestParams();
518
+
519
+ await ethSendTransaction(requestParams);
520
+
521
+ expect(mockGetProvider).toHaveBeenCalledWith({
522
+ chainId: 1,
523
+ chainName: 'chainName',
524
+ rpcUrl: 'rpcUrl',
525
+ multiContractAddress: 'multiContractAddress',
526
+ pollingInterval: 1000,
527
+ });
528
+
529
+ if (resultType === 'Malicious') {
530
+ expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
531
+ request: requestParams.request,
532
+ displayData: {
533
+ ...displayData,
534
+ alert: {
535
+ type: AlertType.DANGER,
536
+ details: {
537
+ title: 'Scam Transaction',
538
+ description: 'This transaction is malicious, do not proceed.',
539
+ actionTitles: {
540
+ reject: 'Reject Transaction',
541
+ proceed: 'Proceed Anyway',
542
+ },
543
+ },
544
+ },
545
+ },
546
+ signingData,
547
+ });
548
+ } else {
549
+ expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
550
+ request: requestParams.request,
551
+ displayData: {
552
+ ...displayData,
553
+ alert: {
554
+ type: AlertType.WARNING,
555
+ details: {
556
+ title: 'Suspicious Transaction',
557
+ description: 'Use caution, this transaction may be malicious.',
558
+ },
559
+ },
560
+ },
561
+ signingData,
562
+ });
563
+ }
564
+ };
@@ -5,7 +5,7 @@ import {
5
5
  type ApprovalController,
6
6
  type DisplayData,
7
7
  type SigningData,
8
- SigningDataType,
8
+ RpcMethod,
9
9
  } from '@avalabs/vm-module-types';
10
10
  import { parseRequestParams } from './schema';
11
11
  import { estimateGasLimit } from '../../utils/estimate-gas-limit';
@@ -13,15 +13,20 @@ import { getNonce } from '../../utils/get-nonce';
13
13
  import { rpcErrors } from '@metamask/rpc-errors';
14
14
  import { getProvider } from '../../utils/get-provider';
15
15
  import type { JsonRpcBatchInternal } from '@avalabs/wallets-sdk';
16
+ import { processTransactionSimulation } from '../../utils/process-transaction-simulation';
17
+ import { parseERC20TransactionType } from '../../utils/parse-erc20-transaction-type';
18
+ import { ERC20TransactionType } from '../../types';
16
19
 
17
20
  export const ethSendTransaction = async ({
18
21
  request,
19
22
  network,
20
23
  approvalController,
24
+ proxyApiUrl,
21
25
  }: {
22
26
  request: RpcRequest;
23
27
  network: Network;
24
28
  approvalController: ApprovalController;
29
+ proxyApiUrl: string;
25
30
  }) => {
26
31
  const { dappInfo, params } = request;
27
32
 
@@ -29,7 +34,7 @@ export const ethSendTransaction = async ({
29
34
  const result = parseRequestParams(params);
30
35
 
31
36
  if (!result.success) {
32
- console.error(result.error);
37
+ console.error('invalid params', result.error);
33
38
  return {
34
39
  error: rpcErrors.invalidParams('Transaction params are invalid'),
35
40
  };
@@ -87,14 +92,24 @@ export const ethSendTransaction = async ({
87
92
  }
88
93
  }
89
94
 
90
- // TODO: validate + simulate transaction
91
- // https://ava-labs.atlassian.net/browse/CP-8870
95
+ const transactionType = parseERC20TransactionType(transaction);
96
+
97
+ const { alert, balanceChange, tokenApprovals } = await processTransactionSimulation({
98
+ request,
99
+ proxyApiUrl,
100
+ chainId: network.chainId,
101
+ params: transaction,
102
+ dAppUrl: request.dappInfo.url,
103
+ });
92
104
 
93
105
  // generate display and signing data
94
- // TODO adjust title for different transaction types
95
- // https://ava-labs.atlassian.net/browse/CP-8870
106
+ let title = 'Approve Transaction';
107
+ if (transactionType === ERC20TransactionType.APPROVE) {
108
+ title = 'Token Spend Approval';
109
+ }
110
+
96
111
  const displayData: DisplayData = {
97
- title: 'Approve Transaction',
112
+ title,
98
113
  network: {
99
114
  chainId: network.chainId,
100
115
  name: network.chainName,
@@ -105,12 +120,16 @@ export const ethSendTransaction = async ({
105
120
  from: transaction.from,
106
121
  to: transaction.to,
107
122
  data: transaction.data,
123
+ type: transactionType,
108
124
  },
109
125
  networkFeeSelector: true,
126
+ alert,
127
+ balanceChange,
128
+ tokenApprovals,
110
129
  };
111
130
 
112
131
  const signingData: SigningData = {
113
- type: SigningDataType.EVM_TRANSACTION,
132
+ type: RpcMethod.ETH_SEND_TRANSACTION,
114
133
  account: transaction.from,
115
134
  chainId: network.chainId,
116
135
  data: {