1
1
// eslint-disable-next-line import/no-nodejs-modules
2
2
import { hexlify } from '@ethersproject/bytes' ;
3
3
import type { BaseConfig , BaseState } from '@metamask/base-controller' ;
4
- import { safelyExecute , query } from '@metamask/controller-utils' ;
4
+ import { query , safelyExecute } from '@metamask/controller-utils' ;
5
5
import type { Provider } from '@metamask/eth-query' ;
6
6
import EthQuery from '@metamask/eth-query' ;
7
7
import type {
8
- NetworkState ,
9
- NetworkController ,
10
8
NetworkClientId ,
9
+ NetworkController ,
10
+ NetworkState ,
11
11
} from '@metamask/network-controller' ;
12
12
import { StaticIntervalPollingControllerV1 } from '@metamask/polling-controller' ;
13
13
import { BigNumber } from 'bignumber.js' ;
14
14
// eslint-disable-next-line import/no-nodejs-modules
15
- import { EventEmitter } from 'events' ;
15
+ import EventEmitter from 'events' ;
16
16
import cloneDeep from 'lodash/cloneDeep' ;
17
17
18
18
import {
@@ -21,31 +21,33 @@ import {
21
21
MetaMetricsEventName ,
22
22
} from './constants' ;
23
23
import type {
24
- SmartTransaction ,
25
- SignedTransaction ,
26
- SignedCanceledTransaction ,
27
- UnsignedTransaction ,
28
- SmartTransactionsStatus ,
29
24
Fees ,
30
- IndividualTxFees ,
31
25
Hex ,
26
+ IndividualTxFees ,
27
+ SignedCanceledTransaction ,
28
+ SignedTransaction ,
29
+ SmartTransaction ,
30
+ SmartTransactionsStatus ,
31
+ UnsignedTransaction ,
32
32
} from './types' ;
33
33
import { APIType , SmartTransactionStatuses } from './types' ;
34
34
import {
35
- getAPIRequestURL ,
36
- isSmartTransactionPending ,
37
35
calculateStatus ,
38
- snapshotFromTxMeta ,
39
- replayHistory ,
40
36
generateHistoryEntry ,
37
+ getAPIRequestURL ,
41
38
getStxProcessingTime ,
42
39
handleFetch ,
43
- isSmartTransactionCancellable ,
44
40
incrementNonceInHex ,
41
+ isSmartTransactionCancellable ,
42
+ isSmartTransactionPending ,
43
+ replayHistory ,
44
+ snapshotFromTxMeta ,
45
45
} from './utils' ;
46
46
47
47
const SECOND = 1000 ;
48
48
export const DEFAULT_INTERVAL = SECOND * 5 ;
49
+ const ETH_QUERY_ERROR_MSG =
50
+ '`ethQuery` is not defined on SmartTransactionsController' ;
49
51
50
52
export type SmartTransactionsControllerConfig = BaseConfig & {
51
53
interval : number ;
@@ -84,7 +86,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
84
86
85
87
private readonly getNonceLock : any ;
86
88
87
- private ethQuery : EthQuery ;
89
+ private ethQuery : EthQuery | undefined ;
88
90
89
91
public confirmExternalTransaction : any ;
90
92
@@ -168,7 +170,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
168
170
this . initialize ( ) ;
169
171
this . setIntervalLength ( this . config . interval ) ;
170
172
this . getNonceLock = getNonceLock ;
171
- this . ethQuery = new EthQuery ( provider ) ;
173
+ this . ethQuery = undefined ;
172
174
this . confirmExternalTransaction = confirmExternalTransaction ;
173
175
this . trackMetaMetricsEvent = trackMetaMetricsEvent ;
174
176
this . getNetworkClientById = getNetworkClientById ;
@@ -333,7 +335,7 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
333
335
ethQuery = this . ethQuery ,
334
336
} : {
335
337
chainId : Hex ;
336
- ethQuery : EthQuery ;
338
+ ethQuery : EthQuery | undefined ;
337
339
} ,
338
340
) : void {
339
341
const { smartTransactionsState } = this . state ;
@@ -345,6 +347,9 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
345
347
const isNewSmartTransaction = this . isNewSmartTransaction (
346
348
smartTransaction . uuid ,
347
349
) ;
350
+ if ( this . ethQuery === undefined ) {
351
+ throw new Error ( ETH_QUERY_ERROR_MSG ) ;
352
+ }
348
353
349
354
this . trackStxStatusChange (
350
355
smartTransaction ,
@@ -445,12 +450,16 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
445
450
ethQuery = this . ethQuery ,
446
451
} : {
447
452
chainId : Hex ;
448
- ethQuery : EthQuery ;
453
+ ethQuery : EthQuery | undefined ;
449
454
} ,
450
455
) {
451
456
if ( smartTransaction . skipConfirm ) {
452
457
return ;
453
458
}
459
+
460
+ if ( ethQuery === undefined ) {
461
+ throw new Error ( ETH_QUERY_ERROR_MSG ) ;
462
+ }
454
463
const txHash = smartTransaction . statusMetadata ?. minedHash ;
455
464
try {
456
465
const transactionReceipt : {
@@ -744,9 +753,15 @@ export default class SmartTransactionsController extends StaticIntervalPollingCo
744
753
} : {
745
754
networkClientId ?: NetworkClientId ;
746
755
} = { } ) : EthQuery {
747
- return networkClientId
748
- ? new EthQuery ( this . getNetworkClientById ( networkClientId ) . provider )
749
- : this . ethQuery ;
756
+ if ( networkClientId ) {
757
+ return new EthQuery ( this . getNetworkClientById ( networkClientId ) . provider ) ;
758
+ }
759
+
760
+ if ( this . ethQuery === undefined ) {
761
+ throw new Error ( ETH_QUERY_ERROR_MSG ) ;
762
+ }
763
+
764
+ return this . ethQuery ;
750
765
}
751
766
752
767
// TODO: This should return if the cancellation was on chain or not (for nonce management)
0 commit comments