99 safelyExecute ,
1010 ChainId ,
1111 isSafeDynamicKey ,
12+ type TraceCallback ,
1213} from '@metamask/controller-utils' ;
1314import EthQuery from '@metamask/eth-query' ;
1415import type {
@@ -27,7 +28,11 @@ import { TransactionStatus } from '@metamask/transaction-controller';
2728import { BigNumber } from 'bignumber.js' ;
2829import cloneDeep from 'lodash/cloneDeep' ;
2930
30- import { MetaMetricsEventCategory , MetaMetricsEventName } from './constants' ;
31+ import {
32+ MetaMetricsEventCategory ,
33+ MetaMetricsEventName ,
34+ SmartTransactionsTraceName ,
35+ } from './constants' ;
3136import type {
3237 Fees ,
3338 Hex ,
@@ -206,6 +211,7 @@ type SmartTransactionsControllerOptions = {
206211 getMetaMetricsProps : ( ) => Promise < MetaMetricsProps > ;
207212 getFeatureFlags : ( ) => FeatureFlags ;
208213 updateTransaction : ( transaction : TransactionMeta , note : string ) => void ;
214+ trace ?: TraceCallback ;
209215} ;
210216
211217export type SmartTransactionsControllerPollingInput = {
@@ -245,6 +251,8 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
245251
246252 #updateTransaction: SmartTransactionsControllerOptions [ 'updateTransaction' ] ;
247253
254+ #trace: TraceCallback ;
255+
248256 /* istanbul ignore next */
249257 async #fetch( request : string , options ?: RequestInit ) {
250258 const fetchOptions = {
@@ -272,6 +280,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
272280 getMetaMetricsProps,
273281 getFeatureFlags,
274282 updateTransaction,
283+ trace,
275284 } : SmartTransactionsControllerOptions ) {
276285 super ( {
277286 name : controllerName ,
@@ -295,6 +304,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
295304 this . #getMetaMetricsProps = getMetaMetricsProps ;
296305 this . #getFeatureFlags = getFeatureFlags ;
297306 this . #updateTransaction = updateTransaction ;
307+ this . #trace = trace ?? ( ( ( _request , fn ) => fn ?.( ) ) as TraceCallback ) ;
298308
299309 this . initializeSmartTransactionsForChainId ( ) ;
300310
@@ -860,7 +870,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
860870 const chainId = this . #getChainId( {
861871 networkClientId : selectedNetworkClientId ,
862872 } ) ;
863- const transactions = [ ] ;
873+ const transactions : UnsignedTransaction [ ] = [ ] ;
864874 let unsignedTradeTransactionWithNonce ;
865875 if ( approvalTx ) {
866876 const unsignedApprovalTransactionWithNonce =
@@ -880,14 +890,15 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
880890 ) ;
881891 }
882892 transactions . push ( unsignedTradeTransactionWithNonce ) ;
883- const data = await this . #fetch(
884- getAPIRequestURL ( APIType . GET_FEES , chainId ) ,
885- {
886- method : 'POST' ,
887- body : JSON . stringify ( {
888- txs : transactions ,
893+ const data = await this . #trace(
894+ { name : SmartTransactionsTraceName . GetFees } ,
895+ async ( ) =>
896+ await this . #fetch( getAPIRequestURL ( APIType . GET_FEES , chainId ) , {
897+ method : 'POST' ,
898+ body : JSON . stringify ( {
899+ txs : transactions ,
900+ } ) ,
889901 } ) ,
890- } ,
891902 ) ;
892903 let approvalTxFees : IndividualTxFees | null ;
893904 let tradeTxFees : IndividualTxFees | null ;
@@ -943,15 +954,19 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
943954 const ethQuery = this . #getEthQuery( {
944955 networkClientId : selectedNetworkClientId ,
945956 } ) ;
946- const data = await this . #fetch(
947- getAPIRequestURL ( APIType . SUBMIT_TRANSACTIONS , chainId ) ,
948- {
949- method : 'POST' ,
950- body : JSON . stringify ( {
951- rawTxs : signedTransactions ,
952- rawCancelTxs : signedCanceledTransactions ,
953- } ) ,
954- } ,
957+ const data = await this . #trace(
958+ { name : SmartTransactionsTraceName . SubmitTransactions } ,
959+ async ( ) =>
960+ await this . #fetch(
961+ getAPIRequestURL ( APIType . SUBMIT_TRANSACTIONS , chainId ) ,
962+ {
963+ method : 'POST' ,
964+ body : JSON . stringify ( {
965+ rawTxs : signedTransactions ,
966+ rawCancelTxs : signedCanceledTransactions ,
967+ } ) ,
968+ } ,
969+ ) ,
955970 ) ;
956971 const time = Date . now ( ) ;
957972 let preTxBalance ;
@@ -1089,10 +1104,14 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
10891104 } = { } ,
10901105 ) : Promise < void > {
10911106 const chainId = this . #getChainId( { networkClientId } ) ;
1092- await this . #fetch( getAPIRequestURL ( APIType . CANCEL , chainId ) , {
1093- method : 'POST' ,
1094- body : JSON . stringify ( { uuid } ) ,
1095- } ) ;
1107+ await this . #trace(
1108+ { name : SmartTransactionsTraceName . CancelTransaction } ,
1109+ async ( ) =>
1110+ await this . #fetch( getAPIRequestURL ( APIType . CANCEL , chainId ) , {
1111+ method : 'POST' ,
1112+ body : JSON . stringify ( { uuid } ) ,
1113+ } ) ,
1114+ ) ;
10961115 }
10971116
10981117 async fetchLiveness ( {
@@ -1103,8 +1122,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
11031122 const chainId = this . #getChainId( { networkClientId } ) ;
11041123 let liveness = false ;
11051124 try {
1106- const response = await this . #fetch(
1107- getAPIRequestURL ( APIType . LIVENESS , chainId ) ,
1125+ const response = await this . #trace(
1126+ { name : SmartTransactionsTraceName . FetchLiveness } ,
1127+ async ( ) =>
1128+ await this . #fetch( getAPIRequestURL ( APIType . LIVENESS , chainId ) ) ,
11081129 ) ;
11091130 liveness = Boolean ( response . smartTransactions ) ;
11101131 } catch ( error ) {
0 commit comments