Skip to content

Commit 6c3d296

Browse files
committed
Split gas fee token request
1 parent b53f7d7 commit 6c3d296

File tree

6 files changed

+405
-397
lines changed

6 files changed

+405
-397
lines changed

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

+63-83
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ import type {
6868
InternalAccount,
6969
PublishHook,
7070
GasFeeToken,
71+
SimulationData,
7172
} from './types';
7273
import {
7374
GasFeeEstimateType,
@@ -78,6 +79,7 @@ import {
7879
TransactionType,
7980
WalletDevice,
8081
} from './types';
82+
import { getBalanceChanges } from './utils/balance-changes';
8183
import { addTransactionBatch } from './utils/batch';
8284
import { DELEGATION_PREFIX, getDelegationAddress } from './utils/eip7702';
8385
import { addGasBuffer, estimateGas, updateGas } from './utils/gas';
@@ -87,8 +89,6 @@ import {
8789
getTransactionLayer1GasFee,
8890
updateTransactionLayer1GasFee,
8991
} from './utils/layer1-gas-fee-flow';
90-
import type { GetSimulationDataResult } from './utils/simulation';
91-
import { getSimulationData } from './utils/simulation';
9292
import {
9393
updatePostTransactionBalance,
9494
updateSwapsTransaction,
@@ -100,6 +100,7 @@ import {
100100
buildCustomNetworkClientConfiguration,
101101
buildMockGetNetworkClientById,
102102
} from '../../network-controller/tests/helpers';
103+
import { getGasFeeTokens } from './utils/gas-fee-tokens';
103104

104105
type UnrestrictedMessenger = Messenger<
105106
TransactionControllerActions | AllowedActions,
@@ -128,8 +129,9 @@ jest.mock('./utils/batch');
128129
jest.mock('./utils/gas');
129130
jest.mock('./utils/gas-fees');
130131
jest.mock('./utils/gas-flow');
132+
jest.mock('./utils/gas-fee-tokens');
131133
jest.mock('./utils/layer1-gas-fee-flow');
132-
jest.mock('./utils/simulation');
134+
jest.mock('./utils/balance-changes');
133135
jest.mock('./utils/swaps');
134136
jest.mock('uuid');
135137

@@ -453,27 +455,24 @@ const TRANSACTION_META_2_MOCK = {
453455
},
454456
} as TransactionMeta;
455457

456-
const SIMULATION_DATA_RESULT_MOCK: GetSimulationDataResult = {
457-
gasFeeTokens: [],
458-
simulationData: {
459-
nativeBalanceChange: {
460-
previousBalance: '0x0',
461-
newBalance: '0x1',
462-
difference: '0x1',
458+
const SIMULATION_DATA_RESULT_MOCK: SimulationData = {
459+
nativeBalanceChange: {
460+
previousBalance: '0x0',
461+
newBalance: '0x1',
462+
difference: '0x1',
463+
isDecrease: false,
464+
},
465+
tokenBalanceChanges: [
466+
{
467+
address: '0x123',
468+
standard: SimulationTokenStandard.erc721,
469+
id: '0x456',
470+
previousBalance: '0x1',
471+
newBalance: '0x3',
472+
difference: '0x2',
463473
isDecrease: false,
464474
},
465-
tokenBalanceChanges: [
466-
{
467-
address: '0x123',
468-
standard: SimulationTokenStandard.erc721,
469-
id: '0x456',
470-
previousBalance: '0x1',
471-
newBalance: '0x3',
472-
difference: '0x2',
473-
isDecrease: false,
474-
},
475-
],
476-
},
475+
],
477476
};
478477

479478
const GAS_FEE_TOKEN_MOCK: GasFeeToken = {
@@ -523,7 +522,8 @@ describe('TransactionController', () => {
523522
);
524523
const testGasFeeFlowClassMock = jest.mocked(TestGasFeeFlow);
525524
const gasFeePollerClassMock = jest.mocked(GasFeePoller);
526-
const getSimulationDataMock = jest.mocked(getSimulationData);
525+
const getBalanceChangesMock = jest.mocked(getBalanceChanges);
526+
const getGasFeeTokensMock = jest.mocked(getGasFeeTokens);
527527
const getTransactionLayer1GasFeeMock = jest.mocked(
528528
getTransactionLayer1GasFee,
529529
);
@@ -2168,7 +2168,7 @@ describe('TransactionController', () => {
21682168

21692169
describe('updates simulation data', () => {
21702170
it('by default', async () => {
2171-
getSimulationDataMock.mockResolvedValueOnce(
2171+
getBalanceChangesMock.mockResolvedValueOnce(
21722172
SIMULATION_DATA_RESULT_MOCK,
21732173
);
21742174

@@ -2186,27 +2186,23 @@ describe('TransactionController', () => {
21862186

21872187
await flushPromises();
21882188

2189-
expect(getSimulationDataMock).toHaveBeenCalledTimes(1);
2190-
expect(getSimulationDataMock).toHaveBeenCalledWith(
2191-
{
2192-
chainId: MOCK_NETWORK.chainId,
2193-
data: undefined,
2194-
from: ACCOUNT_MOCK,
2195-
to: ACCOUNT_MOCK,
2196-
value: '0x0',
2197-
},
2198-
{
2199-
blockTime: undefined,
2200-
},
2201-
);
2189+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
2190+
expect(getBalanceChangesMock).toHaveBeenCalledWith({
2191+
blockTime: undefined,
2192+
chainId: MOCK_NETWORK.chainId,
2193+
data: undefined,
2194+
from: ACCOUNT_MOCK,
2195+
to: ACCOUNT_MOCK,
2196+
value: '0x0',
2197+
});
22022198

22032199
expect(controller.state.transactions[0].simulationData).toStrictEqual(
2204-
SIMULATION_DATA_RESULT_MOCK.simulationData,
2200+
SIMULATION_DATA_RESULT_MOCK,
22052201
);
22062202
});
22072203

22082204
it('with error if simulation disabled', async () => {
2209-
getSimulationDataMock.mockResolvedValueOnce(
2205+
getBalanceChangesMock.mockResolvedValueOnce(
22102206
SIMULATION_DATA_RESULT_MOCK,
22112207
);
22122208

@@ -2224,7 +2220,7 @@ describe('TransactionController', () => {
22242220
},
22252221
);
22262222

2227-
expect(getSimulationDataMock).toHaveBeenCalledTimes(0);
2223+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(0);
22282224
expect(controller.state.transactions[0].simulationData).toStrictEqual({
22292225
error: {
22302226
code: SimulationErrorCode.Disabled,
@@ -2235,7 +2231,7 @@ describe('TransactionController', () => {
22352231
});
22362232

22372233
it('unless approval not required', async () => {
2238-
getSimulationDataMock.mockResolvedValueOnce(
2234+
getBalanceChangesMock.mockResolvedValueOnce(
22392235
SIMULATION_DATA_RESULT_MOCK,
22402236
);
22412237

@@ -2249,12 +2245,12 @@ describe('TransactionController', () => {
22492245
{ requireApproval: false, networkClientId: NETWORK_CLIENT_ID_MOCK },
22502246
);
22512247

2252-
expect(getSimulationDataMock).toHaveBeenCalledTimes(0);
2248+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(0);
22532249
expect(controller.state.transactions[0].simulationData).toBeUndefined();
22542250
});
22552251

22562252
it('with sender code if type 4', async () => {
2257-
getSimulationDataMock.mockResolvedValueOnce(
2253+
getBalanceChangesMock.mockResolvedValueOnce(
22582254
SIMULATION_DATA_RESULT_MOCK,
22592255
);
22602256

@@ -2277,20 +2273,17 @@ describe('TransactionController', () => {
22772273

22782274
await flushPromises();
22792275

2280-
expect(getSimulationDataMock).toHaveBeenCalledWith(expect.any(Object), {
2281-
senderCode: DELEGATION_PREFIX + ACCOUNT_2_MOCK.slice(2),
2282-
});
2276+
expect(getBalanceChangesMock).toHaveBeenCalledWith(
2277+
expect.objectContaining({
2278+
senderCode: DELEGATION_PREFIX + ACCOUNT_2_MOCK.slice(2),
2279+
}),
2280+
);
22832281
});
22842282
});
22852283

22862284
describe('updates gas fee tokens', () => {
22872285
it('by default', async () => {
2288-
getSimulationDataMock.mockResolvedValueOnce({
2289-
gasFeeTokens: [GAS_FEE_TOKEN_MOCK],
2290-
simulationData: {
2291-
tokenBalanceChanges: [],
2292-
},
2293-
});
2286+
getGasFeeTokensMock.mockResolvedValueOnce([GAS_FEE_TOKEN_MOCK]);
22942287

22952288
const { controller } = setupController();
22962289

@@ -2312,12 +2305,7 @@ describe('TransactionController', () => {
23122305
});
23132306

23142307
it('unless approval not required', async () => {
2315-
getSimulationDataMock.mockResolvedValueOnce({
2316-
gasFeeTokens: [GAS_FEE_TOKEN_MOCK],
2317-
simulationData: {
2318-
tokenBalanceChanges: [],
2319-
},
2320-
});
2308+
getGasFeeTokensMock.mockResolvedValueOnce([GAS_FEE_TOKEN_MOCK]);
23212309

23222310
const { controller } = setupController();
23232311

@@ -2329,7 +2317,7 @@ describe('TransactionController', () => {
23292317
{ requireApproval: false, networkClientId: NETWORK_CLIENT_ID_MOCK },
23302318
);
23312319

2332-
expect(getSimulationDataMock).toHaveBeenCalledTimes(0);
2320+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(0);
23332321
expect(controller.state.transactions[0].gasFeeTokens).toBeUndefined();
23342322
});
23352323
});
@@ -6837,7 +6825,7 @@ describe('TransactionController', () => {
68376825
updateToInitialState: true,
68386826
});
68396827

6840-
expect(getSimulationDataMock).toHaveBeenCalledTimes(0);
6828+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(0);
68416829

68426830
shouldResimulateMock.mockReturnValueOnce({
68436831
blockTime: 123,
@@ -6848,17 +6836,13 @@ describe('TransactionController', () => {
68486836

68496837
await flushPromises();
68506838

6851-
expect(getSimulationDataMock).toHaveBeenCalledTimes(1);
6852-
expect(getSimulationDataMock).toHaveBeenCalledWith(
6853-
{
6854-
from: ACCOUNT_MOCK,
6855-
to: ACCOUNT_2_MOCK,
6856-
value: TRANSACTION_META_MOCK.txParams.value,
6857-
},
6858-
{
6859-
blockTime: 123,
6860-
},
6861-
);
6839+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
6840+
expect(getBalanceChangesMock).toHaveBeenCalledWith({
6841+
blockTime: 123,
6842+
from: ACCOUNT_MOCK,
6843+
to: ACCOUNT_2_MOCK,
6844+
value: TRANSACTION_META_MOCK.txParams.value,
6845+
});
68626846
});
68636847

68646848
it('does not trigger simulation loop', async () => {
@@ -6876,7 +6860,7 @@ describe('TransactionController', () => {
68766860
updateToInitialState: true,
68776861
});
68786862

6879-
expect(getSimulationDataMock).toHaveBeenCalledTimes(0);
6863+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(0);
68806864

68816865
shouldResimulateMock.mockReturnValue({
68826866
blockTime: 123,
@@ -6887,17 +6871,13 @@ describe('TransactionController', () => {
68876871

68886872
await flushPromises();
68896873

6890-
expect(getSimulationDataMock).toHaveBeenCalledTimes(1);
6891-
expect(getSimulationDataMock).toHaveBeenCalledWith(
6892-
{
6893-
from: ACCOUNT_MOCK,
6894-
to: ACCOUNT_2_MOCK,
6895-
value: TRANSACTION_META_MOCK.txParams.value,
6896-
},
6897-
{
6898-
blockTime: 123,
6899-
},
6900-
);
6874+
expect(getBalanceChangesMock).toHaveBeenCalledTimes(1);
6875+
expect(getBalanceChangesMock).toHaveBeenCalledWith({
6876+
blockTime: 123,
6877+
from: ACCOUNT_MOCK,
6878+
to: ACCOUNT_2_MOCK,
6879+
value: TRANSACTION_META_MOCK.txParams.value,
6880+
});
69016881
});
69026882
});
69036883

packages/transaction-controller/src/TransactionController.ts

+16-30
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ import {
118118
TransactionStatus,
119119
SimulationErrorCode,
120120
} from './types';
121+
import { getBalanceChanges } from './utils/balance-changes';
121122
import { addTransactionBatch, isAtomicBatchSupported } from './utils/batch';
122123
import {
123124
DELEGATION_PREFIX,
@@ -127,6 +128,7 @@ import {
127128
} from './utils/eip7702';
128129
import { validateConfirmedExternalTransaction } from './utils/external-transactions';
129130
import { addGasBuffer, estimateGas, updateGas } from './utils/gas';
131+
import { getGasFeeTokens } from './utils/gas-fee-tokens';
130132
import { updateGasFees } from './utils/gas-fees';
131133
import { getGasFeeFlow } from './utils/gas-flow';
132134
import {
@@ -143,7 +145,6 @@ import {
143145
} from './utils/nonce';
144146
import { prepareTransaction, serializeTransaction } from './utils/prepare';
145147
import { getTransactionParamsWithIncreasedGasFee } from './utils/retry';
146-
import { getSimulationData } from './utils/simulation';
147148
import {
148149
updatePostTransactionBalance,
149150
updateSwapsTransaction,
@@ -3986,27 +3987,20 @@ export class TransactionController extends BaseController<
39863987
authorizationAddress &&
39873988
((DELEGATION_PREFIX + remove0x(authorizationAddress)) as Hex);
39883989

3989-
const result = await this.#trace(
3990+
simulationData = await this.#trace(
39903991
{ name: 'Simulate', parentContext: traceContext },
39913992
() =>
3992-
getSimulationData(
3993-
{
3994-
chainId,
3995-
from: from as Hex,
3996-
to: to as Hex,
3997-
value: value as Hex,
3998-
data: data as Hex,
3999-
},
4000-
{
4001-
blockTime,
4002-
senderCode,
4003-
},
4004-
),
3993+
getBalanceChanges({
3994+
blockTime,
3995+
chainId,
3996+
data: data as Hex,
3997+
from: from as Hex,
3998+
senderCode,
3999+
to: to as Hex,
4000+
value: value as Hex,
4001+
}),
40054002
);
40064003

4007-
gasFeeTokens = result?.gasFeeTokens;
4008-
simulationData = result?.simulationData;
4009-
40104004
if (
40114005
blockTime &&
40124006
prevSimulationData &&
@@ -4017,19 +4011,11 @@ export class TransactionController extends BaseController<
40174011
isUpdatedAfterSecurityCheck: true,
40184012
};
40194013
}
4020-
}
40214014

4022-
const finalTransactionMeta = this.#getTransaction(transactionId);
4023-
4024-
/* istanbul ignore if */
4025-
if (!finalTransactionMeta) {
4026-
log(
4027-
'Cannot update simulation data as transaction not found',
4028-
transactionId,
4029-
simulationData,
4030-
);
4031-
4032-
return;
4015+
gasFeeTokens = await getGasFeeTokens({
4016+
chainId,
4017+
txParams,
4018+
});
40334019
}
40344020

40354021
this.#updateTransactionInternal(

0 commit comments

Comments
 (0)