@@ -68,6 +68,7 @@ import type {
6868 InternalAccount ,
6969 PublishHook ,
7070 GasFeeToken ,
71+ SimulationData ,
7172} from './types' ;
7273import {
7374 GasFeeEstimateType ,
@@ -78,6 +79,7 @@ import {
7879 TransactionType ,
7980 WalletDevice ,
8081} from './types' ;
82+ import { getBalanceChanges } from './utils/balance-changes' ;
8183import { addTransactionBatch } from './utils/batch' ;
8284import { DELEGATION_PREFIX , getDelegationAddress } from './utils/eip7702' ;
8385import { 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' ;
9292import {
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
104105type UnrestrictedMessenger = Messenger <
105106 TransactionControllerActions | AllowedActions ,
@@ -128,8 +129,9 @@ jest.mock('./utils/batch');
128129jest . mock ( './utils/gas' ) ;
129130jest . mock ( './utils/gas-fees' ) ;
130131jest . mock ( './utils/gas-flow' ) ;
132+ jest . mock ( './utils/gas-fee-tokens' ) ;
131133jest . mock ( './utils/layer1-gas-fee-flow' ) ;
132- jest . mock ( './utils/simulation ' ) ;
134+ jest . mock ( './utils/balance-changes ' ) ;
133135jest . mock ( './utils/swaps' ) ;
134136jest . 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
479478const 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
0 commit comments