@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,572 +0,0 @@
|
|
|
1
|
-
import { ethSendTransaction } from './eth-send-transaction';
|
|
2
|
-
import { parseRequestParams } from './schema';
|
|
3
|
-
import { estimateGasLimit } from '../../utils/estimate-gas-limit';
|
|
4
|
-
import { getNonce } from '../../utils/get-nonce';
|
|
5
|
-
import { rpcErrors } from '@metamask/rpc-errors';
|
|
6
|
-
import {
|
|
7
|
-
AlertType,
|
|
8
|
-
RpcMethod,
|
|
9
|
-
TokenType,
|
|
10
|
-
type ApprovalController,
|
|
11
|
-
type Network,
|
|
12
|
-
NetworkVMType,
|
|
13
|
-
} from '@avalabs/vm-module-types';
|
|
14
|
-
import { ZodError } from 'zod';
|
|
15
|
-
import { getProvider } from '../../utils/get-provider';
|
|
16
|
-
import Blockaid from '@blockaid/client';
|
|
17
|
-
|
|
18
|
-
const mockGetProvider = getProvider as jest.MockedFunction<typeof getProvider>;
|
|
19
|
-
|
|
20
|
-
const PROXY_API_URL = 'https://proxy-api.avax.network';
|
|
21
|
-
|
|
22
|
-
jest.mock('./schema');
|
|
23
|
-
jest.mock('../../utils/estimate-gas-limit');
|
|
24
|
-
jest.mock('../../utils/get-nonce');
|
|
25
|
-
jest.mock('../../utils/get-provider');
|
|
26
|
-
jest.mock('@blockaid/client', () => {
|
|
27
|
-
return jest.fn().mockImplementation(() => {
|
|
28
|
-
return {
|
|
29
|
-
evm: {
|
|
30
|
-
transaction: {
|
|
31
|
-
scan: jest.fn().mockResolvedValue({ validation: { result_type: 'Benign' } }),
|
|
32
|
-
},
|
|
33
|
-
jsonRpc: {
|
|
34
|
-
scan: jest.fn(),
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
};
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
const mockOnTransactionConfirmed = jest.fn();
|
|
42
|
-
const mockOnTransactionReverted = jest.fn();
|
|
43
|
-
const mockApprovalController: jest.Mocked<ApprovalController> = {
|
|
44
|
-
requestApproval: jest.fn(),
|
|
45
|
-
onTransactionConfirmed: mockOnTransactionConfirmed,
|
|
46
|
-
onTransactionReverted: mockOnTransactionReverted,
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
const mockParseRequestParams = parseRequestParams as jest.MockedFunction<typeof parseRequestParams>;
|
|
50
|
-
const mockEstimateGasLimit = estimateGasLimit as jest.MockedFunction<typeof estimateGasLimit>;
|
|
51
|
-
const mockGetNonce = getNonce as jest.MockedFunction<typeof getNonce>;
|
|
52
|
-
const mockSend = jest.fn();
|
|
53
|
-
const mockWaitForTransaction = jest.fn();
|
|
54
|
-
|
|
55
|
-
const mockProvider = {
|
|
56
|
-
send: mockSend,
|
|
57
|
-
waitForTransaction: mockWaitForTransaction,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// @ts-expect-error missing properties
|
|
61
|
-
mockGetProvider.mockReturnValue(mockProvider);
|
|
62
|
-
const testNetwork: Network = {
|
|
63
|
-
isTestnet: false,
|
|
64
|
-
chainId: 1,
|
|
65
|
-
chainName: 'chainName',
|
|
66
|
-
rpcUrl: 'rpcUrl',
|
|
67
|
-
logoUri: 'logoUri',
|
|
68
|
-
utilityAddresses: { multicall: 'multiContractAddress' },
|
|
69
|
-
networkToken: {
|
|
70
|
-
name: 'Ethereum',
|
|
71
|
-
symbol: 'ETH',
|
|
72
|
-
decimals: 9,
|
|
73
|
-
description: 'Ethereum Token',
|
|
74
|
-
logoUri: 'some logo uri',
|
|
75
|
-
},
|
|
76
|
-
vmName: NetworkVMType.EVM,
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const testParams = { from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12', gas: '0x5208' };
|
|
80
|
-
|
|
81
|
-
const testRequestParams = () => ({
|
|
82
|
-
request: {
|
|
83
|
-
requestId: '1',
|
|
84
|
-
sessionId: '2',
|
|
85
|
-
method: RpcMethod.ETH_SEND_TRANSACTION,
|
|
86
|
-
chainId: 'eip155:1',
|
|
87
|
-
dappInfo: { url: 'https://example.com', name: 'dapp', icon: 'icon' },
|
|
88
|
-
params: [testParams],
|
|
89
|
-
},
|
|
90
|
-
network: testNetwork,
|
|
91
|
-
approvalController: mockApprovalController,
|
|
92
|
-
proxyApiUrl: PROXY_API_URL,
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const displayData = {
|
|
96
|
-
title: 'Approve Transaction',
|
|
97
|
-
network: {
|
|
98
|
-
chainId: testNetwork.chainId,
|
|
99
|
-
name: testNetwork.chainName,
|
|
100
|
-
logoUri: testNetwork.logoUri,
|
|
101
|
-
},
|
|
102
|
-
transactionDetails: {
|
|
103
|
-
website: 'example.com',
|
|
104
|
-
from: '0xfrom',
|
|
105
|
-
to: '0xto',
|
|
106
|
-
data: '0xdata',
|
|
107
|
-
},
|
|
108
|
-
networkFeeSelector: true,
|
|
109
|
-
alert: undefined,
|
|
110
|
-
tokenApprovals: undefined,
|
|
111
|
-
balanceChange: undefined,
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const signingData = {
|
|
115
|
-
type: 'eth_sendTransaction',
|
|
116
|
-
account: '0xfrom',
|
|
117
|
-
chainId: 1,
|
|
118
|
-
data: {
|
|
119
|
-
type: 2,
|
|
120
|
-
nonce: 12,
|
|
121
|
-
gasLimit: 21000,
|
|
122
|
-
to: '0xto',
|
|
123
|
-
from: '0xfrom',
|
|
124
|
-
data: '0xdata',
|
|
125
|
-
value: '0xvalue',
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
|
|
129
|
-
const testTxHash = '0xtxhash';
|
|
130
|
-
|
|
131
|
-
describe('eth_sendTransaction handler', () => {
|
|
132
|
-
beforeEach(() => {
|
|
133
|
-
jest.clearAllMocks();
|
|
134
|
-
|
|
135
|
-
mockParseRequestParams.mockReturnValue({
|
|
136
|
-
success: true,
|
|
137
|
-
data: [testParams],
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
mockApprovalController.requestApproval.mockResolvedValue({ result: testTxHash });
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('should return error if request params are invalid', async () => {
|
|
144
|
-
mockParseRequestParams.mockReturnValue({
|
|
145
|
-
success: false,
|
|
146
|
-
error: new Error('Invalid params') as ZodError,
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
const response = await ethSendTransaction(testRequestParams());
|
|
150
|
-
|
|
151
|
-
expect(response).toEqual({
|
|
152
|
-
error: rpcErrors.invalidParams('Transaction params are invalid'),
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
it('should calculate gas limit if not provided', async () => {
|
|
157
|
-
mockParseRequestParams.mockReturnValue({
|
|
158
|
-
success: true,
|
|
159
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12' }],
|
|
160
|
-
});
|
|
161
|
-
mockEstimateGasLimit.mockResolvedValue(21000);
|
|
162
|
-
|
|
163
|
-
const requestParams = testRequestParams();
|
|
164
|
-
|
|
165
|
-
await ethSendTransaction(requestParams);
|
|
166
|
-
|
|
167
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
168
|
-
chainId: 1,
|
|
169
|
-
chainName: 'chainName',
|
|
170
|
-
rpcUrl: 'rpcUrl',
|
|
171
|
-
multiContractAddress: 'multiContractAddress',
|
|
172
|
-
pollingInterval: 1000,
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
expect(mockEstimateGasLimit).toHaveBeenCalledWith({
|
|
176
|
-
provider: mockProvider,
|
|
177
|
-
transactionParams: { from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue' },
|
|
178
|
-
});
|
|
179
|
-
|
|
180
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
181
|
-
request: requestParams.request,
|
|
182
|
-
displayData,
|
|
183
|
-
signingData,
|
|
184
|
-
});
|
|
185
|
-
});
|
|
186
|
-
|
|
187
|
-
it('should calculate nonce if not provided', async () => {
|
|
188
|
-
mockParseRequestParams.mockReturnValue({
|
|
189
|
-
success: true,
|
|
190
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', gas: '0x5208' }],
|
|
191
|
-
});
|
|
192
|
-
mockGetNonce.mockResolvedValue(12);
|
|
193
|
-
|
|
194
|
-
const requestParams = testRequestParams();
|
|
195
|
-
await ethSendTransaction(requestParams);
|
|
196
|
-
|
|
197
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
198
|
-
chainId: 1,
|
|
199
|
-
chainName: 'chainName',
|
|
200
|
-
rpcUrl: 'rpcUrl',
|
|
201
|
-
multiContractAddress: 'multiContractAddress',
|
|
202
|
-
pollingInterval: 1000,
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
expect(mockGetNonce).toHaveBeenCalledWith({
|
|
206
|
-
provider: mockProvider,
|
|
207
|
-
from: '0xfrom',
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
211
|
-
request: requestParams.request,
|
|
212
|
-
displayData,
|
|
213
|
-
signingData,
|
|
214
|
-
});
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
it('should calculate both gas and nonce if not provided', async () => {
|
|
218
|
-
mockParseRequestParams.mockReturnValue({
|
|
219
|
-
success: true,
|
|
220
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue' }],
|
|
221
|
-
});
|
|
222
|
-
mockGetNonce.mockResolvedValue(12);
|
|
223
|
-
mockEstimateGasLimit.mockResolvedValue(21000);
|
|
224
|
-
|
|
225
|
-
const requestParams = testRequestParams();
|
|
226
|
-
await ethSendTransaction(requestParams);
|
|
227
|
-
|
|
228
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
229
|
-
chainId: 1,
|
|
230
|
-
chainName: 'chainName',
|
|
231
|
-
rpcUrl: 'rpcUrl',
|
|
232
|
-
multiContractAddress: 'multiContractAddress',
|
|
233
|
-
pollingInterval: 1000,
|
|
234
|
-
});
|
|
235
|
-
|
|
236
|
-
expect(mockGetNonce).toHaveBeenCalledWith({
|
|
237
|
-
provider: mockProvider,
|
|
238
|
-
from: '0xfrom',
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
expect(mockEstimateGasLimit).toHaveBeenCalledWith({
|
|
242
|
-
provider: mockProvider,
|
|
243
|
-
transactionParams: { from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue' },
|
|
244
|
-
});
|
|
245
|
-
|
|
246
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
247
|
-
request: requestParams.request,
|
|
248
|
-
displayData,
|
|
249
|
-
signingData,
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
|
|
253
|
-
it('should add alert object with Warning type to displayData when validation result is Warning', async () => {
|
|
254
|
-
testWithValidationResultType('Warning');
|
|
255
|
-
});
|
|
256
|
-
|
|
257
|
-
it('should add alert object with Warning type to displayData when validation result is Error', async () => {
|
|
258
|
-
testWithValidationResultType('Error');
|
|
259
|
-
});
|
|
260
|
-
|
|
261
|
-
it('should add alert object with Danger type to displayData when validation result is Malicious', async () => {
|
|
262
|
-
testWithValidationResultType('Malicious');
|
|
263
|
-
});
|
|
264
|
-
|
|
265
|
-
it('should process transaction and add token approvals and balance changes to displayData', async () => {
|
|
266
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
267
|
-
(Blockaid as any).mockImplementation(() => ({
|
|
268
|
-
evm: {
|
|
269
|
-
transaction: {
|
|
270
|
-
scan: jest.fn().mockResolvedValue({
|
|
271
|
-
validation: { result_type: 'Benign' },
|
|
272
|
-
simulation: {
|
|
273
|
-
status: 'Success',
|
|
274
|
-
account_summary: {
|
|
275
|
-
exposures: [
|
|
276
|
-
{
|
|
277
|
-
asset: {
|
|
278
|
-
type: TokenType.ERC20,
|
|
279
|
-
address: '0xTokenAddress',
|
|
280
|
-
name: 'TokenName',
|
|
281
|
-
symbol: 'TKN',
|
|
282
|
-
decimals: 18,
|
|
283
|
-
logo_url: 'logo_url',
|
|
284
|
-
},
|
|
285
|
-
spenders: {
|
|
286
|
-
'0xSpenderAddress': {
|
|
287
|
-
exposure: [{ raw_value: '1', usd_price: '1' }],
|
|
288
|
-
},
|
|
289
|
-
},
|
|
290
|
-
},
|
|
291
|
-
],
|
|
292
|
-
assets_diffs: [
|
|
293
|
-
{
|
|
294
|
-
asset: {
|
|
295
|
-
name: 'TokenName',
|
|
296
|
-
symbol: 'TKN',
|
|
297
|
-
decimals: 18,
|
|
298
|
-
logo_url: 'logo_url',
|
|
299
|
-
type: TokenType.ERC20,
|
|
300
|
-
address: '0xTokenAddress',
|
|
301
|
-
},
|
|
302
|
-
in: [{ value: '1', usd_price: '1' }],
|
|
303
|
-
out: [{ value: '1', usd_price: '1' }],
|
|
304
|
-
},
|
|
305
|
-
],
|
|
306
|
-
},
|
|
307
|
-
},
|
|
308
|
-
}),
|
|
309
|
-
},
|
|
310
|
-
},
|
|
311
|
-
}));
|
|
312
|
-
|
|
313
|
-
mockParseRequestParams.mockReturnValue({
|
|
314
|
-
success: true,
|
|
315
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12', gas: '0x5208' }],
|
|
316
|
-
});
|
|
317
|
-
|
|
318
|
-
const requestParams = testRequestParams();
|
|
319
|
-
|
|
320
|
-
await ethSendTransaction(requestParams);
|
|
321
|
-
|
|
322
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
323
|
-
chainId: 1,
|
|
324
|
-
chainName: 'chainName',
|
|
325
|
-
rpcUrl: 'rpcUrl',
|
|
326
|
-
multiContractAddress: 'multiContractAddress',
|
|
327
|
-
pollingInterval: 1000,
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
331
|
-
request: requestParams.request,
|
|
332
|
-
displayData: {
|
|
333
|
-
...displayData,
|
|
334
|
-
tokenApprovals: {
|
|
335
|
-
isEditable: true,
|
|
336
|
-
approvals: [
|
|
337
|
-
{
|
|
338
|
-
token: {
|
|
339
|
-
type: TokenType.ERC20,
|
|
340
|
-
address: '0xTokenAddress',
|
|
341
|
-
name: 'TokenName',
|
|
342
|
-
symbol: 'TKN',
|
|
343
|
-
decimals: 18,
|
|
344
|
-
logoUri: 'logo_url',
|
|
345
|
-
},
|
|
346
|
-
spenderAddress: '0xSpenderAddress',
|
|
347
|
-
value: '1',
|
|
348
|
-
usdPrice: '1',
|
|
349
|
-
logoUri: 'logo_url',
|
|
350
|
-
},
|
|
351
|
-
],
|
|
352
|
-
},
|
|
353
|
-
balanceChange: {
|
|
354
|
-
ins: [
|
|
355
|
-
{
|
|
356
|
-
token: {
|
|
357
|
-
type: TokenType.ERC20,
|
|
358
|
-
address: '0xTokenAddress',
|
|
359
|
-
name: 'TokenName',
|
|
360
|
-
symbol: 'TKN',
|
|
361
|
-
decimals: 18,
|
|
362
|
-
logoUri: 'logo_url',
|
|
363
|
-
},
|
|
364
|
-
items: [
|
|
365
|
-
{
|
|
366
|
-
displayValue: '1',
|
|
367
|
-
usdPrice: '1',
|
|
368
|
-
},
|
|
369
|
-
],
|
|
370
|
-
},
|
|
371
|
-
],
|
|
372
|
-
outs: [
|
|
373
|
-
{
|
|
374
|
-
token: {
|
|
375
|
-
type: TokenType.ERC20,
|
|
376
|
-
address: '0xTokenAddress',
|
|
377
|
-
name: 'TokenName',
|
|
378
|
-
symbol: 'TKN',
|
|
379
|
-
decimals: 18,
|
|
380
|
-
logoUri: 'logo_url',
|
|
381
|
-
},
|
|
382
|
-
items: [
|
|
383
|
-
{
|
|
384
|
-
displayValue: '1',
|
|
385
|
-
usdPrice: '1',
|
|
386
|
-
},
|
|
387
|
-
],
|
|
388
|
-
},
|
|
389
|
-
],
|
|
390
|
-
},
|
|
391
|
-
},
|
|
392
|
-
signingData,
|
|
393
|
-
});
|
|
394
|
-
});
|
|
395
|
-
|
|
396
|
-
it('should return error if gas limit calculation fails', async () => {
|
|
397
|
-
mockParseRequestParams.mockReturnValue({
|
|
398
|
-
success: true,
|
|
399
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12' }],
|
|
400
|
-
});
|
|
401
|
-
|
|
402
|
-
mockEstimateGasLimit.mockRejectedValue(new Error('gas calculation error'));
|
|
403
|
-
|
|
404
|
-
const requestParams = testRequestParams();
|
|
405
|
-
const response = await ethSendTransaction(requestParams);
|
|
406
|
-
|
|
407
|
-
expect(response).toEqual({
|
|
408
|
-
error: rpcErrors.internal('Unable to calculate gas limit'),
|
|
409
|
-
});
|
|
410
|
-
});
|
|
411
|
-
|
|
412
|
-
it('should return error if nonce calculation fails', async () => {
|
|
413
|
-
mockParseRequestParams.mockReturnValue({
|
|
414
|
-
success: true,
|
|
415
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', gas: '0x5208' }],
|
|
416
|
-
});
|
|
417
|
-
mockGetNonce.mockRejectedValue(new Error('Nonce calculation error'));
|
|
418
|
-
|
|
419
|
-
const requestParams = testRequestParams();
|
|
420
|
-
const response = await ethSendTransaction(requestParams);
|
|
421
|
-
|
|
422
|
-
expect(response).toEqual({
|
|
423
|
-
error: rpcErrors.internal('Unable to calculate nonce'),
|
|
424
|
-
});
|
|
425
|
-
});
|
|
426
|
-
|
|
427
|
-
describe('approval succeeds', () => {
|
|
428
|
-
beforeEach(() => {
|
|
429
|
-
jest.clearAllMocks();
|
|
430
|
-
|
|
431
|
-
mockApprovalController.requestApproval.mockResolvedValue({ result: testTxHash });
|
|
432
|
-
mockSend.mockResolvedValue(testTxHash);
|
|
433
|
-
});
|
|
434
|
-
|
|
435
|
-
it('should broadcast the signed transaction and return transaction hash', async () => {
|
|
436
|
-
const requestParams = testRequestParams();
|
|
437
|
-
const response = await ethSendTransaction(requestParams);
|
|
438
|
-
|
|
439
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
440
|
-
chainId: 1,
|
|
441
|
-
chainName: 'chainName',
|
|
442
|
-
rpcUrl: 'rpcUrl',
|
|
443
|
-
multiContractAddress: 'multiContractAddress',
|
|
444
|
-
pollingInterval: 1000,
|
|
445
|
-
});
|
|
446
|
-
|
|
447
|
-
expect(mockSend).toHaveBeenCalledWith('eth_sendRawTransaction', [testTxHash]);
|
|
448
|
-
|
|
449
|
-
expect(response).toStrictEqual({ result: testTxHash });
|
|
450
|
-
});
|
|
451
|
-
|
|
452
|
-
it('should notify when transaction is confirmed', async () => {
|
|
453
|
-
mockWaitForTransaction.mockResolvedValue({ status: 1 });
|
|
454
|
-
|
|
455
|
-
const requestParams = testRequestParams();
|
|
456
|
-
const response = await ethSendTransaction(requestParams);
|
|
457
|
-
|
|
458
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
459
|
-
chainId: 1,
|
|
460
|
-
chainName: 'chainName',
|
|
461
|
-
rpcUrl: 'rpcUrl',
|
|
462
|
-
multiContractAddress: 'multiContractAddress',
|
|
463
|
-
pollingInterval: 1000,
|
|
464
|
-
});
|
|
465
|
-
|
|
466
|
-
expect(response).toStrictEqual({ result: testTxHash });
|
|
467
|
-
|
|
468
|
-
expect(mockWaitForTransaction).toHaveBeenCalledWith(testTxHash);
|
|
469
|
-
|
|
470
|
-
expect(mockOnTransactionConfirmed).toHaveBeenCalledWith(testTxHash);
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it('should notify when transaction is reverted', async () => {
|
|
474
|
-
mockWaitForTransaction.mockResolvedValue({ status: 0 });
|
|
475
|
-
|
|
476
|
-
const requestParams = testRequestParams();
|
|
477
|
-
const response = await ethSendTransaction(requestParams);
|
|
478
|
-
|
|
479
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
480
|
-
chainId: 1,
|
|
481
|
-
chainName: 'chainName',
|
|
482
|
-
rpcUrl: 'rpcUrl',
|
|
483
|
-
multiContractAddress: 'multiContractAddress',
|
|
484
|
-
pollingInterval: 1000,
|
|
485
|
-
});
|
|
486
|
-
|
|
487
|
-
expect(response).toStrictEqual({ result: testTxHash });
|
|
488
|
-
|
|
489
|
-
expect(mockWaitForTransaction).toHaveBeenCalledWith(testTxHash);
|
|
490
|
-
|
|
491
|
-
expect(mockOnTransactionReverted).toHaveBeenCalledWith(testTxHash);
|
|
492
|
-
});
|
|
493
|
-
});
|
|
494
|
-
|
|
495
|
-
describe('approval fails', () => {
|
|
496
|
-
it('should return error', async () => {
|
|
497
|
-
mockApprovalController.requestApproval.mockResolvedValue({ error: rpcErrors.internal('something went wrong') });
|
|
498
|
-
|
|
499
|
-
const requestParams = testRequestParams();
|
|
500
|
-
const response = await ethSendTransaction(requestParams);
|
|
501
|
-
|
|
502
|
-
expect(response).toStrictEqual({ error: rpcErrors.internal('something went wrong') });
|
|
503
|
-
});
|
|
504
|
-
});
|
|
505
|
-
});
|
|
506
|
-
|
|
507
|
-
const testWithValidationResultType = async (resultType: 'Warning' | 'Error' | 'Malicious') => {
|
|
508
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
509
|
-
(Blockaid as any).mockImplementation(() => ({
|
|
510
|
-
evm: {
|
|
511
|
-
transaction: {
|
|
512
|
-
scan: jest.fn().mockResolvedValue({
|
|
513
|
-
validation: { result_type: resultType },
|
|
514
|
-
simulation: { status: 'Success', account_summary: { exposures: [], assets_diffs: [] } },
|
|
515
|
-
}),
|
|
516
|
-
},
|
|
517
|
-
},
|
|
518
|
-
}));
|
|
519
|
-
|
|
520
|
-
mockParseRequestParams.mockReturnValue({
|
|
521
|
-
success: true,
|
|
522
|
-
data: [{ from: '0xfrom', to: '0xto', data: '0xdata', value: '0xvalue', nonce: '12', gas: '0x5208' }],
|
|
523
|
-
});
|
|
524
|
-
|
|
525
|
-
const requestParams = testRequestParams();
|
|
526
|
-
|
|
527
|
-
await ethSendTransaction(requestParams);
|
|
528
|
-
|
|
529
|
-
expect(mockGetProvider).toHaveBeenCalledWith({
|
|
530
|
-
chainId: 1,
|
|
531
|
-
chainName: 'chainName',
|
|
532
|
-
rpcUrl: 'rpcUrl',
|
|
533
|
-
multiContractAddress: 'multiContractAddress',
|
|
534
|
-
pollingInterval: 1000,
|
|
535
|
-
});
|
|
536
|
-
|
|
537
|
-
if (resultType === 'Malicious') {
|
|
538
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
539
|
-
request: requestParams.request,
|
|
540
|
-
displayData: {
|
|
541
|
-
...displayData,
|
|
542
|
-
alert: {
|
|
543
|
-
type: AlertType.DANGER,
|
|
544
|
-
details: {
|
|
545
|
-
title: 'Scam Transaction',
|
|
546
|
-
description: 'This transaction is malicious, do not proceed.',
|
|
547
|
-
actionTitles: {
|
|
548
|
-
reject: 'Reject Transaction',
|
|
549
|
-
proceed: 'Proceed Anyway',
|
|
550
|
-
},
|
|
551
|
-
},
|
|
552
|
-
},
|
|
553
|
-
},
|
|
554
|
-
signingData,
|
|
555
|
-
});
|
|
556
|
-
} else {
|
|
557
|
-
expect(mockApprovalController.requestApproval).toHaveBeenCalledWith({
|
|
558
|
-
request: requestParams.request,
|
|
559
|
-
displayData: {
|
|
560
|
-
...displayData,
|
|
561
|
-
alert: {
|
|
562
|
-
type: AlertType.WARNING,
|
|
563
|
-
details: {
|
|
564
|
-
title: 'Suspicious Transaction',
|
|
565
|
-
description: 'Use caution, this transaction may be malicious.',
|
|
566
|
-
},
|
|
567
|
-
},
|
|
568
|
-
},
|
|
569
|
-
signingData,
|
|
570
|
-
});
|
|
571
|
-
}
|
|
572
|
-
};
|