9
9
safelyExecute ,
10
10
ChainId ,
11
11
isSafeDynamicKey ,
12
+ type TraceCallback ,
12
13
} from '@metamask/controller-utils' ;
13
14
import EthQuery from '@metamask/eth-query' ;
14
15
import type {
@@ -27,7 +28,11 @@ import { TransactionStatus } from '@metamask/transaction-controller';
27
28
import { BigNumber } from 'bignumber.js' ;
28
29
import cloneDeep from 'lodash/cloneDeep' ;
29
30
30
- import { MetaMetricsEventCategory , MetaMetricsEventName } from './constants' ;
31
+ import {
32
+ MetaMetricsEventCategory ,
33
+ MetaMetricsEventName ,
34
+ SmartTransactionsTraceName ,
35
+ } from './constants' ;
31
36
import type {
32
37
Fees ,
33
38
Hex ,
@@ -206,6 +211,7 @@ type SmartTransactionsControllerOptions = {
206
211
getMetaMetricsProps : ( ) => Promise < MetaMetricsProps > ;
207
212
getFeatureFlags : ( ) => FeatureFlags ;
208
213
updateTransaction : ( transaction : TransactionMeta , note : string ) => void ;
214
+ trace ?: TraceCallback ;
209
215
} ;
210
216
211
217
export type SmartTransactionsControllerPollingInput = {
@@ -245,6 +251,8 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
245
251
246
252
#updateTransaction: SmartTransactionsControllerOptions [ 'updateTransaction' ] ;
247
253
254
+ #trace: TraceCallback ;
255
+
248
256
/* istanbul ignore next */
249
257
async #fetch( request : string , options ?: RequestInit ) {
250
258
const fetchOptions = {
@@ -272,6 +280,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
272
280
getMetaMetricsProps,
273
281
getFeatureFlags,
274
282
updateTransaction,
283
+ trace,
275
284
} : SmartTransactionsControllerOptions ) {
276
285
super ( {
277
286
name : controllerName ,
@@ -295,6 +304,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
295
304
this . #getMetaMetricsProps = getMetaMetricsProps ;
296
305
this . #getFeatureFlags = getFeatureFlags ;
297
306
this . #updateTransaction = updateTransaction ;
307
+ this . #trace = trace ?? ( ( ( _request , fn ) => fn ?.( ) ) as TraceCallback ) ;
298
308
299
309
this . initializeSmartTransactionsForChainId ( ) ;
300
310
@@ -860,7 +870,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
860
870
const chainId = this . #getChainId( {
861
871
networkClientId : selectedNetworkClientId ,
862
872
} ) ;
863
- const transactions = [ ] ;
873
+ const transactions : UnsignedTransaction [ ] = [ ] ;
864
874
let unsignedTradeTransactionWithNonce ;
865
875
if ( approvalTx ) {
866
876
const unsignedApprovalTransactionWithNonce =
@@ -880,14 +890,15 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
880
890
) ;
881
891
}
882
892
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
+ } ) ,
889
901
} ) ,
890
- } ,
891
902
) ;
892
903
let approvalTxFees : IndividualTxFees | null ;
893
904
let tradeTxFees : IndividualTxFees | null ;
@@ -943,15 +954,19 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
943
954
const ethQuery = this . #getEthQuery( {
944
955
networkClientId : selectedNetworkClientId ,
945
956
} ) ;
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
+ ) ,
955
970
) ;
956
971
const time = Date . now ( ) ;
957
972
let preTxBalance ;
@@ -1089,10 +1104,14 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
1089
1104
} = { } ,
1090
1105
) : Promise < void > {
1091
1106
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
+ ) ;
1096
1115
}
1097
1116
1098
1117
async fetchLiveness ( {
@@ -1103,8 +1122,10 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
1103
1122
const chainId = this . #getChainId( { networkClientId } ) ;
1104
1123
let liveness = false ;
1105
1124
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 ) ) ,
1108
1129
) ;
1109
1130
liveness = Boolean ( response . smartTransactions ) ;
1110
1131
} catch ( error ) {
0 commit comments