@@ -14,6 +14,7 @@ import EthQuery from '@metamask/eth-query';
1414import type {
1515 NetworkClientId ,
1616 NetworkControllerGetNetworkClientByIdAction ,
17+ NetworkControllerGetStateAction ,
1718 NetworkControllerStateChangeEvent ,
1819} from '@metamask/network-controller' ;
1920import { StaticIntervalPollingController } from '@metamask/polling-controller' ;
@@ -138,7 +139,9 @@ export type SmartTransactionsControllerGetStateAction =
138139export type SmartTransactionsControllerActions =
139140 SmartTransactionsControllerGetStateAction ;
140141
141- export type AllowedActions = NetworkControllerGetNetworkClientByIdAction ;
142+ type AllowedActions =
143+ | NetworkControllerGetNetworkClientByIdAction
144+ | NetworkControllerGetStateAction ;
142145
143146export type SmartTransactionsControllerStateChangeEvent =
144147 ControllerStateChangeEvent <
@@ -164,7 +167,7 @@ export type SmartTransactionsControllerEvents =
164167 | SmartTransactionsControllerSmartTransactionEvent
165168 | SmartTransactionsControllerSmartTransactionConfirmationDoneEvent ;
166169
167- export type AllowedEvents = NetworkControllerStateChangeEvent ;
170+ type AllowedEvents = NetworkControllerStateChangeEvent ;
168171
169172/**
170173 * The messenger of the {@link SmartTransactionsController}.
@@ -763,6 +766,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
763766 status : calculateStatus ( stxStatus ) ,
764767 cancellable : isSmartTransactionCancellable ( stxStatus ) ,
765768 uuid,
769+ networkClientId,
766770 } ;
767771 await this . #createOrUpdateSmartTransaction( smartTransaction , {
768772 chainId,
@@ -773,10 +777,14 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
773777 return data ;
774778 }
775779
776- async addNonceToTransaction (
780+ async # addNonceToTransaction(
777781 transaction : UnsignedTransaction ,
782+ networkClientId : NetworkClientId ,
778783 ) : Promise < UnsignedTransaction > {
779- const nonceLock = await this . #getNonceLock( transaction . from ) ;
784+ const nonceLock = await this . #getNonceLock(
785+ transaction . from ,
786+ networkClientId ,
787+ ) ;
780788 const nonce = nonceLock . nextNonce ;
781789 nonceLock . releaseLock ( ) ;
782790 return {
@@ -802,12 +810,18 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
802810 approvalTx ?: UnsignedTransaction ,
803811 { networkClientId } : { networkClientId ?: NetworkClientId } = { } ,
804812 ) : Promise < Fees > {
805- const chainId = this . #getChainId( { networkClientId } ) ;
813+ const selectedNetworkClientId =
814+ networkClientId ??
815+ this . messagingSystem . call ( 'NetworkController:getState' )
816+ . selectedNetworkClientId ;
817+ const chainId = this . #getChainId( {
818+ networkClientId : selectedNetworkClientId ,
819+ } ) ;
806820 const transactions = [ ] ;
807821 let unsignedTradeTransactionWithNonce ;
808822 if ( approvalTx ) {
809823 const unsignedApprovalTransactionWithNonce =
810- await this . addNonceToTransaction ( approvalTx ) ;
824+ await this . # addNonceToTransaction( approvalTx , selectedNetworkClientId ) ;
811825 transactions . push ( unsignedApprovalTransactionWithNonce ) ;
812826 unsignedTradeTransactionWithNonce = {
813827 ...tradeTx ,
@@ -817,8 +831,9 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
817831 } else if ( tradeTx . nonce ) {
818832 unsignedTradeTransactionWithNonce = tradeTx ;
819833 } else {
820- unsignedTradeTransactionWithNonce = await this . addNonceToTransaction (
834+ unsignedTradeTransactionWithNonce = await this . # addNonceToTransaction(
821835 tradeTx ,
836+ selectedNetworkClientId ,
822837 ) ;
823838 }
824839 transactions . push ( unsignedTradeTransactionWithNonce ) ;
@@ -875,8 +890,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
875890 txParams ?: TransactionParams ;
876891 networkClientId ?: NetworkClientId ;
877892 } ) {
878- const chainId = this . #getChainId( { networkClientId } ) ;
879- const ethQuery = this . #getEthQuery( { networkClientId } ) ;
893+ const selectedNetworkClientId =
894+ networkClientId ??
895+ this . messagingSystem . call ( 'NetworkController:getState' )
896+ . selectedNetworkClientId ;
897+ const chainId = this . #getChainId( {
898+ networkClientId : selectedNetworkClientId ,
899+ } ) ;
900+ const ethQuery = this . #getEthQuery( {
901+ networkClientId : selectedNetworkClientId ,
902+ } ) ;
880903 const data = await this . #fetch(
881904 getAPIRequestURL ( APIType . SUBMIT_TRANSACTIONS , chainId ) ,
882905 {
@@ -904,7 +927,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
904927 let nonceDetails = { } ;
905928
906929 if ( requiresNonce ) {
907- nonceLock = await this . #getNonceLock( txParams . from ) ;
930+ nonceLock = await this . #getNonceLock(
931+ txParams . from ,
932+ selectedNetworkClientId ,
933+ ) ;
908934 nonce = hexlify ( nonceLock . nextNonce ) ;
909935 nonceDetails = nonceLock . nonceDetails ;
910936 txParams . nonce ??= nonce ;
@@ -928,6 +954,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
928954 cancellable : true ,
929955 type : transactionMeta ?. type ?? 'swap' ,
930956 transactionId : transactionMeta ?. id ,
957+ networkClientId : selectedNetworkClientId ,
931958 } ,
932959 { chainId, ethQuery } ,
933960 ) ;
0 commit comments