@toruslabs/ethereum-controllers 4.1.0

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.
Files changed (88) hide show
  1. package/dist/ethereumControllers.cjs.js +6153 -0
  2. package/dist/ethereumControllers.cjs.js.map +1 -0
  3. package/dist/ethereumControllers.esm.js +5570 -0
  4. package/dist/ethereumControllers.esm.js.map +1 -0
  5. package/dist/ethereumControllers.umd.min.js +3 -0
  6. package/dist/ethereumControllers.umd.min.js.LICENSE.txt +38 -0
  7. package/dist/ethereumControllers.umd.min.js.map +1 -0
  8. package/dist/types/Account/AccountTrackerController.d.ts +35 -0
  9. package/dist/types/Block/PollingBlockTracker.d.ts +14 -0
  10. package/dist/types/Currency/CurrencyController.d.ts +30 -0
  11. package/dist/types/Gas/GasFeeController.d.ts +64 -0
  12. package/dist/types/Gas/IGasFeeController.d.ts +49 -0
  13. package/dist/types/Gas/gasUtil.d.ts +21 -0
  14. package/dist/types/Keyring/KeyringController.d.ts +20 -0
  15. package/dist/types/Message/AbstractMessageController.d.ts +36 -0
  16. package/dist/types/Message/DecryptMessageController.d.ts +20 -0
  17. package/dist/types/Message/EncryptionPublicKeyController.d.ts +20 -0
  18. package/dist/types/Message/MessageController.d.ts +20 -0
  19. package/dist/types/Message/PersonalMessageController.d.ts +20 -0
  20. package/dist/types/Message/TypedMessageController.d.ts +21 -0
  21. package/dist/types/Message/utils.d.ts +10 -0
  22. package/dist/types/Network/NetworkController.d.ts +40 -0
  23. package/dist/types/Network/createEthereumMiddleware.d.ts +66 -0
  24. package/dist/types/Network/createJsonRpcClient.d.ts +9 -0
  25. package/dist/types/Nfts/INftsController.d.ts +10 -0
  26. package/dist/types/Nfts/NftHandler.d.ts +35 -0
  27. package/dist/types/Nfts/NftsController.d.ts +40 -0
  28. package/dist/types/Preferences/PreferencesController.d.ts +53 -0
  29. package/dist/types/Tokens/ITokensController.d.ts +10 -0
  30. package/dist/types/Tokens/TokenHandler.d.ts +20 -0
  31. package/dist/types/Tokens/TokenRatesController.d.ts +42 -0
  32. package/dist/types/Tokens/TokensController.d.ts +42 -0
  33. package/dist/types/Transaction/NonceTracker.d.ts +37 -0
  34. package/dist/types/Transaction/PendingTransactionTracker.d.ts +32 -0
  35. package/dist/types/Transaction/TransactionController.d.ts +67 -0
  36. package/dist/types/Transaction/TransactionGasUtil.d.ts +21 -0
  37. package/dist/types/Transaction/TransactionStateHistoryHelper.d.ts +16 -0
  38. package/dist/types/Transaction/TransactionStateManager.d.ts +30 -0
  39. package/dist/types/Transaction/TransactionUtils.d.ts +70 -0
  40. package/dist/types/index.d.ts +43 -0
  41. package/dist/types/utils/abiDecoder.d.ts +17 -0
  42. package/dist/types/utils/abis.d.ts +84 -0
  43. package/dist/types/utils/constants.d.ts +81 -0
  44. package/dist/types/utils/contractAddresses.d.ts +1 -0
  45. package/dist/types/utils/conversionUtils.d.ts +42 -0
  46. package/dist/types/utils/helpers.d.ts +24 -0
  47. package/dist/types/utils/interfaces.d.ts +384 -0
  48. package/package.json +71 -0
  49. package/src/Account/AccountTrackerController.ts +157 -0
  50. package/src/Block/PollingBlockTracker.ts +89 -0
  51. package/src/Currency/CurrencyController.ts +117 -0
  52. package/src/Gas/GasFeeController.ts +254 -0
  53. package/src/Gas/IGasFeeController.ts +56 -0
  54. package/src/Gas/gasUtil.ts +163 -0
  55. package/src/Keyring/KeyringController.ts +118 -0
  56. package/src/Message/AbstractMessageController.ts +136 -0
  57. package/src/Message/DecryptMessageController.ts +81 -0
  58. package/src/Message/EncryptionPublicKeyController.ts +83 -0
  59. package/src/Message/MessageController.ts +74 -0
  60. package/src/Message/PersonalMessageController.ts +74 -0
  61. package/src/Message/TypedMessageController.ts +112 -0
  62. package/src/Message/utils.ts +107 -0
  63. package/src/Network/NetworkController.ts +184 -0
  64. package/src/Network/createEthereumMiddleware.ts +307 -0
  65. package/src/Network/createJsonRpcClient.ts +59 -0
  66. package/src/Nfts/INftsController.ts +13 -0
  67. package/src/Nfts/NftHandler.ts +191 -0
  68. package/src/Nfts/NftsController.ts +230 -0
  69. package/src/Preferences/PreferencesController.ts +409 -0
  70. package/src/Tokens/ITokensController.ts +13 -0
  71. package/src/Tokens/TokenHandler.ts +60 -0
  72. package/src/Tokens/TokenRatesController.ts +134 -0
  73. package/src/Tokens/TokensController.ts +278 -0
  74. package/src/Transaction/NonceTracker.ts +152 -0
  75. package/src/Transaction/PendingTransactionTracker.ts +235 -0
  76. package/src/Transaction/TransactionController.ts +558 -0
  77. package/src/Transaction/TransactionGasUtil.ts +74 -0
  78. package/src/Transaction/TransactionStateHistoryHelper.ts +41 -0
  79. package/src/Transaction/TransactionStateManager.ts +315 -0
  80. package/src/Transaction/TransactionUtils.ts +333 -0
  81. package/src/index.ts +45 -0
  82. package/src/utils/abiDecoder.ts +195 -0
  83. package/src/utils/abis.ts +677 -0
  84. package/src/utils/constants.ts +379 -0
  85. package/src/utils/contractAddresses.ts +21 -0
  86. package/src/utils/conversionUtils.ts +269 -0
  87. package/src/utils/helpers.ts +177 -0
  88. package/src/utils/interfaces.ts +454 -0
@@ -0,0 +1,235 @@
1
+ import {
2
+ BASE_TX_EVENT_TYPE,
3
+ ITransactionController,
4
+ TransactionStatus,
5
+ TX_CONFIRMED_EVENT_TYPE,
6
+ TX_DROPPED_EVENT_TYPE,
7
+ TX_EVENTS,
8
+ TX_FAILED_EVENT_TYPE,
9
+ TX_WARNING_EVENT_TYPE,
10
+ } from "@toruslabs/base-controllers";
11
+ import { SafeEventEmitter, SafeEventEmitterProvider } from "@toruslabs/openlogin-jrpc";
12
+ import log from "loglevel";
13
+
14
+ import { METHOD_TYPES } from "../utils/constants";
15
+ import { EthereumBlock, EthereumTransactionMeta, TransactionParams, TransactionReceipt } from "../utils/interfaces";
16
+ import NonceTracker from "./NonceTracker";
17
+ import TransactionStateManager from "./TransactionStateManager";
18
+
19
+ export default class PendingTransactionTracker extends SafeEventEmitter {
20
+ DROPPED_BUFFER_COUNT = 3;
21
+
22
+ private nonceTracker: NonceTracker;
23
+
24
+ private provider: SafeEventEmitterProvider;
25
+
26
+ private approveTransaction: ITransactionController<EthereumTransactionMeta>["approveTransaction"];
27
+
28
+ private droppedBlocksBufferByHash: Map<string, number>;
29
+
30
+ private getConfirmedTransactions: TransactionStateManager["getConfirmedTransactions"];
31
+
32
+ private getPendingTransactions: TransactionStateManager["getPendingTransactions"];
33
+
34
+ private publishTransaction: (rawTx: string) => Promise<string>;
35
+
36
+ constructor({
37
+ provider,
38
+ nonceTracker,
39
+ approveTransaction,
40
+ publishTransaction,
41
+ getPendingTransactions,
42
+ getConfirmedTransactions,
43
+ }: {
44
+ provider: SafeEventEmitterProvider;
45
+ nonceTracker: NonceTracker;
46
+ approveTransaction: ITransactionController<EthereumTransactionMeta>["approveTransaction"];
47
+ publishTransaction: (rawTx: string) => Promise<string>;
48
+ getPendingTransactions: TransactionStateManager["getPendingTransactions"];
49
+ getConfirmedTransactions: TransactionStateManager["getConfirmedTransactions"];
50
+ }) {
51
+ super();
52
+ this.provider = provider;
53
+ this.nonceTracker = nonceTracker;
54
+ this.approveTransaction = approveTransaction;
55
+ this.publishTransaction = publishTransaction;
56
+ this.getPendingTransactions = getPendingTransactions;
57
+ this.getConfirmedTransactions = getConfirmedTransactions;
58
+ this.droppedBlocksBufferByHash = new Map();
59
+ }
60
+
61
+ /**
62
+ checks the network for signed txs and releases the nonce global lock if it is
63
+ */
64
+ public async updatePendingTxs(): Promise<void> {
65
+ // in order to keep the nonceTracker accurate we block it while updating pending transactions
66
+ const nonceGlobalLock = await this.nonceTracker.getGlobalLock();
67
+ try {
68
+ const pendingTxs = this.getPendingTransactions();
69
+ await Promise.all(pendingTxs.map((txMeta) => this._checkPendingTx(txMeta)));
70
+ } catch (error) {
71
+ log.error("PendingTransactionTracker - Error updating pending transactions");
72
+ log.error(error);
73
+ }
74
+ nonceGlobalLock.releaseLock();
75
+ }
76
+
77
+ public async resubmitPendingTxs(block: EthereumBlock) {
78
+ const pending = this.getPendingTransactions();
79
+ // only try resubmitting if their are transactions to resubmit
80
+ if (pending.length === 0) return;
81
+ // Keep this as a for loop because we want to wait for each item to be submitted
82
+ for (const txMeta of pending) {
83
+ try {
84
+ await this._resubmitTx(txMeta, block.idempotencyKey);
85
+ } catch (error: unknown) {
86
+ /*
87
+ Dont marked as failed if the error is a "known" transaction warning
88
+ "there is already a transaction with the same sender-nonce
89
+ but higher/same gas price"
90
+
91
+ Also don't mark as failed if it has ever been broadcast successfully.
92
+ A successful broadcast means it may still be mined.
93
+ */
94
+ const errorMessage = (error as { value: Error }).value?.message?.toLowerCase() || (error as Error).message.toLowerCase();
95
+ const isKnownTx =
96
+ // geth
97
+ errorMessage.includes("replacement transaction underpriced") ||
98
+ errorMessage.includes("known transaction") ||
99
+ // parity
100
+ errorMessage.includes("gas price too low to replace") ||
101
+ errorMessage.includes("transaction with the same hash was already imported") ||
102
+ // other
103
+ errorMessage.includes("gateway timeout") ||
104
+ errorMessage.includes("nonce too low");
105
+ // ignore resubmit warnings, return early
106
+ if (isKnownTx) return;
107
+ // encountered real error - transition to error state
108
+ txMeta.warning = {
109
+ error: errorMessage,
110
+ message: "There was an error when resubmitting this transaction.",
111
+ };
112
+ this.emit(TX_EVENTS.TX_WARNING, { txMeta, error, txId: txMeta.id } as TX_WARNING_EVENT_TYPE<TransactionParams, EthereumTransactionMeta>);
113
+ }
114
+ }
115
+ }
116
+
117
+ async _resubmitTx(txMeta: EthereumTransactionMeta, latestBlockNumber?: string) {
118
+ if (!txMeta.firstRetryBlockNumber) {
119
+ this.emit(TX_EVENTS.TX_BLOCK_UPDATE, { txMeta, latestBlockNumber, txId: txMeta.id } as BASE_TX_EVENT_TYPE);
120
+ }
121
+
122
+ const firstRetryBlockNumber = txMeta.firstRetryBlockNumber || latestBlockNumber;
123
+ const txBlockDistance = Number.parseInt(latestBlockNumber, 16) - Number.parseInt(firstRetryBlockNumber, 16);
124
+
125
+ const retryCount = txMeta.retryCount || 0;
126
+
127
+ // Exponential backoff to limit retries at publishing (capped at last 15 mins)
128
+ if (txBlockDistance <= Math.min(50, 2 ** retryCount)) return undefined;
129
+
130
+ // Only auto-submit already-signed txs:
131
+ if (!("rawTx" in txMeta)) return this.approveTransaction(txMeta.id);
132
+
133
+ const { rawTx } = txMeta;
134
+ const txHash = await this.publishTransaction(rawTx as string);
135
+
136
+ // Increment successful tries:
137
+ this.emit(TX_EVENTS.TX_RETRY, { txMeta, txId: txMeta.id } as BASE_TX_EVENT_TYPE);
138
+ return txHash;
139
+ }
140
+
141
+ async _checkPendingTx(foundTx: EthereumTransactionMeta): Promise<void> {
142
+ const txMeta = foundTx;
143
+ const txHash = txMeta.transactionHash;
144
+ const txId = txMeta.id;
145
+
146
+ // Only check submitted txs
147
+ if (txMeta.status !== TransactionStatus.submitted) return;
148
+
149
+ // extra check in case there was an uncaught error during the
150
+ // signature and submission process
151
+ if (!txHash) {
152
+ const noTxHashError = new Error("We had an error while submitting this transaction, please try again.");
153
+ noTxHashError.name = "NoTxHashError";
154
+ this.emit(TX_EVENTS.TX_FAILED, { txId, error: noTxHashError } as TX_FAILED_EVENT_TYPE);
155
+ return;
156
+ }
157
+
158
+ // If another tx with the same nonce is mined, set as failed.
159
+ if (this._checkIfNonceIsTaken(txMeta)) {
160
+ this.emit(TX_EVENTS.TX_DROPPED, { txId } as TX_DROPPED_EVENT_TYPE);
161
+ return;
162
+ }
163
+
164
+ try {
165
+ const transactionReceipt = await this.provider.request<[string], TransactionReceipt>({
166
+ method: METHOD_TYPES.ETH_GET_TRANSACTION_RECEIPT,
167
+ params: [txHash],
168
+ });
169
+ if (transactionReceipt?.blockNumber) {
170
+ const { baseFeePerGas, timestamp } = await this.provider.request<[string, boolean], EthereumBlock>({
171
+ method: METHOD_TYPES.ETH_GET_BLOCK_BY_HASH,
172
+ params: [transactionReceipt.blockHash, false],
173
+ });
174
+ this.emit(TX_EVENTS.TX_CONFIRMED, {
175
+ txId,
176
+ txReceipt: transactionReceipt,
177
+ baseFeePerGas,
178
+ blockTimestamp: timestamp,
179
+ } as TX_CONFIRMED_EVENT_TYPE);
180
+ return;
181
+ }
182
+ } catch (error) {
183
+ log.error("error while loading tx", error);
184
+ txMeta.warning = {
185
+ error: (error as Error).message,
186
+ message: "There was a problem loading this transaction.",
187
+ };
188
+ this.emit(TX_EVENTS.TX_WARNING, { txMeta } as TX_WARNING_EVENT_TYPE<TransactionParams, EthereumTransactionMeta>);
189
+ }
190
+
191
+ if (await this._checkIfTxWasDropped(txMeta)) {
192
+ this.emit(TX_EVENTS.TX_DROPPED, { txId } as TX_DROPPED_EVENT_TYPE);
193
+ }
194
+ }
195
+
196
+ async _checkIfTxWasDropped(txMeta: EthereumTransactionMeta): Promise<boolean> {
197
+ const {
198
+ transactionHash: txHash,
199
+ transaction: { nonce, from },
200
+ } = txMeta;
201
+ const networkNextNonce = await this.provider.request<[string, string], string>({
202
+ method: METHOD_TYPES.ETH_GET_TRANSACTION_COUNT,
203
+ params: [from, "latest"],
204
+ });
205
+
206
+ if (Number.parseInt(nonce, 16) >= Number.parseInt(networkNextNonce, 16)) {
207
+ return false;
208
+ }
209
+
210
+ if (!this.droppedBlocksBufferByHash.has(txHash)) {
211
+ this.droppedBlocksBufferByHash.set(txHash, 0);
212
+ }
213
+
214
+ const currentBlockBuffer = this.droppedBlocksBufferByHash.get(txHash);
215
+
216
+ if (currentBlockBuffer < this.DROPPED_BUFFER_COUNT) {
217
+ this.droppedBlocksBufferByHash.set(txHash, currentBlockBuffer + 1);
218
+ return false;
219
+ }
220
+
221
+ this.droppedBlocksBufferByHash.delete(txHash);
222
+ return true;
223
+ }
224
+
225
+ _checkIfNonceIsTaken(txMeta: EthereumTransactionMeta) {
226
+ const address = txMeta.transaction.from;
227
+ const completed = this.getConfirmedTransactions(address);
228
+ return completed.some((otherMeta) => {
229
+ if (otherMeta.id === txMeta.id) {
230
+ return false;
231
+ }
232
+ return otherMeta.transaction.nonce === txMeta.transaction.nonce;
233
+ });
234
+ }
235
+ }