11import { NetworkType , convertHexToDecimal } from '@metamask/controller-utils' ;
22import type { NetworkState } from '@metamask/network-controller' ;
33import { NetworkStatus } from '@metamask/network-controller' ;
4+ import {
5+ TransactionStatus ,
6+ TransactionType ,
7+ } from '@metamask/transaction-controller' ;
48import nock from 'nock' ;
59import * as sinon from 'sinon' ;
610
@@ -58,6 +62,8 @@ jest.mock('@metamask/eth-query', () => {
5862} ) ;
5963
6064const addressFrom = '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3' ;
65+ const txHash =
66+ '0x0302b75dfb9fd9eb34056af031efcaee2a8cbd799ea054a85966165cd82a7356' ;
6167
6268const createUnsignedTransaction = ( chainId : number ) => {
6369 return {
@@ -184,7 +190,7 @@ const createSignedTransaction = () => {
184190
185191const createTxParams = ( ) => {
186192 return {
187- from : '0x268392a24B6b093127E8581eAfbD1DA228bAdAe3' ,
193+ from : addressFrom ,
188194 to : '0x0000000000000000000000000000000000000000' ,
189195 value : 0 ,
190196 data : '0x' ,
@@ -268,6 +274,37 @@ const testHistory = [
268274 } ,
269275] ;
270276
277+ const createTransactionMeta = (
278+ status : TransactionStatus = TransactionStatus . signed ,
279+ ) => {
280+ return {
281+ hash : txHash ,
282+ status,
283+ id : '1' ,
284+ txParams : {
285+ from : addressFrom ,
286+ to : '0x1678a085c290ebd122dc42cba69373b5953b831d' ,
287+ gasPrice : '0x77359400' ,
288+ gas : '0x7b0d' ,
289+ nonce : '0x4b' ,
290+ } ,
291+ type : TransactionType . simpleSend ,
292+ chainId : CHAIN_IDS . ETHEREUM ,
293+ time : 1624408066355 ,
294+ defaultGasEstimates : {
295+ gas : '0x7b0d' ,
296+ gasPrice : '0x77359400' ,
297+ } ,
298+ error : {
299+ name : 'Error' ,
300+ message : 'Details of the error' ,
301+ } ,
302+ securityProviderResponse : {
303+ flagAsDangerous : 0 ,
304+ } ,
305+ } ;
306+ } ;
307+
271308const ethereumChainIdDec = parseInt ( CHAIN_IDS . ETHEREUM , 16 ) ;
272309const goerliChainIdDec = parseInt ( CHAIN_IDS . GOERLI , 16 ) ;
273310
@@ -352,6 +389,7 @@ describe('SmartTransactionsController', () => {
352389 } ) ,
353390 provider : { sendAsync : jest . fn ( ) } ,
354391 confirmExternalTransaction : jest . fn ( ) ,
392+ getTransactions : jest . fn ( ) ,
355393 trackMetaMetricsEvent : trackMetaMetricsEventSpy ,
356394 getNetworkClientById : jest . fn ( ) . mockImplementation ( ( networkClientId ) => {
357395 switch ( networkClientId ) {
@@ -843,6 +881,12 @@ describe('SmartTransactionsController', () => {
843881 ...createStateAfterPending ( ) [ 0 ] ,
844882 history : testHistory ,
845883 } ;
884+
885+ jest
886+ . spyOn ( smartTransactionsController , 'getRegularTransactions' )
887+ . mockImplementation ( ( ) => {
888+ return [ createTransactionMeta ( ) ] ;
889+ } ) ;
846890 smartTransactionsController . update ( {
847891 smartTransactionsState : {
848892 ...smartTransactionsState ,
@@ -853,6 +897,10 @@ describe('SmartTransactionsController', () => {
853897 } ) ;
854898 const updateTransaction = {
855899 ...pendingStx ,
900+ statusMetadata : {
901+ ...pendingStx . statusMetadata ,
902+ minedHash : txHash ,
903+ } ,
856904 status : SmartTransactionStatuses . SUCCESS ,
857905 } ;
858906
@@ -862,8 +910,108 @@ describe('SmartTransactionsController', () => {
862910 networkClientId : 'mainnet' ,
863911 } ,
864912 ) ;
913+ await flushPromises ( ) ;
914+ expect (
915+ smartTransactionsController . confirmExternalTransaction ,
916+ ) . toHaveBeenCalledTimes ( 1 ) ;
917+ expect (
918+ smartTransactionsController . state . smartTransactionsState
919+ . smartTransactions [ CHAIN_IDS . ETHEREUM ] ,
920+ ) . toStrictEqual ( [
921+ {
922+ ...updateTransaction ,
923+ confirmed : true ,
924+ } ,
925+ ] ) ;
926+ } ) ;
865927
928+ it ( 'does not call the "confirmExternalTransaction" fn if a tx is already confirmed' , async ( ) => {
929+ const { smartTransactionsState } = smartTransactionsController . state ;
930+ const pendingStx = {
931+ ...createStateAfterPending ( ) [ 0 ] ,
932+ history : testHistory ,
933+ } ;
934+ jest
935+ . spyOn ( smartTransactionsController , 'getRegularTransactions' )
936+ . mockImplementation ( ( ) => {
937+ return [ createTransactionMeta ( TransactionStatus . confirmed ) ] ;
938+ } ) ;
939+ smartTransactionsController . update ( {
940+ smartTransactionsState : {
941+ ...smartTransactionsState ,
942+ smartTransactions : {
943+ [ CHAIN_IDS . ETHEREUM ] : [ pendingStx ] as SmartTransaction [ ] ,
944+ } ,
945+ } ,
946+ } ) ;
947+ const updateTransaction = {
948+ ...pendingStx ,
949+ status : SmartTransactionStatuses . SUCCESS ,
950+ statusMetadata : {
951+ ...pendingStx . statusMetadata ,
952+ minedHash : txHash ,
953+ } ,
954+ } ;
955+
956+ smartTransactionsController . updateSmartTransaction (
957+ updateTransaction as SmartTransaction ,
958+ {
959+ networkClientId : 'mainnet' ,
960+ } ,
961+ ) ;
962+ await flushPromises ( ) ;
963+ expect (
964+ smartTransactionsController . confirmExternalTransaction ,
965+ ) . not . toHaveBeenCalled ( ) ;
966+ expect (
967+ smartTransactionsController . state . smartTransactionsState
968+ . smartTransactions [ CHAIN_IDS . ETHEREUM ] ,
969+ ) . toStrictEqual ( [
970+ {
971+ ...updateTransaction ,
972+ confirmed : true ,
973+ } ,
974+ ] ) ;
975+ } ) ;
976+
977+ it ( 'does not call the "confirmExternalTransaction" fn if a tx is already submitted' , async ( ) => {
978+ const { smartTransactionsState } = smartTransactionsController . state ;
979+ const pendingStx = {
980+ ...createStateAfterPending ( ) [ 0 ] ,
981+ history : testHistory ,
982+ } ;
983+ jest
984+ . spyOn ( smartTransactionsController , 'getRegularTransactions' )
985+ . mockImplementation ( ( ) => {
986+ return [ createTransactionMeta ( TransactionStatus . submitted ) ] ;
987+ } ) ;
988+ smartTransactionsController . update ( {
989+ smartTransactionsState : {
990+ ...smartTransactionsState ,
991+ smartTransactions : {
992+ [ CHAIN_IDS . ETHEREUM ] : [ pendingStx ] as SmartTransaction [ ] ,
993+ } ,
994+ } ,
995+ } ) ;
996+ const updateTransaction = {
997+ ...pendingStx ,
998+ status : SmartTransactionStatuses . SUCCESS ,
999+ statusMetadata : {
1000+ ...pendingStx . statusMetadata ,
1001+ minedHash : txHash ,
1002+ } ,
1003+ } ;
1004+
1005+ smartTransactionsController . updateSmartTransaction (
1006+ updateTransaction as SmartTransaction ,
1007+ {
1008+ networkClientId : 'mainnet' ,
1009+ } ,
1010+ ) ;
8661011 await flushPromises ( ) ;
1012+ expect (
1013+ smartTransactionsController . confirmExternalTransaction ,
1014+ ) . not . toHaveBeenCalled ( ) ;
8671015 expect (
8681016 smartTransactionsController . state . smartTransactionsState
8691017 . smartTransactions [ CHAIN_IDS . ETHEREUM ] ,
0 commit comments