@@ -24,7 +24,6 @@ import type {
2424import {
2525 type TransactionParams ,
2626 TransactionStatus ,
27- TransactionType ,
2827} from '@metamask/transaction-controller' ;
2928import nock from 'nock' ;
3029import * as sinon from 'sinon' ;
@@ -55,15 +54,21 @@ type RootMessenger = Messenger<MockAnyNamespace, AllActions, AllEvents>;
5554jest . mock ( '@metamask/eth-query' , ( ) => {
5655 const EthQuery = jest . requireActual ( '@metamask/eth-query' ) ;
5756 return class FakeEthQuery extends EthQuery {
58- sendAsync = jest . fn ( ( { method } , callback ) => {
57+ sendAsync = jest . fn ( ( { method, params } , callback ) => {
5958 switch ( method ) {
6059 case 'eth_getBalance' : {
6160 callback ( null , '0x1000' ) ;
6261 break ;
6362 }
6463
6564 case 'eth_getTransactionReceipt' : {
66- callback ( null , { blockNumber : '123' } ) ;
65+ // Return null if txHash is empty/falsy
66+ const txHash = params ?. [ 0 ] ;
67+ if ( txHash ) {
68+ callback ( null , { blockNumber : '123' } ) ;
69+ } else {
70+ callback ( null , null ) ;
71+ }
6772 break ;
6873 }
6974
@@ -307,37 +312,6 @@ const testHistory = [
307312 } ,
308313] ;
309314
310- const createTransactionMeta = (
311- status : TransactionStatus = TransactionStatus . signed ,
312- ) => {
313- return {
314- hash : txHash ,
315- status,
316- id : '1' ,
317- txParams : {
318- from : addressFrom ,
319- to : '0x1678a085c290ebd122dc42cba69373b5953b831d' ,
320- gasPrice : '0x77359400' ,
321- gas : '0x7b0d' ,
322- nonce : '0x4b' ,
323- } ,
324- type : TransactionType . simpleSend ,
325- chainId : ChainId . mainnet ,
326- time : 1624408066355 ,
327- defaultGasEstimates : {
328- gas : '0x7b0d' ,
329- gasPrice : '0x77359400' ,
330- } ,
331- error : {
332- name : 'Error' ,
333- message : 'Details of the error' ,
334- } ,
335- securityProviderResponse : {
336- flagAsDangerous : 0 ,
337- } ,
338- } ;
339- } ;
340-
341315const ethereumChainIdDec = parseInt ( ChainId . mainnet , 16 ) ;
342316const sepoliaChainIdDec = parseInt ( ChainId . sepolia , 16 ) ;
343317
@@ -459,6 +433,7 @@ describe('SmartTransactionsController', () => {
459433
460434 controller . timeoutHandle = setTimeout ( ( ) => ( { } ) ) ;
461435
436+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
462437 controller . poll ( 1000 ) ;
463438
464439 expect ( updateSmartTransactionsSpy ) . toHaveBeenCalled ( ) ;
@@ -1395,6 +1370,147 @@ describe('SmartTransactionsController', () => {
13951370 } ,
13961371 ) ;
13971372 } ) ;
1373+
1374+ it ( 'confirms a smart transaction with SUCCESS status' , async ( ) => {
1375+ const { smartTransactionsState } =
1376+ getDefaultSmartTransactionsControllerState ( ) ;
1377+ const pendingStx = {
1378+ ...createStateAfterPending ( ) [ 0 ] ,
1379+ history : testHistory ,
1380+ } ;
1381+ await withController (
1382+ {
1383+ options : {
1384+ state : {
1385+ smartTransactionsState : {
1386+ ...smartTransactionsState ,
1387+ smartTransactions : {
1388+ [ ChainId . mainnet ] : [ pendingStx ] as SmartTransaction [ ] ,
1389+ } ,
1390+ } ,
1391+ } ,
1392+ } ,
1393+ } ,
1394+ async ( { controller } ) => {
1395+ const updateTransaction = {
1396+ ...pendingStx ,
1397+ statusMetadata : {
1398+ ...pendingStx . statusMetadata ,
1399+ minedHash : txHash ,
1400+ } ,
1401+ status : SmartTransactionStatuses . SUCCESS ,
1402+ } ;
1403+
1404+ controller . updateSmartTransaction (
1405+ updateTransaction as SmartTransaction ,
1406+ {
1407+ networkClientId : NetworkType . mainnet ,
1408+ } ,
1409+ ) ;
1410+ await flushPromises ( ) ;
1411+
1412+ expect (
1413+ controller . state . smartTransactionsState . smartTransactions [
1414+ ChainId . mainnet
1415+ ] [ 0 ] . confirmed ,
1416+ ) . toBe ( true ) ;
1417+ } ,
1418+ ) ;
1419+ } ) ;
1420+
1421+ it ( 'confirms a smart transaction with REVERTED status' , async ( ) => {
1422+ const { smartTransactionsState } =
1423+ getDefaultSmartTransactionsControllerState ( ) ;
1424+ const pendingStx = {
1425+ ...createStateAfterPending ( ) [ 0 ] ,
1426+ history : testHistory ,
1427+ } ;
1428+ await withController (
1429+ {
1430+ options : {
1431+ state : {
1432+ smartTransactionsState : {
1433+ ...smartTransactionsState ,
1434+ smartTransactions : {
1435+ [ ChainId . mainnet ] : [ pendingStx ] as SmartTransaction [ ] ,
1436+ } ,
1437+ } ,
1438+ } ,
1439+ } ,
1440+ } ,
1441+ async ( { controller } ) => {
1442+ const updateTransaction = {
1443+ ...pendingStx ,
1444+ statusMetadata : {
1445+ ...pendingStx . statusMetadata ,
1446+ minedHash : txHash ,
1447+ } ,
1448+ status : SmartTransactionStatuses . REVERTED ,
1449+ } ;
1450+
1451+ controller . updateSmartTransaction (
1452+ updateTransaction as SmartTransaction ,
1453+ {
1454+ networkClientId : NetworkType . mainnet ,
1455+ } ,
1456+ ) ;
1457+ await flushPromises ( ) ;
1458+
1459+ expect (
1460+ controller . state . smartTransactionsState . smartTransactions [
1461+ ChainId . mainnet
1462+ ] [ 0 ] . confirmed ,
1463+ ) . toBe ( true ) ;
1464+ } ,
1465+ ) ;
1466+ } ) ;
1467+
1468+ it ( 'does not confirm a smart transaction that does not have a minedHash' , async ( ) => {
1469+ const { smartTransactionsState } =
1470+ getDefaultSmartTransactionsControllerState ( ) ;
1471+ const pendingStx = {
1472+ ...createStateAfterPending ( ) [ 0 ] ,
1473+ history : testHistory ,
1474+ } ;
1475+ await withController (
1476+ {
1477+ options : {
1478+ state : {
1479+ smartTransactionsState : {
1480+ ...smartTransactionsState ,
1481+ smartTransactions : {
1482+ [ ChainId . mainnet ] : [ pendingStx ] as SmartTransaction [ ] ,
1483+ } ,
1484+ } ,
1485+ } ,
1486+ } ,
1487+ } ,
1488+ async ( { controller } ) => {
1489+ const updateTransaction = {
1490+ ...pendingStx ,
1491+ statusMetadata : {
1492+ ...pendingStx . statusMetadata ,
1493+ minedHash : '' ,
1494+ } ,
1495+ status : SmartTransactionStatuses . SUCCESS ,
1496+ } ;
1497+
1498+ controller . updateSmartTransaction (
1499+ updateTransaction as SmartTransaction ,
1500+ {
1501+ networkClientId : NetworkType . mainnet ,
1502+ } ,
1503+ ) ;
1504+ await flushPromises ( ) ;
1505+
1506+ expect (
1507+ controller . state . smartTransactionsState . smartTransactions [
1508+ ChainId . mainnet
1509+ ] [ 0 ] . confirmed ,
1510+ ) . toBeUndefined ( ) ;
1511+ } ,
1512+ ) ;
1513+ } ) ;
13981514 } ) ;
13991515
14001516 describe ( 'cancelSmartTransaction' , ( ) => {
@@ -2577,6 +2693,7 @@ async function withController<ReturnValue>(
25772693 triggerNetworStateChange,
25782694 } ) ;
25792695 } finally {
2696+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
25802697 controller . stop ( ) ;
25812698 controller . stopAllPolling ( ) ;
25822699 }
0 commit comments