@metamask-previews/transaction-controller 8.0.1-preview.d32a7cc

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,327 @@
1
+ /// <reference types="node" />
2
+ import { Hardfork } from '@ethereumjs/common';
3
+ import type { TypedTransaction } from '@ethereumjs/tx';
4
+ import type { AddApprovalRequest } from '@metamask/approval-controller';
5
+ import type { BaseConfig, BaseState, RestrictedControllerMessenger } from '@metamask/base-controller';
6
+ import { BaseController } from '@metamask/base-controller';
7
+ import type { BlockTracker, NetworkState, Provider } from '@metamask/network-controller';
8
+ import { EventEmitter } from 'events';
9
+ import type { Transaction, TransactionMeta, WalletDevice } from './types';
10
+ export declare const HARDFORK = Hardfork.London;
11
+ /**
12
+ * @type Result
13
+ * @property result - Promise resolving to a new transaction hash
14
+ * @property transactionMeta - Meta information about this new transaction
15
+ */
16
+ export interface Result {
17
+ result: Promise<string>;
18
+ transactionMeta: TransactionMeta;
19
+ }
20
+ /**
21
+ * @type Fetch All Options
22
+ * @property fromBlock - String containing a specific block decimal number
23
+ * @property etherscanApiKey - API key to be used to fetch token transactions
24
+ */
25
+ export interface FetchAllOptions {
26
+ fromBlock?: string;
27
+ etherscanApiKey?: string;
28
+ }
29
+ export interface GasPriceValue {
30
+ gasPrice: string;
31
+ }
32
+ export interface FeeMarketEIP1559Values {
33
+ maxFeePerGas: string;
34
+ maxPriorityFeePerGas: string;
35
+ }
36
+ /**
37
+ * @type TransactionConfig
38
+ *
39
+ * Transaction controller configuration
40
+ * @property interval - Polling interval used to fetch new currency rate
41
+ * @property provider - Provider used to create a new underlying EthQuery instance
42
+ * @property sign - Method used to sign transactions
43
+ */
44
+ export interface TransactionConfig extends BaseConfig {
45
+ interval: number;
46
+ sign?: (transaction: Transaction, from: string) => Promise<any>;
47
+ txHistoryLimit: number;
48
+ }
49
+ /**
50
+ * @type MethodData
51
+ *
52
+ * Method data registry object
53
+ * @property registryMethod - Registry method raw string
54
+ * @property parsedRegistryMethod - Registry method object, containing name and method arguments
55
+ */
56
+ export interface MethodData {
57
+ registryMethod: string;
58
+ parsedRegistryMethod: Record<string, unknown>;
59
+ }
60
+ /**
61
+ * @type TransactionState
62
+ *
63
+ * Transaction controller state
64
+ * @property transactions - A list of TransactionMeta objects
65
+ * @property methodData - Object containing all known method data information
66
+ */
67
+ export interface TransactionState extends BaseState {
68
+ transactions: TransactionMeta[];
69
+ methodData: {
70
+ [key: string]: MethodData;
71
+ };
72
+ lastFetchedBlockNumbers: {
73
+ [key: string]: number;
74
+ };
75
+ }
76
+ /**
77
+ * Multiplier used to determine a transaction's increased gas fee during cancellation
78
+ */
79
+ export declare const CANCEL_RATE = 1.5;
80
+ /**
81
+ * Multiplier used to determine a transaction's increased gas fee during speed up
82
+ */
83
+ export declare const SPEED_UP_RATE = 1.1;
84
+ /**
85
+ * The name of the {@link TransactionController}.
86
+ */
87
+ declare const controllerName = "TransactionController";
88
+ /**
89
+ * The external actions available to the {@link TransactionController}.
90
+ */
91
+ declare type AllowedActions = AddApprovalRequest;
92
+ /**
93
+ * The messenger of the {@link TransactionController}.
94
+ */
95
+ export declare type TransactionControllerMessenger = RestrictedControllerMessenger<typeof controllerName, AllowedActions, never, AllowedActions['type'], never>;
96
+ /**
97
+ * Controller responsible for submitting and managing transactions.
98
+ */
99
+ export declare class TransactionController extends BaseController<TransactionConfig, TransactionState> {
100
+ private ethQuery;
101
+ private readonly nonceTracker;
102
+ private registry;
103
+ private readonly provider;
104
+ private handle?;
105
+ private readonly mutex;
106
+ private readonly getNetworkState;
107
+ private readonly messagingSystem;
108
+ private readonly incomingTransactionHelper;
109
+ private failTransaction;
110
+ private registryLookup;
111
+ /**
112
+ * EventEmitter instance used to listen to specific transactional events
113
+ */
114
+ hub: EventEmitter;
115
+ /**
116
+ * Name of this controller used during composition
117
+ */
118
+ name: string;
119
+ /**
120
+ * Method used to sign transactions
121
+ */
122
+ sign?: (transaction: TypedTransaction, from: string) => Promise<TypedTransaction>;
123
+ /**
124
+ * Creates a TransactionController instance.
125
+ *
126
+ * @param options - The controller options.
127
+ * @param options.blockTracker - The block tracker used to poll for new blocks data.
128
+ * @param options.getNetworkState - Gets the state of the network controller.
129
+ * @param options.getSelectedAddress - Gets the address of the currently selected account.
130
+ * @param options.incomingTransactions - Configuration options for incoming transaction support.
131
+ * @param options.incomingTransactions.apiKey - An optional API key to use when fetching remote transaction data.
132
+ * @param options.incomingTransactions.includeTokenTransfers - Whether or not to include ERC20 token transfers.
133
+ * @param options.incomingTransactions.isEnabled - Whether or not incoming transaction retrieval is enabled.
134
+ * @param options.incomingTransactions.updateTransactions - Whether or not to update local transactions using remote transaction data.
135
+ * @param options.messenger - The controller messenger.
136
+ * @param options.onNetworkStateChange - Allows subscribing to network controller state changes.
137
+ * @param options.provider - The provider used to create the underlying EthQuery instance.
138
+ * @param config - Initial options used to configure this controller.
139
+ * @param state - Initial state to set on this controller.
140
+ */
141
+ constructor({ blockTracker, getNetworkState, getSelectedAddress, incomingTransactions, messenger, onNetworkStateChange, provider, }: {
142
+ blockTracker: BlockTracker;
143
+ getNetworkState: () => NetworkState;
144
+ getSelectedAddress: () => string;
145
+ incomingTransactions: {
146
+ apiKey?: string;
147
+ includeTokenTransfers?: boolean;
148
+ isEnabled?: () => boolean;
149
+ updateTransactions?: boolean;
150
+ };
151
+ messenger: TransactionControllerMessenger;
152
+ onNetworkStateChange: (listener: (state: NetworkState) => void) => void;
153
+ provider: Provider;
154
+ }, config?: Partial<TransactionConfig>, state?: Partial<TransactionState>);
155
+ /**
156
+ * Starts a new polling interval.
157
+ *
158
+ * @param interval - The polling interval used to fetch new transaction statuses.
159
+ */
160
+ poll(interval?: number): Promise<void>;
161
+ /**
162
+ * Handle new method data request.
163
+ *
164
+ * @param fourBytePrefix - The method prefix.
165
+ * @returns The method data object corresponding to the given signature prefix.
166
+ */
167
+ handleMethodData(fourBytePrefix: string): Promise<MethodData>;
168
+ /**
169
+ * Add a new unapproved transaction to state. Parameters will be validated, a
170
+ * unique transaction id will be generated, and gas and gasPrice will be calculated
171
+ * if not provided. If A `<tx.id>:unapproved` hub event will be emitted once added.
172
+ *
173
+ * @param transaction - The transaction object to add.
174
+ * @param opts - Additional options to control how the transaction is added.
175
+ * @param opts.deviceConfirmedOn - An enum to indicate what device confirmed the transaction.
176
+ * @param opts.origin - The origin of the transaction request, such as a dApp hostname.
177
+ * @param opts.requireApproval - Whether the transaction requires approval by the user, defaults to true unless explicitly disabled.
178
+ * @returns Object containing a promise resolving to the transaction hash if approved.
179
+ */
180
+ addTransaction(transaction: Transaction, { deviceConfirmedOn, origin, requireApproval, }?: {
181
+ deviceConfirmedOn?: WalletDevice;
182
+ origin?: string;
183
+ requireApproval?: boolean | undefined;
184
+ }): Promise<Result>;
185
+ startIncomingTransactionPolling(): void;
186
+ stopIncomingTransactionPolling(): void;
187
+ updateIncomingTransactions(): Promise<void>;
188
+ /**
189
+ * Creates approvals for all unapproved transactions persisted.
190
+ */
191
+ initApprovals(): void;
192
+ /**
193
+ * Attempts to cancel a transaction based on its ID by setting its status to "rejected"
194
+ * and emitting a `<tx.id>:finished` hub event.
195
+ *
196
+ * @param transactionID - The ID of the transaction to cancel.
197
+ * @param gasValues - The gas values to use for the cancellation transaction.
198
+ */
199
+ stopTransaction(transactionID: string, gasValues?: GasPriceValue | FeeMarketEIP1559Values): Promise<void>;
200
+ /**
201
+ * Attempts to speed up a transaction increasing transaction gasPrice by ten percent.
202
+ *
203
+ * @param transactionID - The ID of the transaction to speed up.
204
+ * @param gasValues - The gas values to use for the speed up transation.
205
+ */
206
+ speedUpTransaction(transactionID: string, gasValues?: GasPriceValue | FeeMarketEIP1559Values): Promise<void>;
207
+ /**
208
+ * Estimates required gas for a given transaction.
209
+ *
210
+ * @param transaction - The transaction to estimate gas for.
211
+ * @returns The gas and gas price.
212
+ */
213
+ estimateGas(transaction: Transaction): Promise<{
214
+ gas: string;
215
+ gasPrice: any;
216
+ estimateGasError?: undefined;
217
+ } | {
218
+ gas: string;
219
+ gasPrice: any;
220
+ estimateGasError: string | undefined;
221
+ }>;
222
+ /**
223
+ * Check the status of submitted transactions on the network to determine whether they have
224
+ * been included in a block. Any that have been included in a block are marked as confirmed.
225
+ */
226
+ queryTransactionStatuses(): Promise<void>;
227
+ /**
228
+ * Updates an existing transaction in state.
229
+ *
230
+ * @param transactionMeta - The new transaction to store in state.
231
+ */
232
+ updateTransaction(transactionMeta: TransactionMeta): void;
233
+ /**
234
+ * Removes all transactions from state, optionally based on the current network.
235
+ *
236
+ * @param ignoreNetwork - Determines whether to wipe all transactions, or just those on the
237
+ * current network. If `true`, all transactions are wiped.
238
+ * @param address - If specified, only transactions originating from this address will be
239
+ * wiped on current network.
240
+ */
241
+ wipeTransactions(ignoreNetwork?: boolean, address?: string): void;
242
+ startIncomingTransactionProcessing(): void;
243
+ stopIncomingTransactionProcessing(): void;
244
+ private processApproval;
245
+ /**
246
+ * Approves a transaction and updates it's status in state. If this is not a
247
+ * retry transaction, a nonce will be generated. The transaction is signed
248
+ * using the sign configuration property, then published to the blockchain.
249
+ * A `<tx.id>:finished` hub event is fired after success or failure.
250
+ *
251
+ * @param transactionID - The ID of the transaction to approve.
252
+ */
253
+ private approveTransaction;
254
+ /**
255
+ * Cancels a transaction based on its ID by setting its status to "rejected"
256
+ * and emitting a `<tx.id>:finished` hub event.
257
+ *
258
+ * @param transactionID - The ID of the transaction to cancel.
259
+ */
260
+ private cancelTransaction;
261
+ /**
262
+ * Trim the amount of transactions that are set on the state. Checks
263
+ * if the length of the tx history is longer then desired persistence
264
+ * limit and then if it is removes the oldest confirmed or rejected tx.
265
+ * Pending or unapproved transactions will not be removed by this
266
+ * operation. For safety of presenting a fully functional transaction UI
267
+ * representation, this function will not break apart transactions with the
268
+ * same nonce, created on the same day, per network. Not accounting for transactions of the same
269
+ * nonce, same day and network combo can result in confusing or broken experiences
270
+ * in the UI. The transactions are then updated using the BaseController update.
271
+ *
272
+ * @param transactions - The transactions to be applied to the state.
273
+ * @returns The trimmed list of transactions.
274
+ */
275
+ private trimTransactionsForState;
276
+ /**
277
+ * Determines if the transaction is in a final state.
278
+ *
279
+ * @param status - The transaction status.
280
+ * @returns Whether the transaction is in a final state.
281
+ */
282
+ private isFinalState;
283
+ /**
284
+ * Whether the transaction has at least completed all local processing.
285
+ *
286
+ * @param status - The transaction status.
287
+ * @returns Whether the transaction is in a final state.
288
+ */
289
+ private isLocalFinalState;
290
+ /**
291
+ * Method to verify the state of a transaction using the Blockchain as a source of truth.
292
+ *
293
+ * @param meta - The local transaction to verify on the blockchain.
294
+ * @returns A tuple containing the updated transaction, and whether or not an update was required.
295
+ */
296
+ private blockchainTransactionStateReconciler;
297
+ /**
298
+ * Method to check if a tx has failed according to their receipt
299
+ * According to the Web3 docs:
300
+ * TRUE if the transaction was successful, FALSE if the EVM reverted the transaction.
301
+ * The receipt is not available for pending transactions and returns null.
302
+ *
303
+ * @param txHash - The transaction hash.
304
+ * @returns Whether the transaction has failed.
305
+ */
306
+ private checkTxReceiptStatusIsFailed;
307
+ private requestApproval;
308
+ private getTransaction;
309
+ private getApprovalId;
310
+ private isTransactionCompleted;
311
+ private getChainAndNetworkId;
312
+ private prepareUnsignedEthTx;
313
+ /**
314
+ * `@ethereumjs/tx` uses `@ethereumjs/common` as a configuration tool for
315
+ * specifying which chain, network, hardfork and EIPs to support for
316
+ * a transaction. By referencing this configuration, and analyzing the fields
317
+ * specified in txParams, @ethereumjs/tx is able to determine which EIP-2718
318
+ * transaction type to use.
319
+ *
320
+ * @returns common configuration object
321
+ */
322
+ private getCommonConfiguration;
323
+ private onIncomingTransactions;
324
+ private onUpdatedLastFetchedBlockNumbers;
325
+ }
326
+ export default TransactionController;
327
+ //# sourceMappingURL=TransactionController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"TransactionController.d.ts","sourceRoot":"","sources":["../src/TransactionController.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,QAAQ,EAA4B,MAAM,oBAAoB,CAAC;AACxE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