@metamask-previews/transaction-controller 56.3.0-preview-e9f8bc88 → 57.0.0-preview-39fa8d37
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/CHANGELOG.md +13 -1
- package/dist/TransactionController.cjs +17 -55
- package/dist/TransactionController.cjs.map +1 -1
- package/dist/TransactionController.d.cts.map +1 -1
- package/dist/TransactionController.d.mts.map +1 -1
- package/dist/TransactionController.mjs +20 -58
- package/dist/TransactionController.mjs.map +1 -1
- package/dist/api/simulation-api.cjs.map +1 -1
- package/dist/api/simulation-api.d.cts +22 -5
- package/dist/api/simulation-api.d.cts.map +1 -1
- package/dist/api/simulation-api.d.mts +22 -5
- package/dist/api/simulation-api.d.mts.map +1 -1
- package/dist/api/simulation-api.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +2 -0
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +2 -0
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/dist/utils/{simulation.cjs → balance-changes.cjs} +103 -112
- package/dist/utils/balance-changes.cjs.map +1 -0
- package/dist/utils/balance-changes.d.cts +30 -0
- package/dist/utils/balance-changes.d.cts.map +1 -0
- package/dist/utils/balance-changes.d.mts +30 -0
- package/dist/utils/balance-changes.d.mts.map +1 -0
- package/dist/utils/{simulation.mjs → balance-changes.mjs} +107 -111
- package/dist/utils/balance-changes.mjs.map +1 -0
- package/dist/utils/gas-fee-tokens.cjs +123 -0
- package/dist/utils/gas-fee-tokens.cjs.map +1 -0
- package/dist/utils/gas-fee-tokens.d.cts +22 -0
- package/dist/utils/gas-fee-tokens.d.cts.map +1 -0
- package/dist/utils/gas-fee-tokens.d.mts +22 -0
- package/dist/utils/gas-fee-tokens.d.mts.map +1 -0
- package/dist/utils/gas-fee-tokens.mjs +119 -0
- package/dist/utils/gas-fee-tokens.mjs.map +1 -0
- package/package.json +3 -3
- package/dist/utils/simulation.cjs.map +0 -1
- package/dist/utils/simulation.d.cts +0 -60
- package/dist/utils/simulation.d.cts.map +0 -1
- package/dist/utils/simulation.d.mts +0 -60
- package/dist/utils/simulation.d.mts.map +0 -1
- package/dist/utils/simulation.mjs.map +0 -1
|
@@ -1,10 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
6
|
+
exports.getBalanceChanges = exports.SupportedToken = void 0;
|
|
4
7
|
const abi_1 = require("@ethersproject/abi");
|
|
5
8
|
const controller_utils_1 = require("@metamask/controller-utils");
|
|
6
9
|
const metamask_eth_abis_1 = require("@metamask/metamask-eth-abis");
|
|
7
10
|
const utils_1 = require("@metamask/utils");
|
|
11
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
8
12
|
const simulation_api_1 = require("../api/simulation-api.cjs");
|
|
9
13
|
const constants_1 = require("../constants.cjs");
|
|
10
14
|
const errors_1 = require("../errors.cjs");
|
|
@@ -18,7 +22,7 @@ var SupportedToken;
|
|
|
18
22
|
SupportedToken["ERC20_WRAPPED"] = "erc20Wrapped";
|
|
19
23
|
SupportedToken["ERC721_LEGACY"] = "erc721Legacy";
|
|
20
24
|
})(SupportedToken || (exports.SupportedToken = SupportedToken = {}));
|
|
21
|
-
const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, '
|
|
25
|
+
const log = (0, utils_1.createModuleLogger)(logger_1.projectLogger, 'balance-changes');
|
|
22
26
|
const SUPPORTED_EVENTS = [
|
|
23
27
|
'Transfer',
|
|
24
28
|
'TransferSingle',
|
|
@@ -58,102 +62,71 @@ const REVERTED_ERRORS = ['execution reverted', 'insufficient funds for gas'];
|
|
|
58
62
|
* @param request.to - The recipient of the transaction.
|
|
59
63
|
* @param request.value - The value of the transaction.
|
|
60
64
|
* @param request.data - The data of the transaction.
|
|
61
|
-
* @param options - Additional options.
|
|
62
|
-
* @param options.blockTime - An optional block time to simulate the transaction at.
|
|
63
65
|
* @returns The simulation data.
|
|
64
66
|
*/
|
|
65
|
-
async function
|
|
66
|
-
|
|
67
|
-
const { use7702Fees } = options;
|
|
68
|
-
log('Getting simulation data', { request, options });
|
|
67
|
+
async function getBalanceChanges(request) {
|
|
68
|
+
log('Request', request);
|
|
69
69
|
try {
|
|
70
70
|
const response = await baseRequest({
|
|
71
|
-
|
|
72
|
-
from,
|
|
73
|
-
options,
|
|
71
|
+
request,
|
|
74
72
|
params: {
|
|
75
|
-
suggestFees: {
|
|
76
|
-
withFeeTransfer: true,
|
|
77
|
-
withTransfer: true,
|
|
78
|
-
...(use7702Fees ? { with7702: true } : {}),
|
|
79
|
-
},
|
|
80
73
|
withCallTrace: true,
|
|
81
74
|
withLogs: true,
|
|
82
75
|
},
|
|
83
|
-
transactions: [
|
|
84
|
-
{
|
|
85
|
-
authorizationList,
|
|
86
|
-
data,
|
|
87
|
-
from,
|
|
88
|
-
to,
|
|
89
|
-
value,
|
|
90
|
-
},
|
|
91
|
-
],
|
|
92
76
|
});
|
|
93
77
|
const transactionError = response.transactions?.[0]?.error;
|
|
94
78
|
if (transactionError) {
|
|
95
79
|
throw new errors_1.SimulationError(transactionError);
|
|
96
80
|
}
|
|
97
|
-
const nativeBalanceChange = getNativeBalanceChange(request
|
|
81
|
+
const nativeBalanceChange = getNativeBalanceChange(request, response);
|
|
98
82
|
const events = getEvents(response);
|
|
99
83
|
log('Parsed events', events);
|
|
100
|
-
const tokenBalanceChanges = await getTokenBalanceChanges(request, events
|
|
84
|
+
const tokenBalanceChanges = await getTokenBalanceChanges(request, events);
|
|
101
85
|
const simulationData = {
|
|
102
86
|
nativeBalanceChange,
|
|
103
87
|
tokenBalanceChanges,
|
|
104
88
|
};
|
|
105
|
-
|
|
106
|
-
try {
|
|
107
|
-
gasFeeTokens = getGasFeeTokens(response);
|
|
108
|
-
}
|
|
109
|
-
catch (error) {
|
|
110
|
-
log('Failed to parse gas fee tokens', error, response);
|
|
111
|
-
}
|
|
112
|
-
return {
|
|
113
|
-
gasFeeTokens,
|
|
114
|
-
simulationData,
|
|
115
|
-
};
|
|
89
|
+
return simulationData;
|
|
116
90
|
}
|
|
117
91
|
catch (error) {
|
|
118
|
-
log('Failed to get
|
|
92
|
+
log('Failed to get balance changes', error, request);
|
|
119
93
|
let simulationError = error;
|
|
120
94
|
if (REVERTED_ERRORS.some((revertErrorMessage) => simulationError.message?.includes(revertErrorMessage))) {
|
|
121
95
|
simulationError = new errors_1.SimulationRevertedError();
|
|
122
96
|
}
|
|
123
97
|
const { code, message } = simulationError;
|
|
124
98
|
return {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
code,
|
|
130
|
-
message,
|
|
131
|
-
},
|
|
99
|
+
tokenBalanceChanges: [],
|
|
100
|
+
error: {
|
|
101
|
+
code,
|
|
102
|
+
message,
|
|
132
103
|
},
|
|
133
104
|
};
|
|
134
105
|
}
|
|
135
106
|
}
|
|
136
|
-
exports.
|
|
107
|
+
exports.getBalanceChanges = getBalanceChanges;
|
|
137
108
|
/**
|
|
138
109
|
* Extract the native balance change from a simulation response.
|
|
139
110
|
*
|
|
140
|
-
* @param
|
|
141
|
-
* @param response -
|
|
142
|
-
* @returns
|
|
111
|
+
* @param request - Simulation request.
|
|
112
|
+
* @param response - Simulation response.
|
|
113
|
+
* @returns Native balance change or undefined if unchanged.
|
|
143
114
|
*/
|
|
144
|
-
function getNativeBalanceChange(
|
|
115
|
+
function getNativeBalanceChange(request, response) {
|
|
145
116
|
const transactionResponse = response.transactions[0];
|
|
146
117
|
/* istanbul ignore next */
|
|
147
118
|
if (!transactionResponse) {
|
|
148
119
|
return undefined;
|
|
149
120
|
}
|
|
121
|
+
const { txParams } = request;
|
|
122
|
+
const userAddress = txParams.from;
|
|
150
123
|
const { stateDiff } = transactionResponse;
|
|
151
124
|
const previousBalance = stateDiff?.pre?.[userAddress]?.balance;
|
|
152
125
|
const newBalance = stateDiff?.post?.[userAddress]?.balance;
|
|
153
126
|
if (!previousBalance || !newBalance) {
|
|
154
127
|
return undefined;
|
|
155
128
|
}
|
|
156
|
-
return getSimulationBalanceChange(previousBalance, newBalance);
|
|
129
|
+
return getSimulationBalanceChange(previousBalance, newBalance, transactionResponse.gasCost);
|
|
157
130
|
}
|
|
158
131
|
/**
|
|
159
132
|
* Extract events from a simulation response.
|
|
@@ -196,7 +169,6 @@ function getEvents(response) {
|
|
|
196
169
|
})
|
|
197
170
|
.filter((e) => e !== undefined);
|
|
198
171
|
}
|
|
199
|
-
exports.getEvents = getEvents;
|
|
200
172
|
/**
|
|
201
173
|
* Normalize event arguments using ABI input definitions.
|
|
202
174
|
*
|
|
@@ -233,30 +205,24 @@ function normalizeEventArgValue(value) {
|
|
|
233
205
|
*
|
|
234
206
|
* @param request - The transaction that was simulated.
|
|
235
207
|
* @param events - The parsed events.
|
|
236
|
-
* @param options - Additional options.
|
|
237
|
-
* @param options.blockTime - An optional block time to simulate the transaction at.
|
|
238
208
|
* @returns An array of token balance changes.
|
|
239
209
|
*/
|
|
240
|
-
async function getTokenBalanceChanges(request, events
|
|
241
|
-
const {
|
|
210
|
+
async function getTokenBalanceChanges(request, events) {
|
|
211
|
+
const { txParams } = request;
|
|
212
|
+
const from = txParams.from;
|
|
242
213
|
const balanceTxs = getTokenBalanceTransactions(request, events);
|
|
243
214
|
log('Generated balance transactions', [...balanceTxs.after.values()]);
|
|
244
|
-
const
|
|
245
|
-
|
|
246
|
-
request,
|
|
247
|
-
...balanceTxs.after.values(),
|
|
248
|
-
];
|
|
249
|
-
if (transactions.length === 1) {
|
|
215
|
+
const transactionCount = balanceTxs.before.size + balanceTxs.after.size + 1;
|
|
216
|
+
if (transactionCount === 1) {
|
|
250
217
|
return [];
|
|
251
218
|
}
|
|
252
219
|
const response = await baseRequest({
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
transactions,
|
|
220
|
+
request,
|
|
221
|
+
before: [...balanceTxs.before.values()],
|
|
222
|
+
after: [...balanceTxs.after.values()],
|
|
257
223
|
});
|
|
258
224
|
log('Balance simulation response', response);
|
|
259
|
-
if (response.transactions.length !==
|
|
225
|
+
if (response.transactions.length !== transactionCount) {
|
|
260
226
|
throw new errors_1.SimulationInvalidResponseError();
|
|
261
227
|
}
|
|
262
228
|
let prevBalanceTxIndex = 0;
|
|
@@ -265,10 +231,10 @@ async function getTokenBalanceChanges(request, events, options) {
|
|
|
265
231
|
const previousBalanceCheckSkipped = !balanceTxs.before.get(token);
|
|
266
232
|
const previousBalance = previousBalanceCheckSkipped
|
|
267
233
|
? '0x0'
|
|
268
|
-
: getAmountFromBalanceTransactionResult(
|
|
234
|
+
: getAmountFromBalanceTransactionResult(from, token,
|
|
269
235
|
// eslint-disable-next-line no-plusplus
|
|
270
236
|
response.transactions[prevBalanceTxIndex++]);
|
|
271
|
-
const newBalance = getAmountFromBalanceTransactionResult(
|
|
237
|
+
const newBalance = getAmountFromBalanceTransactionResult(from, token, response.transactions[index + balanceTxs.before.size + 1]);
|
|
272
238
|
const balanceChange = getSimulationBalanceChange(previousBalance, newBalance);
|
|
273
239
|
if (!balanceChange) {
|
|
274
240
|
return undefined;
|
|
@@ -291,7 +257,8 @@ function getTokenBalanceTransactions(request, events) {
|
|
|
291
257
|
const tokenKeys = new Set();
|
|
292
258
|
const before = new Map();
|
|
293
259
|
const after = new Map();
|
|
294
|
-
const
|
|
260
|
+
const from = request.txParams.from;
|
|
261
|
+
const userEvents = events.filter((event) => [event.args.from, event.args.to].includes(from));
|
|
295
262
|
log('Filtered user events', userEvents);
|
|
296
263
|
for (const event of userEvents) {
|
|
297
264
|
const tokenIds = getEventTokenIds(event);
|
|
@@ -308,9 +275,9 @@ function getTokenBalanceTransactions(request, events) {
|
|
|
308
275
|
continue;
|
|
309
276
|
}
|
|
310
277
|
tokenKeys.add(tokenKey);
|
|
311
|
-
const data = getBalanceTransactionData(event.tokenStandard,
|
|
278
|
+
const data = getBalanceTransactionData(event.tokenStandard, from, tokenId);
|
|
312
279
|
const transaction = {
|
|
313
|
-
from
|
|
280
|
+
from,
|
|
314
281
|
to: event.contractAddress,
|
|
315
282
|
data,
|
|
316
283
|
};
|
|
@@ -466,10 +433,13 @@ function extractLogs(call) {
|
|
|
466
433
|
*
|
|
467
434
|
* @param previousBalance - The previous balance.
|
|
468
435
|
* @param newBalance - The new balance.
|
|
436
|
+
* @param offset - Optional offset to apply to the new balance.
|
|
469
437
|
* @returns The balance change data or undefined if unchanged.
|
|
470
438
|
*/
|
|
471
|
-
function getSimulationBalanceChange(previousBalance, newBalance) {
|
|
472
|
-
const
|
|
439
|
+
function getSimulationBalanceChange(previousBalance, newBalance, offset = 0) {
|
|
440
|
+
const newBalanceBN = (0, controller_utils_1.hexToBN)(newBalance).add(new bn_js_1.default(offset));
|
|
441
|
+
const previousBalanceBN = (0, controller_utils_1.hexToBN)(previousBalance);
|
|
442
|
+
const differenceBN = newBalanceBN.sub(previousBalanceBN);
|
|
473
443
|
const isDecrease = differenceBN.isNeg();
|
|
474
444
|
const difference = (0, controller_utils_1.toHex)(differenceBN.abs());
|
|
475
445
|
if (differenceBN.isZero()) {
|
|
@@ -478,7 +448,7 @@ function getSimulationBalanceChange(previousBalance, newBalance) {
|
|
|
478
448
|
}
|
|
479
449
|
return {
|
|
480
450
|
previousBalance,
|
|
481
|
-
newBalance,
|
|
451
|
+
newBalance: (0, controller_utils_1.toHex)(newBalanceBN),
|
|
482
452
|
difference,
|
|
483
453
|
isDecrease,
|
|
484
454
|
};
|
|
@@ -496,61 +466,82 @@ function getContractInterfaces() {
|
|
|
496
466
|
return [tokenType, contractInterface];
|
|
497
467
|
}));
|
|
498
468
|
}
|
|
499
|
-
/**
|
|
500
|
-
* Extract gas fee tokens from a simulation response.
|
|
501
|
-
*
|
|
502
|
-
* @param response - The simulation response.
|
|
503
|
-
* @returns An array of gas fee tokens.
|
|
504
|
-
*/
|
|
505
|
-
function getGasFeeTokens(response) {
|
|
506
|
-
const feeLevel = response.transactions?.[0]
|
|
507
|
-
?.fees?.[0];
|
|
508
|
-
const tokenFees = feeLevel?.tokenFees ?? [];
|
|
509
|
-
return tokenFees.map((tokenFee) => ({
|
|
510
|
-
amount: tokenFee.balanceNeededToken,
|
|
511
|
-
balance: tokenFee.currentBalanceToken,
|
|
512
|
-
decimals: tokenFee.token.decimals,
|
|
513
|
-
gas: feeLevel.gas,
|
|
514
|
-
gasTransfer: tokenFee.transferEstimate,
|
|
515
|
-
maxFeePerGas: feeLevel.maxFeePerGas,
|
|
516
|
-
maxPriorityFeePerGas: feeLevel.maxPriorityFeePerGas,
|
|
517
|
-
rateWei: tokenFee.rateWei,
|
|
518
|
-
recipient: tokenFee.feeRecipient,
|
|
519
|
-
symbol: tokenFee.token.symbol,
|
|
520
|
-
tokenAddress: tokenFee.token.address,
|
|
521
|
-
}));
|
|
522
|
-
}
|
|
523
469
|
/**
|
|
524
470
|
* Base request to simulation API.
|
|
525
471
|
*
|
|
526
|
-
* @param
|
|
527
|
-
* @param
|
|
528
|
-
* @param
|
|
529
|
-
* @param
|
|
530
|
-
* @param request
|
|
531
|
-
* @param request.transactions - Transactions to simulate.
|
|
472
|
+
* @param options - Options bag.
|
|
473
|
+
* @param options.after - Transactions to simulate after user's transaction.
|
|
474
|
+
* @param options.before - Transactions to simulate before user's transaction.
|
|
475
|
+
* @param options.params - Additional parameters for the request.
|
|
476
|
+
* @param options.request - Original request object.
|
|
532
477
|
* @returns The simulation response.
|
|
533
478
|
*/
|
|
534
|
-
async function baseRequest({
|
|
535
|
-
const { blockTime,
|
|
479
|
+
async function baseRequest({ request, params, before = [], after = [], }) {
|
|
480
|
+
const { blockTime, chainId, ethQuery, txParams } = request;
|
|
481
|
+
const { authorizationList } = txParams;
|
|
482
|
+
const from = txParams.from;
|
|
483
|
+
const authorizationListFinal = authorizationList?.map((authorization) => ({
|
|
484
|
+
address: authorization.address,
|
|
485
|
+
from,
|
|
486
|
+
}));
|
|
487
|
+
const userTransaction = {
|
|
488
|
+
authorizationList: authorizationListFinal,
|
|
489
|
+
data: txParams.data,
|
|
490
|
+
from: txParams.from,
|
|
491
|
+
gas: txParams.gas,
|
|
492
|
+
maxFeePerGas: (txParams.maxFeePerGas ?? txParams.gasPrice),
|
|
493
|
+
maxPriorityFeePerGas: (txParams.maxPriorityFeePerGas ??
|
|
494
|
+
txParams.gasPrice),
|
|
495
|
+
to: txParams.to,
|
|
496
|
+
value: txParams.value,
|
|
497
|
+
};
|
|
498
|
+
const transactions = [...before, userTransaction, ...after];
|
|
499
|
+
const requiredBalanceBN = getRequiredBalance(request);
|
|
500
|
+
const requiredBalanceHex = (0, controller_utils_1.toHex)(requiredBalanceBN);
|
|
501
|
+
log('Required balance', requiredBalanceHex);
|
|
502
|
+
const currentBalanceHex = (await (0, controller_utils_1.query)(ethQuery, 'getBalance', [
|
|
503
|
+
from,
|
|
504
|
+
'latest',
|
|
505
|
+
]));
|
|
506
|
+
const currentBalanceBN = (0, controller_utils_1.hexToBN)(currentBalanceHex);
|
|
507
|
+
log('Current balance', currentBalanceHex);
|
|
508
|
+
const isInsufficientBalance = currentBalanceBN.lt(requiredBalanceBN);
|
|
536
509
|
return await (0, simulation_api_1.simulateTransactions)(chainId, {
|
|
537
|
-
transactions,
|
|
538
510
|
...params,
|
|
511
|
+
transactions,
|
|
512
|
+
withGas: true,
|
|
513
|
+
withDefaultBlockOverrides: true,
|
|
539
514
|
...(blockTime && {
|
|
540
515
|
blockOverrides: {
|
|
541
516
|
...params?.blockOverrides,
|
|
542
517
|
time: (0, controller_utils_1.toHex)(blockTime),
|
|
543
518
|
},
|
|
544
519
|
}),
|
|
545
|
-
...(
|
|
520
|
+
...(isInsufficientBalance && {
|
|
546
521
|
overrides: {
|
|
547
522
|
...params?.overrides,
|
|
548
523
|
[from]: {
|
|
549
524
|
...params?.overrides?.[from],
|
|
550
|
-
|
|
525
|
+
balance: requiredBalanceHex,
|
|
551
526
|
},
|
|
552
527
|
},
|
|
553
528
|
}),
|
|
554
529
|
});
|
|
555
530
|
}
|
|
556
|
-
|
|
531
|
+
/**
|
|
532
|
+
* Calculate the required minimum balance for a transaction.
|
|
533
|
+
*
|
|
534
|
+
* @param request - The transaction request.
|
|
535
|
+
* @returns The minimal balance as a BN.
|
|
536
|
+
*/
|
|
537
|
+
function getRequiredBalance(request) {
|
|
538
|
+
const { txParams } = request;
|
|
539
|
+
const gasLimit = (0, controller_utils_1.hexToBN)(txParams.gas ?? '0x0');
|
|
540
|
+
const gasPrice = (0, controller_utils_1.hexToBN)(txParams.maxFeePerGas ?? txParams.gasPrice ?? '0x0');
|
|
541
|
+
const value = (0, controller_utils_1.hexToBN)(txParams.value ?? '0x0');
|
|
542
|
+
const nestedValue = (request.nestedTransactions ?? [])
|
|
543
|
+
.map((tx) => (0, controller_utils_1.hexToBN)(tx.value ?? '0x0'))
|
|
544
|
+
.reduce((acc, val) => acc.add(val), new bn_js_1.default(0));
|
|
545
|
+
return gasLimit.mul(gasPrice).add(value).add(nestedValue);
|
|
546
|
+
}
|
|
547
|
+
//# sourceMappingURL=balance-changes.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance-changes.cjs","sourceRoot":"","sources":["../../src/utils/balance-changes.ts"],"names":[],"mappings":";;;;;;AACA,4CAA+C;AAC/C,iEAAmE;AAEnE,mEAA8E;AAC9E,2CAA+D;AAC/D,kDAAuB;AAEvB,8DAA6D;AAS7D,gDAGsB;AACtB,0CAImB;AACnB,0CAA0C;AAS1C,wCAAmD;AAEnD,IAAY,cAMX;AAND,WAAY,cAAc;IACxB,iCAAe,CAAA;IACf,mCAAiB,CAAA;IACjB,qCAAmB,CAAA;IACnB,gDAA8B,CAAA;IAC9B,gDAA8B,CAAA;AAChC,CAAC,EANW,cAAc,8BAAd,cAAc,QAMzB;AAoBD,MAAM,GAAG,GAAG,IAAA,0BAAkB,EAAC,sBAAa,EA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EAAE;gBACT,GAAG,MAAM,EAAE,SAAS;gBACpB,CAAC,IAAI,CAAC,EAAE;oBACN,GAAG,MAAM,EAAE,SAAS,EAAE,CAAC,IAAI,CAAC;oBAC5B,OAAO,EAAE,kBAAkB;iBAC5B;aACF;SACF,CAAC;KACH,CAAC,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,SAAS,kBAAkB,CAAC,OAAiC;IAC3D,MAAM,EAAE,QAAQ,EAAE,GAAG,OAAO,CAAC;IAC7B,MAAM,QAAQ,GAAG,IAAA,0BAAO,EAAC,QAAQ,CAAC,GAAG,IAAI,KAAK,CAAC,CAAC;IAChD,MAAM,QAAQ,GAAG,IAAA,0BAAO,EAAC,QAAQ,CAAC,YAAY,IAAI,QAAQ,CAAC,QAAQ,IAAI,KAAK,CAAC,CAAC;IAC9E,MAAM,KAAK,GAAG,IAAA,0BAAO,EAAC,QAAQ,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC;IAE/C,MAAM,WAAW,GAAG,CAAC,OAAO,CAAC,kBAAkB,IAAI,EAAE,CAAC;SACnD,GAAG,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,IAAA,0BAAO,EAAC,EAAE,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC;SACvC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,IAAI,eAAE,CAAC,CAAC,CAAC,CAAC,CAAC;IAEjD,OAAO,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;AAC5D,CAAC","sourcesContent":["import type { Fragment, LogDescription, Result } from '@ethersproject/abi';\nimport { Interface } from '@ethersproject/abi';\nimport { hexToBN, query, toHex } from '@metamask/controller-utils';\nimport type EthQuery from '@metamask/eth-query';\nimport { abiERC20, abiERC721, abiERC1155 } from '@metamask/metamask-eth-abis';\nimport { createModuleLogger, type Hex } from '@metamask/utils';\nimport BN from 'bn.js';\n\nimport { simulateTransactions } from '../api/simulation-api';\nimport type {\n SimulationResponseLog,\n SimulationRequestTransaction,\n SimulationResponse,\n SimulationResponseCallTrace,\n SimulationResponseTransaction,\n SimulationRequest,\n} from '../api/simulation-api';\nimport {\n ABI_SIMULATION_ERC20_WRAPPED,\n ABI_SIMULATION_ERC721_LEGACY,\n} from '../constants';\nimport {\n SimulationError,\n SimulationInvalidResponseError,\n SimulationRevertedError,\n} from '../errors';\nimport { projectLogger } from '../logger';\nimport type {\n SimulationBalanceChange,\n SimulationData,\n SimulationTokenBalanceChange,\n SimulationToken,\n TransactionParams,\n NestedTransactionMetadata,\n} from '../types';\nimport { SimulationTokenStandard } from '../types';\n\nexport enum SupportedToken {\n ERC20 = 'erc20',\n ERC721 = 'erc721',\n ERC1155 = 'erc1155',\n ERC20_WRAPPED = 'erc20Wrapped',\n ERC721_LEGACY = 'erc721Legacy',\n}\n\ntype ABI = Fragment[];\n\nexport type GetBalanceChangesRequest = {\n blockTime?: number;\n chainId: Hex;\n ethQuery: EthQuery;\n nestedTransactions?: NestedTransactionMetadata[];\n txParams: TransactionParams;\n};\n\ntype ParsedEvent = {\n contractAddress: Hex;\n tokenStandard: SimulationTokenStandard;\n name: string;\n args: Record<string, Hex | Hex[]>;\n abi: ABI;\n};\n\nconst log = createModuleLogger(projectLogger, 'balance-changes');\n\nconst SUPPORTED_EVENTS = [\n 'Transfer',\n 'TransferSingle',\n 'TransferBatch',\n 'Deposit',\n 'Withdrawal',\n];\n\nconst SUPPORTED_TOKEN_ABIS = {\n [SupportedToken.ERC20]: {\n abi: abiERC20,\n standard: SimulationTokenStandard.erc20,\n },\n [SupportedToken.ERC721]: {\n abi: abiERC721,\n standard: SimulationTokenStandard.erc721,\n },\n [SupportedToken.ERC1155]: {\n abi: abiERC1155,\n standard: SimulationTokenStandard.erc1155,\n },\n [SupportedToken.ERC20_WRAPPED]: {\n abi: ABI_SIMULATION_ERC20_WRAPPED,\n standard: SimulationTokenStandard.erc20,\n },\n [SupportedToken.ERC721_LEGACY]: {\n abi: ABI_SIMULATION_ERC721_LEGACY,\n standard: SimulationTokenStandard.erc721,\n },\n};\n\nconst REVERTED_ERRORS = ['execution reverted', 'insufficient funds for gas'];\n\ntype BalanceTransactionMap = Map<SimulationToken, SimulationRequestTransaction>;\n\n/**\n * Generate simulation data for a transaction.\n *\n * @param request - The transaction to simulate.\n * @param request.chainId - The chain ID of the transaction.\n * @param request.from - The sender of the transaction.\n * @param request.to - The recipient of the transaction.\n * @param request.value - The value of the transaction.\n * @param request.data - The data of the transaction.\n * @returns The simulation data.\n */\nexport async function getBalanceChanges(\n request: GetBalanceChangesRequest,\n): Promise<SimulationData> {\n log('Request', request);\n\n try {\n const response = await baseRequest({\n request,\n params: {\n withCallTrace: true,\n withLogs: true,\n },\n });\n\n const transactionError = response.transactions?.[0]?.error;\n\n if (transactionError) {\n throw new SimulationError(transactionError);\n }\n\n const nativeBalanceChange = getNativeBalanceChange(request, response);\n const events = getEvents(response);\n\n log('Parsed events', events);\n\n const tokenBalanceChanges = await getTokenBalanceChanges(request, events);\n\n const simulationData = {\n nativeBalanceChange,\n tokenBalanceChanges,\n };\n\n return simulationData;\n } catch (error) {\n log('Failed to get balance changes', error, request);\n\n let simulationError = error as SimulationError;\n\n if (\n REVERTED_ERRORS.some((revertErrorMessage) =>\n simulationError.message?.includes(revertErrorMessage),\n )\n ) {\n simulationError = new SimulationRevertedError();\n }\n\n const { code, message } = simulationError;\n\n return {\n tokenBalanceChanges: [],\n error: {\n code,\n message,\n },\n };\n }\n}\n\n/**\n * Extract the native balance change from a simulation response.\n *\n * @param request - Simulation request.\n * @param response - Simulation response.\n * @returns Native balance change or undefined if unchanged.\n */\nfunction getNativeBalanceChange(\n request: GetBalanceChangesRequest,\n response: SimulationResponse,\n): SimulationBalanceChange | undefined {\n const transactionResponse = response.transactions[0];\n\n /* istanbul ignore next */\n if (!transactionResponse) {\n return undefined;\n }\n\n const { txParams } = request;\n const userAddress = txParams.from as Hex;\n const { stateDiff } = transactionResponse;\n const previousBalance = stateDiff?.pre?.[userAddress]?.balance;\n const newBalance = stateDiff?.post?.[userAddress]?.balance;\n\n if (!previousBalance || !newBalance) {\n return undefined;\n }\n\n return getSimulationBalanceChange(\n previousBalance,\n newBalance,\n transactionResponse.gasCost,\n );\n}\n\n/**\n * Extract events from a simulation response.\n *\n * @param response - The simulation response.\n * @returns The parsed events.\n */\nfunction getEvents(response: SimulationResponse): ParsedEvent[] {\n /* istanbul ignore next */\n const logs = extractLogs(\n response.transactions[0]?.callTrace ?? ({} as SimulationResponseCallTrace),\n );\n\n log('Extracted logs', logs);\n\n const interfaces = getContractInterfaces();\n\n return logs\n .map((currentLog) => {\n const event = parseLog(currentLog, interfaces);\n\n if (!event) {\n log('Failed to parse log', currentLog);\n return undefined;\n }\n\n /* istanbul ignore next */\n const inputs = event.abi.find((e) => e.name === event.name)?.inputs;\n\n /* istanbul ignore if */\n if (!inputs) {\n log('Failed to find inputs for event', event);\n return undefined;\n }\n\n if (!SUPPORTED_EVENTS.includes(event.name)) {\n log('Ignoring unsupported event', event.name, event);\n return undefined;\n }\n\n log('Normalizing event args', event.name, event);\n\n const args = normalizeEventArgs(event.args, inputs);\n\n return {\n contractAddress: currentLog.address,\n tokenStandard: event.standard,\n name: event.name,\n args,\n abi: event.abi,\n };\n })\n .filter((e) => e !== undefined) as ParsedEvent[];\n}\n\n/**\n * Normalize event arguments using ABI input definitions.\n *\n * @param args - The raw event arguments.\n * @param abiInputs - The ABI input definitions.\n * @returns The normalized event arguments.\n */\nfunction normalizeEventArgs(\n args: Result,\n abiInputs: { name: string }[],\n): Record<string, Hex | Hex[]> {\n return args.reduce((result, arg, index) => {\n const name = abiInputs[index].name.replace('_', '');\n const value = normalizeEventArgValue(arg);\n\n result[name] = value;\n\n return result;\n }, {});\n}\n\n/**\n * Normalize an event argument value.\n *\n * @param value - The event argument value.\n * @returns The normalized event argument value.\n */\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nfunction normalizeEventArgValue(value: any): any {\n if (Array.isArray(value)) {\n return value.map(normalizeEventArgValue);\n }\n\n let normalizedValue = value;\n\n normalizedValue = normalizedValue.toHexString?.() ?? normalizedValue;\n normalizedValue = normalizedValue.toLowerCase?.() ?? normalizedValue;\n\n return normalizedValue;\n}\n\n/**\n * Generate token balance changes from parsed events.\n *\n * @param request - The transaction that was simulated.\n * @param events - The parsed events.\n * @returns An array of token balance changes.\n */\nasync function getTokenBalanceChanges(\n request: GetBalanceChangesRequest,\n events: ParsedEvent[],\n): Promise<SimulationTokenBalanceChange[]> {\n const { txParams } = request;\n const from = txParams.from as Hex;\n const balanceTxs = getTokenBalanceTransactions(request, events);\n\n log('Generated balance transactions', [...balanceTxs.after.values()]);\n\n const transactionCount = balanceTxs.before.size + balanceTxs.after.size + 1;\n\n if (transactionCount === 1) {\n return [];\n }\n\n const response = await baseRequest({\n request,\n before: [...balanceTxs.before.values()],\n after: [...balanceTxs.after.values()],\n });\n\n log('Balance simulation response', response);\n\n if (response.transactions.length !== transactionCount) {\n throw new SimulationInvalidResponseError();\n }\n\n let prevBalanceTxIndex = 0;\n return [...balanceTxs.after.keys()]\n .map((token, index) => {\n const previousBalanceCheckSkipped = !balanceTxs.before.get(token);\n const previousBalance = previousBalanceCheckSkipped\n ? '0x0'\n : getAmountFromBalanceTransactionResult(\n from,\n token,\n // eslint-disable-next-line no-plusplus\n response.transactions[prevBalanceTxIndex++],\n );\n\n const newBalance = getAmountFromBalanceTransactionResult(\n from,\n token,\n response.transactions[index + balanceTxs.before.size + 1],\n );\n\n const balanceChange = getSimulationBalanceChange(\n previousBalance,\n newBalance,\n );\n\n if (!balanceChange) {\n return undefined;\n }\n\n return {\n ...token,\n ...balanceChange,\n };\n })\n .filter((change) => change !== undefined) as SimulationTokenBalanceChange[];\n}\n\n/**\n * Generate transactions to check token balances.\n *\n * @param request - The transaction that was simulated.\n * @param events - The parsed events.\n * @returns A map of token balance transactions keyed by token.\n */\nfunction getTokenBalanceTransactions(\n request: GetBalanceChangesRequest,\n events: ParsedEvent[],\n): {\n before: BalanceTransactionMap;\n after: BalanceTransactionMap;\n} {\n const tokenKeys = new Set();\n const before = new Map();\n const after = new Map();\n const from = request.txParams.from as Hex;\n\n const userEvents = events.filter((event) =>\n [event.args.from, event.args.to].includes(from),\n );\n\n log('Filtered user events', userEvents);\n\n for (const event of userEvents) {\n const tokenIds = getEventTokenIds(event);\n\n log('Extracted token IDs', tokenIds);\n\n for (const tokenId of tokenIds) {\n const simulationToken: SimulationToken = {\n address: event.contractAddress,\n standard: event.tokenStandard,\n id: tokenId,\n };\n\n const tokenKey = JSON.stringify(simulationToken);\n\n if (tokenKeys.has(tokenKey)) {\n log(\n 'Ignoring additional event with same contract and token ID',\n simulationToken,\n );\n continue;\n }\n\n tokenKeys.add(tokenKey);\n\n const data = getBalanceTransactionData(\n event.tokenStandard,\n from,\n tokenId,\n );\n\n const transaction: SimulationRequestTransaction = {\n from,\n to: event.contractAddress,\n data,\n };\n\n if (skipPriorBalanceCheck(event)) {\n after.set(simulationToken, transaction);\n } else {\n before.set(simulationToken, transaction);\n after.set(simulationToken, transaction);\n }\n }\n }\n\n return { before, after };\n}\n\n/**\n * Check if an event needs to check the previous balance.\n *\n * @param event - The parsed event.\n * @returns True if the prior balance check should be skipped.\n */\nfunction skipPriorBalanceCheck(event: ParsedEvent): boolean {\n // In the case of an NFT mint, we cannot check the NFT owner before the mint\n // as the balance check transaction would revert.\n return (\n event.name === 'Transfer' &&\n event.tokenStandard === SimulationTokenStandard.erc721 &&\n parseInt(event.args.from as string, 16) === 0\n );\n}\n\n/**\n * Extract token IDs from a parsed event.\n *\n * @param event - The parsed event.\n * @returns An array of token IDs.\n */\nfunction getEventTokenIds(event: ParsedEvent): (Hex | undefined)[] {\n if (event.tokenStandard === SimulationTokenStandard.erc721) {\n return [event.args.tokenId as Hex];\n }\n\n if (\n event.tokenStandard === SimulationTokenStandard.erc1155 &&\n event.name === 'TransferSingle'\n ) {\n return [event.args.id as Hex];\n }\n\n if (\n event.tokenStandard === SimulationTokenStandard.erc1155 &&\n event.name === 'TransferBatch'\n ) {\n return event.args.ids as Hex[];\n }\n\n // ERC-20 does not have a token ID so default to undefined.\n return [undefined];\n}\n\n/**\n * Get the interface for a token standard.\n *\n * @param tokenStandard - The token standard.\n * @returns The interface for the token standard.\n */\nfunction getContractInterface(\n tokenStandard: SimulationTokenStandard,\n): Interface {\n switch (tokenStandard) {\n case SimulationTokenStandard.erc721:\n return new Interface(abiERC721);\n case SimulationTokenStandard.erc1155:\n return new Interface(abiERC1155);\n default:\n return new Interface(abiERC20);\n }\n}\n\n/**\n * Extract the value from a balance transaction response using the correct ABI.\n *\n * @param from - The address to check the balance of.\n * @param token - The token to check the balance of.\n * @param response - The balance transaction response.\n * @returns The value of the balance transaction as Hex.\n */\nfunction getAmountFromBalanceTransactionResult(\n from: Hex,\n token: SimulationToken,\n response: SimulationResponseTransaction,\n): Hex {\n const contract = getContractInterface(token.standard);\n\n try {\n if (token.standard === SimulationTokenStandard.erc721) {\n const result = contract.decodeFunctionResult('ownerOf', response.return);\n const owner = result[0];\n return owner.toLowerCase() === from.toLowerCase() ? '0x1' : '0x0';\n }\n\n const result = contract.decodeFunctionResult('balanceOf', response.return);\n return toHex(result[0]);\n } catch (error) {\n log('Failed to decode balance transaction', error, { token, response });\n throw new SimulationError(\n `Failed to decode balance transaction for token ${\n token.address\n }: ${String(error)}`,\n );\n }\n}\n\n/**\n * Generate the balance transaction data for a token.\n *\n * @param tokenStandard - The token standard.\n * @param from - The address to check the balance of.\n * @param tokenId - The token ID to check the balance of.\n * @returns The balance transaction data.\n */\nfunction getBalanceTransactionData(\n tokenStandard: SimulationTokenStandard,\n from: Hex,\n tokenId?: Hex,\n): Hex {\n const contract = getContractInterface(tokenStandard);\n switch (tokenStandard) {\n case SimulationTokenStandard.erc721:\n return contract.encodeFunctionData('ownerOf', [tokenId]) as Hex;\n\n case SimulationTokenStandard.erc1155:\n return contract.encodeFunctionData('balanceOf', [from, tokenId]) as Hex;\n\n default:\n return contract.encodeFunctionData('balanceOf', [from]) as Hex;\n }\n}\n\n/**\n * Parse a raw event log using known ABIs.\n *\n * @param eventLog - The raw event log.\n * @param interfaces - The contract interfaces.\n * @returns The parsed event log or undefined if it could not be parsed.\n */\nfunction parseLog(\n eventLog: SimulationResponseLog,\n interfaces: Map<SupportedToken, Interface>,\n):\n | (LogDescription & { abi: ABI; standard: SimulationTokenStandard })\n | undefined {\n const supportedTokens = Object.values(SupportedToken);\n\n for (const token of supportedTokens) {\n try {\n const contractInterface = interfaces.get(token) as Interface;\n const { abi, standard } = SUPPORTED_TOKEN_ABIS[token];\n\n return {\n ...contractInterface.parseLog(eventLog),\n abi,\n standard,\n };\n // Not used\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n } catch (e) {\n continue;\n }\n }\n\n return undefined;\n}\n\n/**\n * Extract all logs from a call trace tree.\n *\n * @param call - The root call trace.\n * @returns An array of logs.\n */\nfunction extractLogs(\n call: SimulationResponseCallTrace,\n): SimulationResponseLog[] {\n /* istanbul ignore next */\n const logs = call.logs ?? [];\n\n /* istanbul ignore next */\n const nestedCalls = call.calls ?? [];\n\n return [\n ...logs,\n ...nestedCalls.map((nestedCall) => extractLogs(nestedCall)).flat(),\n ];\n}\n\n/**\n * Generate balance change data from previous and new balances.\n *\n * @param previousBalance - The previous balance.\n * @param newBalance - The new balance.\n * @param offset - Optional offset to apply to the new balance.\n * @returns The balance change data or undefined if unchanged.\n */\nfunction getSimulationBalanceChange(\n previousBalance: Hex,\n newBalance: Hex,\n offset: number = 0,\n): SimulationBalanceChange | undefined {\n const newBalanceBN = hexToBN(newBalance).add(new BN(offset));\n const previousBalanceBN = hexToBN(previousBalance);\n const differenceBN = newBalanceBN.sub(previousBalanceBN);\n const isDecrease = differenceBN.isNeg();\n const difference = toHex(differenceBN.abs());\n\n if (differenceBN.isZero()) {\n log('Balance change is zero');\n return undefined;\n }\n\n return {\n previousBalance,\n newBalance: toHex(newBalanceBN),\n difference,\n isDecrease,\n };\n}\n\n/**\n * Get the contract interfaces for all supported tokens.\n *\n * @returns A map of supported tokens to their contract interfaces.\n */\nfunction getContractInterfaces(): Map<SupportedToken, Interface> {\n const supportedTokens = Object.values(SupportedToken);\n\n return new Map(\n supportedTokens.map((tokenType) => {\n const { abi } = SUPPORTED_TOKEN_ABIS[tokenType];\n const contractInterface = new Interface(abi);\n return [tokenType, contractInterface];\n }),\n );\n}\n\n/**\n * Base request to simulation API.\n *\n * @param options - Options bag.\n * @param options.after - Transactions to simulate after user's transaction.\n * @param options.before - Transactions to simulate before user's transaction.\n * @param options.params - Additional parameters for the request.\n * @param options.request - Original request object.\n * @returns The simulation response.\n */\nasync function baseRequest({\n request,\n params,\n before = [],\n after = [],\n}: {\n request: GetBalanceChangesRequest;\n params?: Partial<SimulationRequest>;\n before?: SimulationRequestTransaction[];\n after?: SimulationRequestTransaction[];\n}): Promise<SimulationResponse> {\n const { blockTime, chainId, ethQuery, txParams } = request;\n const { authorizationList } = txParams;\n const from = txParams.from as Hex;\n\n const authorizationListFinal = authorizationList?.map((authorization) => ({\n address: authorization.address,\n from,\n }));\n\n const userTransaction: SimulationRequestTransaction = {\n authorizationList: authorizationListFinal,\n data: txParams.data as Hex,\n from: txParams.from as Hex,\n gas: txParams.gas as Hex,\n maxFeePerGas: (txParams.maxFeePerGas ?? txParams.gasPrice) as Hex,\n maxPriorityFeePerGas: (txParams.maxPriorityFeePerGas ??\n txParams.gasPrice) as Hex,\n to: txParams.to as Hex,\n value: txParams.value as Hex,\n };\n\n const transactions = [...before, userTransaction, ...after];\n const requiredBalanceBN = getRequiredBalance(request);\n const requiredBalanceHex = toHex(requiredBalanceBN);\n\n log('Required balance', requiredBalanceHex);\n\n const currentBalanceHex = (await query(ethQuery, 'getBalance', [\n from,\n 'latest',\n ])) as Hex;\n\n const currentBalanceBN = hexToBN(currentBalanceHex);\n\n log('Current balance', currentBalanceHex);\n\n const isInsufficientBalance = currentBalanceBN.lt(requiredBalanceBN);\n\n return await simulateTransactions(chainId, {\n ...params,\n transactions,\n withGas: true,\n withDefaultBlockOverrides: true,\n ...(blockTime && {\n blockOverrides: {\n ...params?.blockOverrides,\n time: toHex(blockTime),\n },\n }),\n ...(isInsufficientBalance && {\n overrides: {\n ...params?.overrides,\n [from]: {\n ...params?.overrides?.[from],\n balance: requiredBalanceHex,\n },\n },\n }),\n });\n}\n\n/**\n * Calculate the required minimum balance for a transaction.\n *\n * @param request - The transaction request.\n * @returns The minimal balance as a BN.\n */\nfunction getRequiredBalance(request: GetBalanceChangesRequest): BN {\n const { txParams } = request;\n const gasLimit = hexToBN(txParams.gas ?? '0x0');\n const gasPrice = hexToBN(txParams.maxFeePerGas ?? txParams.gasPrice ?? '0x0');\n const value = hexToBN(txParams.value ?? '0x0');\n\n const nestedValue = (request.nestedTransactions ?? [])\n .map((tx) => hexToBN(tx.value ?? '0x0'))\n .reduce((acc, val) => acc.add(val), new BN(0));\n\n return gasLimit.mul(gasPrice).add(value).add(nestedValue);\n}\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type EthQuery from "@metamask/eth-query";
|
|
2
|
+
import { type Hex } from "@metamask/utils";
|
|
3
|
+
import type { SimulationData, TransactionParams, NestedTransactionMetadata } from "../types.cjs";
|
|
4
|
+
export declare enum SupportedToken {
|
|
5
|
+
ERC20 = "erc20",
|
|
6
|
+
ERC721 = "erc721",
|
|
7
|
+
ERC1155 = "erc1155",
|
|
8
|
+
ERC20_WRAPPED = "erc20Wrapped",
|
|
9
|
+
ERC721_LEGACY = "erc721Legacy"
|
|
10
|
+
}
|
|
11
|
+
export type GetBalanceChangesRequest = {
|
|
12
|
+
blockTime?: number;
|
|
13
|
+
chainId: Hex;
|
|
14
|
+
ethQuery: EthQuery;
|
|
15
|
+
nestedTransactions?: NestedTransactionMetadata[];
|
|
16
|
+
txParams: TransactionParams;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Generate simulation data for a transaction.
|
|
20
|
+
*
|
|
21
|
+
* @param request - The transaction to simulate.
|
|
22
|
+
* @param request.chainId - The chain ID of the transaction.
|
|
23
|
+
* @param request.from - The sender of the transaction.
|
|
24
|
+
* @param request.to - The recipient of the transaction.
|
|
25
|
+
* @param request.value - The value of the transaction.
|
|
26
|
+
* @param request.data - The data of the transaction.
|
|
27
|
+
* @returns The simulation data.
|
|
28
|
+
*/
|
|
29
|
+
export declare function getBalanceChanges(request: GetBalanceChangesRequest): Promise<SimulationData>;
|
|
30
|
+
//# sourceMappingURL=balance-changes.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance-changes.d.cts","sourceRoot":"","sources":["../../src/utils/balance-changes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,QAAQ,4BAA4B;AAEhD,OAAO,EAAsB,KAAK,GAAG,EAAE,wBAAwB;AAsB/D,OAAO,KAAK,EAEV,cAAc,EAGd,iBAAiB,EACjB,yBAAyB,EAC1B,qBAAiB;AAGlB,oBAAY,cAAc;IACxB,KAAK,UAAU;IACf,MAAM,WAAW;IACjB,OAAO,YAAY;IACnB,aAAa,iBAAiB;IAC9B,aAAa,iBAAiB;CAC/B;AAID,MAAM,MAAM,wBAAwB,GAAG;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,kBAAkB,CAAC,EAAE,yBAAyB,EAAE,CAAC;IACjD,QAAQ,EAAE,iBAAiB,CAAC;CAC7B,CAAC;AA+CF;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,cAAc,CAAC,CAsDzB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type EthQuery from "@metamask/eth-query";
|
|
2
|
+
import { type Hex } from "@metamask/utils";
|
|
3
|
+
import type { SimulationData, TransactionParams, NestedTransactionMetadata } from "../types.mjs";
|
|
4
|
+
export declare enum SupportedToken {
|
|
5
|
+
ERC20 = "erc20",
|
|
6
|
+
ERC721 = "erc721",
|
|
7
|
+
ERC1155 = "erc1155",
|
|
8
|
+
ERC20_WRAPPED = "erc20Wrapped",
|
|
9
|
+
ERC721_LEGACY = "erc721Legacy"
|
|
10
|
+
}
|
|
11
|
+
export type GetBalanceChangesRequest = {
|
|
12
|
+
blockTime?: number;
|
|
13
|
+
chainId: Hex;
|
|
14
|
+
ethQuery: EthQuery;
|
|
15
|
+
nestedTransactions?: NestedTransactionMetadata[];
|
|
16
|
+
txParams: TransactionParams;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Generate simulation data for a transaction.
|
|
20
|
+
*
|
|
21
|
+
* @param request - The transaction to simulate.
|
|
22
|
+
* @param request.chainId - The chain ID of the transaction.
|
|
23
|
+
* @param request.from - The sender of the transaction.
|
|
24
|
+
* @param request.to - The recipient of the transaction.
|
|
25
|
+
* @param request.value - The value of the transaction.
|
|
26
|
+
* @param request.data - The data of the transaction.
|
|
27
|
+
* @returns The simulation data.
|
|
28
|
+
*/
|
|
29
|
+
export declare function getBalanceChanges(request: GetBalanceChangesRequest): Promise<SimulationData>;
|
|
30
|
+
//# sourceMappingURL=balance-changes.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balance-changes.d.mts","sourceRoot":"","sources":["../../src/utils/balance-changes.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,QAAQ,4BAA4B;AAEhD,OAAO,EAAsB,KAAK,GAAG,EAAE,wBAAwB;AAsB/D,OAAO,KAAK,EAEV,cAAc,EAGd,iBAAiB,EACjB,yBAAyB,EAC1B,qBAAiB;AAGlB,oBAAY,cAAc;IACxB,KAAK,UAAU;IACf,MAAM,WAAW;IACjB,OAAO,YAAY;IACnB,aAAa,iBAAiB;IAC9B,aAAa,iBAAiB;CAC/B;AAID,MAAM,MAAM,wBAAwB,GAAG;IACrC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,GAAG,CAAC;IACb,QAAQ,EAAE,QAAQ,CAAC;IACnB,kBAAkB,CAAC,EAAE,yBAAyB,EAAE,CAAC;IACjD,QAAQ,EAAE,iBAAiB,CAAC;CAC7B,CAAC;AA+CF;;;;;;;;;;GAUG;AACH,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,cAAc,CAAC,CAsDzB"}
|