@@ -40,15 +40,15 @@ jest.mock('@ethersproject/providers', () => ({
40
40
41
41
const addressFrom = '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3' ;
42
42
43
- const createUnsignedTransaction = ( ) => {
43
+ const createUnsignedTransaction = ( chainId : number ) => {
44
44
return {
45
45
from : addressFrom ,
46
46
to : '0x0000000000000000000000000000000000000000' ,
47
47
value : 0 ,
48
48
data : '0x' ,
49
49
nonce : 0 ,
50
50
type : 2 ,
51
- chainId : 4 ,
51
+ chainId,
52
52
} ;
53
53
} ;
54
54
@@ -262,6 +262,34 @@ const ethereumChainIdDec = parseInt(CHAIN_IDS.ETHEREUM, 16);
262
262
const trackMetaMetricsEventSpy = jest . fn ( ) ;
263
263
const getNetworkClientByIdSpy = jest . fn ( ) ;
264
264
265
+ const defaultState = {
266
+ smartTransactionsState : {
267
+ smartTransactions : {
268
+ [ CHAIN_IDS . ETHEREUM ] : [ ] ,
269
+ } ,
270
+ userOptIn : undefined ,
271
+ fees : {
272
+ approvalTxFees : undefined ,
273
+ tradeTxFees : undefined ,
274
+ } ,
275
+ feesByChainId : {
276
+ [ CHAIN_IDS . ETHEREUM ] : {
277
+ approvalTxFees : undefined ,
278
+ tradeTxFees : undefined ,
279
+ } ,
280
+ [ CHAIN_IDS . GOERLI ] : {
281
+ approvalTxFees : undefined ,
282
+ tradeTxFees : undefined ,
283
+ } ,
284
+ } ,
285
+ liveness : true ,
286
+ livenessByChainId : {
287
+ [ CHAIN_IDS . ETHEREUM ] : true ,
288
+ [ CHAIN_IDS . GOERLI ] : true ,
289
+ } ,
290
+ } ,
291
+ } ;
292
+
265
293
describe ( 'SmartTransactionsController' , ( ) => {
266
294
let smartTransactionsController : SmartTransactionsController ;
267
295
let networkListener : ( networkState : NetworkState ) => void ;
@@ -301,33 +329,7 @@ describe('SmartTransactionsController', () => {
301
329
} ) ;
302
330
303
331
it ( 'initializes with default state' , ( ) => {
304
- expect ( smartTransactionsController . state ) . toStrictEqual ( {
305
- smartTransactionsState : {
306
- smartTransactions : {
307
- [ CHAIN_IDS . ETHEREUM ] : [ ] ,
308
- } ,
309
- userOptIn : undefined ,
310
- fees : {
311
- approvalTxFees : undefined ,
312
- tradeTxFees : undefined ,
313
- } ,
314
- feesByChainId : {
315
- [ CHAIN_IDS . ETHEREUM ] : {
316
- approvalTxFees : undefined ,
317
- tradeTxFees : undefined ,
318
- } ,
319
- [ CHAIN_IDS . GOERLI ] : {
320
- approvalTxFees : undefined ,
321
- tradeTxFees : undefined ,
322
- } ,
323
- } ,
324
- liveness : true ,
325
- livenessByChainId : {
326
- [ CHAIN_IDS . ETHEREUM ] : true ,
327
- [ CHAIN_IDS . GOERLI ] : true ,
328
- } ,
329
- } ,
330
- } ) ;
332
+ expect ( smartTransactionsController . state ) . toStrictEqual ( defaultState ) ;
331
333
} ) ;
332
334
333
335
describe ( 'onNetworkChange' , ( ) => {
@@ -471,8 +473,8 @@ describe('SmartTransactionsController', () => {
471
473
472
474
describe ( 'getFees' , ( ) => {
473
475
it ( 'gets unsigned transactions and estimates based on an unsigned transaction' , async ( ) => {
474
- const tradeTx = createUnsignedTransaction ( ) ;
475
- const approvalTx = createUnsignedTransaction ( ) ;
476
+ const tradeTx = createUnsignedTransaction ( ethereumChainIdDec ) ;
477
+ const approvalTx = createUnsignedTransaction ( ethereumChainIdDec ) ;
476
478
const getFeesApiResponse = createGetFeesApiResponse ( ) ;
477
479
nock ( API_BASE_URL )
478
480
. post ( `/networks/${ ethereumChainIdDec } /getFees` )
@@ -486,6 +488,54 @@ describe('SmartTransactionsController', () => {
486
488
tradeTxFees : getFeesApiResponse . txs [ 1 ] ,
487
489
} ) ;
488
490
} ) ;
491
+
492
+ it ( 'should add fee data to feesByChainId state using the networkClientId passed in to identify the appropriate chain' , async ( ) => {
493
+ const goerliChainIdDec = parseInt ( CHAIN_IDS . GOERLI , 16 ) ;
494
+ getNetworkClientByIdSpy . mockImplementation ( ( networkClientId ) => {
495
+ switch ( networkClientId ) {
496
+ case 'mainnet' :
497
+ return {
498
+ configuration : {
499
+ chainId : CHAIN_IDS . ETHEREUM ,
500
+ } ,
501
+ } ;
502
+ case 'goerli' :
503
+ return {
504
+ configuration : {
505
+ chainId : CHAIN_IDS . GOERLI ,
506
+ } ,
507
+ } ;
508
+ default :
509
+ throw new Error ( 'Invalid network client id' ) ;
510
+ }
511
+ } ) ;
512
+
513
+ const tradeTx = createUnsignedTransaction ( goerliChainIdDec ) ;
514
+ const approvalTx = createUnsignedTransaction ( goerliChainIdDec ) ;
515
+ const getFeesApiResponse = createGetFeesApiResponse ( ) ;
516
+ nock ( API_BASE_URL )
517
+ . post ( `/networks/${ goerliChainIdDec } /getFees` )
518
+ . reply ( 200 , getFeesApiResponse ) ;
519
+
520
+ expect (
521
+ smartTransactionsController . state . smartTransactionsState . feesByChainId ,
522
+ ) . toStrictEqual ( defaultState . smartTransactionsState . feesByChainId ) ;
523
+
524
+ await smartTransactionsController . getFees ( tradeTx , approvalTx , 'goerli' ) ;
525
+
526
+ expect (
527
+ smartTransactionsController . state . smartTransactionsState . feesByChainId ,
528
+ ) . toMatchObject ( {
529
+ [ CHAIN_IDS . ETHEREUM ] : {
530
+ approvalTxFees : undefined ,
531
+ tradeTxFees : undefined ,
532
+ } ,
533
+ [ CHAIN_IDS . GOERLI ] : {
534
+ approvalTxFees : getFeesApiResponse . txs [ 0 ] ,
535
+ tradeTxFees : getFeesApiResponse . txs [ 1 ] ,
536
+ } ,
537
+ } ) ;
538
+ } ) ;
489
539
} ) ;
490
540
491
541
describe ( 'submitSignedTransactions' , ( ) => {
@@ -823,36 +873,10 @@ describe('SmartTransactionsController', () => {
823
873
. spyOn ( smartTransactionsController , 'checkPoll' )
824
874
. mockImplementation ( ( ) => undefined ) ;
825
875
826
- const defaultState = {
827
- smartTransactions : {
828
- [ CHAIN_IDS . ETHEREUM ] : [ ] ,
829
- } ,
830
- userOptIn : undefined ,
831
- fees : {
832
- approvalTxFees : undefined ,
833
- tradeTxFees : undefined ,
834
- } ,
835
- feesByChainId : {
836
- [ CHAIN_IDS . ETHEREUM ] : {
837
- approvalTxFees : undefined ,
838
- tradeTxFees : undefined ,
839
- } ,
840
- [ CHAIN_IDS . GOERLI ] : {
841
- approvalTxFees : undefined ,
842
- tradeTxFees : undefined ,
843
- } ,
844
- } ,
845
- liveness : true ,
846
- livenessByChainId : {
847
- [ CHAIN_IDS . ETHEREUM ] : true ,
848
- [ CHAIN_IDS . GOERLI ] : true ,
849
- } ,
850
- } ;
851
-
852
876
// pending transactions in state are required to test polling
853
877
smartTransactionsController . update ( {
854
878
smartTransactionsState : {
855
- ...defaultState ,
879
+ ...defaultState . smartTransactionsState ,
856
880
smartTransactions : {
857
881
'0x1' : [
858
882
{
@@ -964,11 +988,7 @@ describe('SmartTransactionsController', () => {
964
988
) ;
965
989
966
990
// cleanup
967
- smartTransactionsController . update ( {
968
- smartTransactionsState : {
969
- ...defaultState ,
970
- } ,
971
- } ) ;
991
+ smartTransactionsController . update ( defaultState ) ;
972
992
973
993
smartTransactionsController . stopAllPolling ( ) ;
974
994
jest . clearAllTimers ( ) ;
0 commit comments