Skip to content

Commit 3a53426

Browse files
committed
Simulate with gas limit and gas fee
Override balance if insufficient.
1 parent 467a955 commit 3a53426

File tree

5 files changed

+150
-79
lines changed

5 files changed

+150
-79
lines changed

packages/transaction-controller/src/TransactionController.test.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,8 @@ describe('TransactionController', () => {
21962196
expect(getBalanceChangesMock).toHaveBeenCalledWith({
21972197
blockTime: undefined,
21982198
chainId: MOCK_NETWORK.chainId,
2199+
ethQuery: expect.any(Object),
2200+
nestedTransactions: undefined,
21992201
txParams: {
22002202
data: undefined,
22012203
from: ACCOUNT_MOCK,
@@ -6817,7 +6819,10 @@ describe('TransactionController', () => {
68176819
expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
68186820
expect(getBalanceChangesMock).toHaveBeenCalledWith({
68196821
blockTime: 123,
6822+
ethQuery: expect.any(Object),
6823+
nestedTransactions: undefined,
68206824
txParams: {
6825+
data: undefined,
68216826
from: ACCOUNT_MOCK,
68226827
to: ACCOUNT_2_MOCK,
68236828
value: TRANSACTION_META_MOCK.txParams.value,
@@ -6854,7 +6859,10 @@ describe('TransactionController', () => {
68546859
expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
68556860
expect(getBalanceChangesMock).toHaveBeenCalledWith({
68566861
blockTime: 123,
6862+
ethQuery: expect.any(Object),
6863+
nestedTransactions: undefined,
68576864
txParams: {
6865+
data: undefined,
68586866
from: ACCOUNT_MOCK,
68596867
to: ACCOUNT_2_MOCK,
68606868
value: TRANSACTION_META_MOCK.txParams.value,

packages/transaction-controller/src/TransactionController.ts

+4
Original file line numberDiff line numberDiff line change
@@ -3975,6 +3975,8 @@ export class TransactionController extends BaseController<
39753975
const {
39763976
chainId,
39773977
id: transactionId,
3978+
nestedTransactions,
3979+
networkClientId,
39783980
simulationData: prevSimulationData,
39793981
txParams,
39803982
} = transactionMeta;
@@ -3996,6 +3998,8 @@ export class TransactionController extends BaseController<
39963998
getBalanceChanges({
39973999
blockTime,
39984000
chainId,
4001+
ethQuery: this.#getEthQuery({ networkClientId }),
4002+
nestedTransactions,
39994003
txParams,
40004004
}),
40014005
);

packages/transaction-controller/src/api/simulation-api.ts

+18-6
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,6 @@ export type SimulationRequestTransaction = {
4444

4545
/** Request to the simulation API to simulate transactions. */
4646
export type SimulationRequest = {
47-
/**
48-
* Transactions to be sequentially simulated.
49-
* State changes impact subsequent transactions in the list.
50-
*/
51-
transactions: SimulationRequestTransaction[];
52-
5347
blockOverrides?: {
5448
time?: Hex;
5549
};
@@ -83,12 +77,30 @@ export type SimulationRequest = {
8377
withTransfer?: boolean;
8478
};
8579

80+
/**
81+
* Transactions to be sequentially simulated.
82+
* State changes impact subsequent transactions in the list.
83+
*/
84+
transactions: SimulationRequestTransaction[];
85+
8686
/**
8787
* Whether to include call traces in the response.
8888
* Defaults to false.
8989
*/
9090
withCallTrace?: boolean;
9191

92+
/**
93+
* Whether to include the default block data in the simulation.
94+
* Defaults to false.
95+
*/
96+
withDefaultBlockOverrides?: boolean;
97+
98+
/**
99+
* Whether to use the gas fees in the simulation.
100+
* Defaults to false.
101+
*/
102+
withGas?: boolean;
103+
92104
/**
93105
* Whether to include event logs in the response.
94106
* Defaults to false.

packages/transaction-controller/src/utils/balance-changes.test.ts

+27-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import type { LogDescription } from '@ethersproject/abi';
22
import { Interface } from '@ethersproject/abi';
3+
import { query } from '@metamask/controller-utils';
4+
import type EthQuery from '@metamask/eth-query';
35
import { type Hex } from '@metamask/utils';
46

57
import type { GetBalanceChangesRequest } from './balance-changes';
@@ -20,6 +22,11 @@ import { SimulationErrorCode, SimulationTokenStandard } from '../types';
2022

2123
jest.mock('../api/simulation-api');
2224

25+
jest.mock('@metamask/controller-utils', () => ({
26+
...jest.requireActual('@metamask/controller-utils'),
27+
query: jest.fn(),
28+
}));
29+
2330
// Utility function to encode addresses and values to 32-byte ABI format
2431
const encodeTo32ByteHex = (value: string | number): Hex => {
2532
// Pad to 32 bytes (64 characters) and add '0x' prefix
@@ -53,8 +60,16 @@ const USER_ADDRESS_WITH_LEADING_ZERO =
5360

5461
const REQUEST_MOCK: GetBalanceChangesRequest = {
5562
chainId: '0x1',
63+
ethQuery: {
64+
sendAsync: jest.fn(),
65+
} as EthQuery,
5666
txParams: {
67+
data: '0x123',
5768
from: USER_ADDRESS_MOCK,
69+
gas: '0xaaa',
70+
maxFeePerGas: '0xbbb',
71+
maxPriorityFeePerGas: '0xabc',
72+
value: '0xddd',
5873
},
5974
};
6075

@@ -256,10 +271,12 @@ function mockParseLog({
256271

257272
describe('Simulation Utils', () => {
258273
const simulateTransactionsMock = jest.mocked(simulateTransactions);
274+
const queryMock = jest.mocked(query);
259275

260276
beforeEach(() => {
261277
jest.resetAllMocks();
262278
jest.spyOn(Interface.prototype, 'encodeFunctionData').mockReturnValue('');
279+
queryMock.mockResolvedValue('0xFFFFFFFFFFFF');
263280
});
264281

265282
describe('getBalanceChanges', () => {
@@ -404,10 +421,8 @@ describe('Simulation Utils', () => {
404421
);
405422

406423
const result = await getBalanceChanges({
407-
chainId: '0x1',
408-
txParams: {
409-
from,
410-
},
424+
...REQUEST_MOCK,
425+
txParams: { ...REQUEST_MOCK.txParams, from },
411426
});
412427

413428
expect(result).toStrictEqual({
@@ -637,7 +652,13 @@ describe('Simulation Utils', () => {
637652
// Minting ERC-721 token.
638653
{
639654
authorizationList: undefined,
655+
data: REQUEST_MOCK.txParams.data,
640656
from: REQUEST_MOCK.txParams.from,
657+
gas: REQUEST_MOCK.txParams.gas,
658+
maxFeePerGas: REQUEST_MOCK.txParams.maxFeePerGas,
659+
maxPriorityFeePerGas:
660+
REQUEST_MOCK.txParams.maxPriorityFeePerGas,
661+
value: REQUEST_MOCK.txParams.value,
641662
},
642663
// ERC-721 owner after minting.
643664
{
@@ -654,6 +675,8 @@ describe('Simulation Utils', () => {
654675
data: expect.any(String),
655676
},
656677
],
678+
withDefaultBlockOverrides: true,
679+
withGas: true,
657680
},
658681
);
659682
expect(result).toStrictEqual({

0 commit comments

Comments
 (0)