6
6
} from '@metamask/transaction-controller' ;
7
7
import { EventEmitter } from 'stream' ;
8
8
9
- import { ADDRESS_ZERO , EMPTY_BYTES , ENTRYPOINT , VALUE_ZERO } from './constants' ;
9
+ import { ADDRESS_ZERO , EMPTY_BYTES , VALUE_ZERO } from './constants' ;
10
10
import * as BundlerHelper from './helpers/Bundler' ;
11
11
import * as PendingUserOperationTrackerHelper from './helpers/PendingUserOperationTracker' ;
12
12
import type { UserOperationMetadata } from './types' ;
@@ -47,6 +47,7 @@ const ERROR_CODE_MOCK = '1234';
47
47
const NETWORK_CLIENT_ID_MOCK = 'testNetworkClientId' ;
48
48
const TRANSACTION_HASH_MOCK = '0x456' ;
49
49
const ORIGIN_MOCK = 'test.com' ;
50
+ const ENTRYPOINT_MOCK = '0x789' ;
50
51
51
52
const USER_OPERATION_METADATA_MOCK = {
52
53
chainId : CHAIN_ID_MOCK ,
@@ -161,6 +162,7 @@ describe('UserOperationController', () => {
161
162
const updateGasFeesMock = jest . mocked ( updateGasFees ) ;
162
163
163
164
const optionsMock = {
165
+ entrypoint : ENTRYPOINT_MOCK ,
164
166
getGasFeeEstimates,
165
167
messenger,
166
168
} ;
@@ -336,7 +338,29 @@ describe('UserOperationController', () => {
336
338
verificationGasLimit :
337
339
PREPARE_USER_OPERATION_RESPONSE_MOCK . gas ?. verificationGasLimit ,
338
340
} ,
339
- ENTRYPOINT ,
341
+ ENTRYPOINT_MOCK ,
342
+ ) ;
343
+ } ) ;
344
+
345
+ it ( 'emits added event' , async ( ) => {
346
+ const controller = new UserOperationController ( optionsMock ) ;
347
+
348
+ const listener = jest . fn ( ) ;
349
+ controller . hub . on ( 'user-operation-added' , listener ) ;
350
+
351
+ const { id, hash } = await addUserOperation (
352
+ controller ,
353
+ ADD_USER_OPERATION_REQUEST_MOCK ,
354
+ { ...ADD_USER_OPERATION_OPTIONS_MOCK , smartContractAccount } ,
355
+ ) ;
356
+
357
+ await hash ( ) ;
358
+
359
+ expect ( listener ) . toHaveBeenCalledTimes ( 1 ) ;
360
+ expect ( listener ) . toHaveBeenCalledWith (
361
+ expect . objectContaining ( {
362
+ id,
363
+ } ) ,
340
364
) ;
341
365
} ) ;
342
366
@@ -346,7 +370,10 @@ describe('UserOperationController', () => {
346
370
const { id } = await addUserOperation (
347
371
controller ,
348
372
ADD_USER_OPERATION_REQUEST_MOCK ,
349
- { ...ADD_USER_OPERATION_OPTIONS_MOCK , smartContractAccount } ,
373
+ {
374
+ ...ADD_USER_OPERATION_OPTIONS_MOCK ,
375
+ smartContractAccount,
376
+ } ,
350
377
) ;
351
378
352
379
expect ( Object . keys ( controller . state . userOperations ) ) . toHaveLength ( 1 ) ;
@@ -362,6 +389,7 @@ describe('UserOperationController', () => {
362
389
id,
363
390
origin : ORIGIN_MOCK ,
364
391
status : UserOperationStatus . Unapproved ,
392
+ swapsMetadata : null ,
365
393
time : expect . any ( Number ) ,
366
394
transactionHash : null ,
367
395
userOperation : expect . objectContaining ( {
@@ -379,6 +407,75 @@ describe('UserOperationController', () => {
379
407
) ;
380
408
} ) ;
381
409
410
+ it ( 'includes swap metadata in metadata' , async ( ) => {
411
+ const controller = new UserOperationController ( optionsMock ) ;
412
+
413
+ const { id } = await addUserOperation (
414
+ controller ,
415
+ ADD_USER_OPERATION_REQUEST_MOCK ,
416
+ {
417
+ ...ADD_USER_OPERATION_OPTIONS_MOCK ,
418
+ smartContractAccount,
419
+ swaps : {
420
+ approvalTxId : 'testTxId' ,
421
+ destinationTokenAddress : '0x1' ,
422
+ destinationTokenDecimals : 3 ,
423
+ destinationTokenSymbol : 'TEST' ,
424
+ estimatedBaseFee : '0x2' ,
425
+ sourceTokenSymbol : 'ETH' ,
426
+ swapMetaData : { test : 'value' } ,
427
+ swapTokenValue : '0x3' ,
428
+ } ,
429
+ } ,
430
+ ) ;
431
+
432
+ expect ( Object . keys ( controller . state . userOperations ) ) . toHaveLength ( 1 ) ;
433
+ expect ( controller . state . userOperations [ id ] ) . toStrictEqual (
434
+ expect . objectContaining ( {
435
+ swapsMetadata : {
436
+ approvalTxId : 'testTxId' ,
437
+ destinationTokenAddress : '0x1' ,
438
+ destinationTokenDecimals : 3 ,
439
+ destinationTokenSymbol : 'TEST' ,
440
+ estimatedBaseFee : '0x2' ,
441
+ sourceTokenSymbol : 'ETH' ,
442
+ swapMetaData : { test : 'value' } ,
443
+ swapTokenValue : '0x3' ,
444
+ } ,
445
+ } ) ,
446
+ ) ;
447
+ } ) ;
448
+
449
+ it ( 'defaults missing swap metadata to null' , async ( ) => {
450
+ const controller = new UserOperationController ( optionsMock ) ;
451
+
452
+ const { id } = await addUserOperation (
453
+ controller ,
454
+ ADD_USER_OPERATION_REQUEST_MOCK ,
455
+ {
456
+ ...ADD_USER_OPERATION_OPTIONS_MOCK ,
457
+ smartContractAccount,
458
+ swaps : { } ,
459
+ } ,
460
+ ) ;
461
+
462
+ expect ( Object . keys ( controller . state . userOperations ) ) . toHaveLength ( 1 ) ;
463
+ expect ( controller . state . userOperations [ id ] ) . toStrictEqual (
464
+ expect . objectContaining ( {
465
+ swapsMetadata : {
466
+ approvalTxId : null ,
467
+ destinationTokenAddress : null ,
468
+ destinationTokenDecimals : null ,
469
+ destinationTokenSymbol : null ,
470
+ estimatedBaseFee : null ,
471
+ sourceTokenSymbol : null ,
472
+ swapMetaData : null ,
473
+ swapTokenValue : null ,
474
+ } ,
475
+ } ) ,
476
+ ) ;
477
+ } ) ;
478
+
382
479
it ( 'updates metadata in state after submission' , async ( ) => {
383
480
const controller = new UserOperationController ( optionsMock ) ;
384
481
@@ -457,7 +554,7 @@ describe('UserOperationController', () => {
457
554
initCode : EMPTY_BYTES ,
458
555
paymasterAndData : EMPTY_BYTES ,
459
556
} ) ,
460
- ENTRYPOINT ,
557
+ ENTRYPOINT_MOCK ,
461
558
) ;
462
559
} ) ;
463
560
@@ -1028,7 +1125,29 @@ describe('UserOperationController', () => {
1028
1125
) ;
1029
1126
} ) ;
1030
1127
1031
- it ( 'sets transaction type in metadata' , async ( ) => {
1128
+ it ( 'uses transaction type from request options' , async ( ) => {
1129
+ const controller = new UserOperationController ( optionsMock ) ;
1130
+
1131
+ const { id } = await addUserOperation (
1132
+ controller ,
1133
+ ADD_USER_OPERATION_REQUEST_MOCK ,
1134
+ {
1135
+ ...ADD_USER_OPERATION_OPTIONS_MOCK ,
1136
+ smartContractAccount,
1137
+ type : TransactionType . swap ,
1138
+ } ,
1139
+ ) ;
1140
+
1141
+ await flushPromises ( ) ;
1142
+
1143
+ expect ( controller . state . userOperations [ id ] . transactionType ) . toBe (
1144
+ TransactionType . swap ,
1145
+ ) ;
1146
+
1147
+ expect ( determineTransactionTypeMock ) . toHaveBeenCalledTimes ( 0 ) ;
1148
+ } ) ;
1149
+
1150
+ it ( 'determines transaction type if not set' , async ( ) => {
1032
1151
const controller = new UserOperationController ( optionsMock ) ;
1033
1152
1034
1153
const { id } = await addUserOperation (
0 commit comments