@@ -62,12 +62,12 @@ import type {
62
62
TransactionParams ,
63
63
TransactionHistoryEntry ,
64
64
TransactionError ,
65
- SimulationData ,
66
65
GasFeeFlow ,
67
66
GasFeeFlowResponse ,
68
67
SubmitHistoryEntry ,
69
68
InternalAccount ,
70
69
PublishHook ,
70
+ GasFeeToken ,
71
71
} from './types' ;
72
72
import {
73
73
GasFeeEstimateType ,
@@ -86,6 +86,7 @@ import {
86
86
getTransactionLayer1GasFee ,
87
87
updateTransactionLayer1GasFee ,
88
88
} from './utils/layer1-gas-fee-flow' ;
89
+ import type { GetSimulationDataResult } from './utils/simulation' ;
89
90
import { getSimulationData } from './utils/simulation' ;
90
91
import {
91
92
updatePostTransactionBalance ,
@@ -448,24 +449,40 @@ const TRANSACTION_META_2_MOCK = {
448
449
} ,
449
450
} as TransactionMeta ;
450
451
451
- const SIMULATION_DATA_MOCK : SimulationData = {
452
- nativeBalanceChange : {
453
- previousBalance : '0x0' ,
454
- newBalance : '0x1' ,
455
- difference : '0x1' ,
456
- isDecrease : false ,
457
- } ,
458
- tokenBalanceChanges : [
459
- {
460
- address : '0x123' ,
461
- standard : SimulationTokenStandard . erc721 ,
462
- id : '0x456' ,
463
- previousBalance : '0x1' ,
464
- newBalance : '0x3' ,
465
- difference : '0x2' ,
452
+ const SIMULATION_DATA_RESULT_MOCK : GetSimulationDataResult = {
453
+ gasFeeTokens : [ ] ,
454
+ simulationData : {
455
+ nativeBalanceChange : {
456
+ previousBalance : '0x0' ,
457
+ newBalance : '0x1' ,
458
+ difference : '0x1' ,
466
459
isDecrease : false ,
467
460
} ,
468
- ] ,
461
+ tokenBalanceChanges : [
462
+ {
463
+ address : '0x123' ,
464
+ standard : SimulationTokenStandard . erc721 ,
465
+ id : '0x456' ,
466
+ previousBalance : '0x1' ,
467
+ newBalance : '0x3' ,
468
+ difference : '0x2' ,
469
+ isDecrease : false ,
470
+ } ,
471
+ ] ,
472
+ } ,
473
+ } ;
474
+
475
+ const GAS_FEE_TOKEN_MOCK : GasFeeToken = {
476
+ amount : '0x1' ,
477
+ balance : '0x2' ,
478
+ decimals : 18 ,
479
+ gas : '0x3' ,
480
+ maxFeePerGas : '0x4' ,
481
+ maxPriorityFeePerGas : '0x5' ,
482
+ rateWei : '0x6' ,
483
+ recipient : '0x7' ,
484
+ symbol : 'ETH' ,
485
+ tokenAddress : '0x8' ,
469
486
} ;
470
487
471
488
const GAS_FEE_ESTIMATES_MOCK : GasFeeFlowResponse = {
@@ -1990,7 +2007,9 @@ describe('TransactionController', () => {
1990
2007
1991
2008
describe ( 'updates simulation data' , ( ) => {
1992
2009
it ( 'by default' , async ( ) => {
1993
- getSimulationDataMock . mockResolvedValueOnce ( SIMULATION_DATA_MOCK ) ;
2010
+ getSimulationDataMock . mockResolvedValueOnce (
2011
+ SIMULATION_DATA_RESULT_MOCK ,
2012
+ ) ;
1994
2013
1995
2014
const { controller } = setupController ( ) ;
1996
2015
@@ -2021,12 +2040,14 @@ describe('TransactionController', () => {
2021
2040
) ;
2022
2041
2023
2042
expect ( controller . state . transactions [ 0 ] . simulationData ) . toStrictEqual (
2024
- SIMULATION_DATA_MOCK ,
2043
+ SIMULATION_DATA_RESULT_MOCK . simulationData ,
2025
2044
) ;
2026
2045
} ) ;
2027
2046
2028
2047
it ( 'with error if simulation disabled' , async ( ) => {
2029
- getSimulationDataMock . mockResolvedValueOnce ( SIMULATION_DATA_MOCK ) ;
2048
+ getSimulationDataMock . mockResolvedValueOnce (
2049
+ SIMULATION_DATA_RESULT_MOCK ,
2050
+ ) ;
2030
2051
2031
2052
const { controller } = setupController ( {
2032
2053
options : { isSimulationEnabled : ( ) => false } ,
@@ -2053,7 +2074,9 @@ describe('TransactionController', () => {
2053
2074
} ) ;
2054
2075
2055
2076
it ( 'unless approval not required' , async ( ) => {
2056
- getSimulationDataMock . mockResolvedValueOnce ( SIMULATION_DATA_MOCK ) ;
2077
+ getSimulationDataMock . mockResolvedValueOnce (
2078
+ SIMULATION_DATA_RESULT_MOCK ,
2079
+ ) ;
2057
2080
2058
2081
const { controller } = setupController ( ) ;
2059
2082
@@ -2070,6 +2093,57 @@ describe('TransactionController', () => {
2070
2093
} ) ;
2071
2094
} ) ;
2072
2095
2096
+ describe ( 'updates gas fee tokens' , ( ) => {
2097
+ it ( 'by default' , async ( ) => {
2098
+ getSimulationDataMock . mockResolvedValueOnce ( {
2099
+ gasFeeTokens : [ GAS_FEE_TOKEN_MOCK ] ,
2100
+ simulationData : {
2101
+ tokenBalanceChanges : [ ] ,
2102
+ } ,
2103
+ } ) ;
2104
+
2105
+ const { controller } = setupController ( ) ;
2106
+
2107
+ await controller . addTransaction (
2108
+ {
2109
+ from : ACCOUNT_MOCK ,
2110
+ to : ACCOUNT_MOCK ,
2111
+ } ,
2112
+ {
2113
+ networkClientId : NETWORK_CLIENT_ID_MOCK ,
2114
+ } ,
2115
+ ) ;
2116
+
2117
+ await flushPromises ( ) ;
2118
+
2119
+ expect ( controller . state . transactions [ 0 ] . gasFeeTokens ) . toStrictEqual ( [
2120
+ GAS_FEE_TOKEN_MOCK ,
2121
+ ] ) ;
2122
+ } ) ;
2123
+
2124
+ it ( 'unless approval not required' , async ( ) => {
2125
+ getSimulationDataMock . mockResolvedValueOnce ( {
2126
+ gasFeeTokens : [ GAS_FEE_TOKEN_MOCK ] ,
2127
+ simulationData : {
2128
+ tokenBalanceChanges : [ ] ,
2129
+ } ,
2130
+ } ) ;
2131
+
2132
+ const { controller } = setupController ( ) ;
2133
+
2134
+ await controller . addTransaction (
2135
+ {
2136
+ from : ACCOUNT_MOCK ,
2137
+ to : ACCOUNT_MOCK ,
2138
+ } ,
2139
+ { requireApproval : false , networkClientId : NETWORK_CLIENT_ID_MOCK } ,
2140
+ ) ;
2141
+
2142
+ expect ( getSimulationDataMock ) . toHaveBeenCalledTimes ( 0 ) ;
2143
+ expect ( controller . state . transactions [ 0 ] . gasFeeTokens ) . toBeUndefined ( ) ;
2144
+ } ) ;
2145
+ } ) ;
2146
+
2073
2147
describe ( 'on approve' , ( ) => {
2074
2148
it ( 'submits transaction' , async ( ) => {
2075
2149
const { controller, messenger } = setupController ( {
@@ -6575,4 +6649,59 @@ describe('TransactionController', () => {
6575
6649
) ;
6576
6650
} ) ;
6577
6651
} ) ;
6652
+
6653
+ describe ( 'updateSelectedGasFeeToken' , ( ) => {
6654
+ it ( 'updates selected gas fee token in state' , ( ) => {
6655
+ const { controller } = setupController ( {
6656
+ options : {
6657
+ state : {
6658
+ transactions : [
6659
+ {
6660
+ ...TRANSACTION_META_MOCK ,
6661
+ gasFeeTokens : [ GAS_FEE_TOKEN_MOCK ] ,
6662
+ } ,
6663
+ ] ,
6664
+ } ,
6665
+ } ,
6666
+ } ) ;
6667
+
6668
+ controller . updateSelectedGasFeeToken (
6669
+ TRANSACTION_META_MOCK . id ,
6670
+ GAS_FEE_TOKEN_MOCK . tokenAddress ,
6671
+ ) ;
6672
+
6673
+ expect ( controller . state . transactions [ 0 ] . selectedGasFeeToken ) . toBe (
6674
+ GAS_FEE_TOKEN_MOCK . tokenAddress ,
6675
+ ) ;
6676
+ } ) ;
6677
+
6678
+ it ( 'throws if transaction does not exist' , ( ) => {
6679
+ const { controller } = setupController ( ) ;
6680
+
6681
+ expect ( ( ) =>
6682
+ controller . updateSelectedGasFeeToken (
6683
+ TRANSACTION_META_MOCK . id ,
6684
+ GAS_FEE_TOKEN_MOCK . tokenAddress ,
6685
+ ) ,
6686
+ ) . toThrow (
6687
+ `Cannot update transaction as ID not found - ${ TRANSACTION_META_MOCK . id } ` ,
6688
+ ) ;
6689
+ } ) ;
6690
+
6691
+ it ( 'throws if no matching gas fee token' , ( ) => {
6692
+ const { controller } = setupController ( {
6693
+ options : {
6694
+ state : {
6695
+ transactions : [
6696
+ { ...TRANSACTION_META_MOCK , gasFeeTokens : [ GAS_FEE_TOKEN_MOCK ] } ,
6697
+ ] ,
6698
+ } ,
6699
+ } ,
6700
+ } ) ;
6701
+
6702
+ expect ( ( ) =>
6703
+ controller . updateSelectedGasFeeToken ( TRANSACTION_META_MOCK . id , '0x123' ) ,
6704
+ ) . toThrow ( 'No matching gas fee token found' ) ;
6705
+ } ) ;
6706
+ } ) ;
6578
6707
} ) ;
0 commit comments