-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
transaction_errors.ts
572 lines (492 loc) · 16.3 KB
/
transaction_errors.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
This file is part of web3.js.
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/* eslint-disable max-classes-per-file */
import {
Bytes,
HexString,
Numbers,
TransactionReceipt,
Web3ValidationErrorObject,
} from 'web3-types';
import {
ERR_RAW_TX_UNDEFINED,
ERR_TX,
ERR_TX_BLOCK_TIMEOUT,
ERR_TX_CONTRACT_NOT_STORED,
ERR_TX_CHAIN_ID_MISMATCH,
ERR_TX_DATA_AND_INPUT,
ERR_TX_GAS_MISMATCH,
ERR_TX_CHAIN_MISMATCH,
ERR_TX_HARDFORK_MISMATCH,
ERR_TX_INVALID_CALL,
ERR_TX_INVALID_CHAIN_INFO,
ERR_TX_INVALID_FEE_MARKET_GAS,
ERR_TX_INVALID_FEE_MARKET_GAS_PRICE,
ERR_TX_INVALID_LEGACY_FEE_MARKET,
ERR_TX_INVALID_LEGACY_GAS,
ERR_TX_INVALID_NONCE_OR_CHAIN_ID,
ERR_TX_INVALID_OBJECT,
ERR_TX_INVALID_SENDER,
ERR_TX_INVALID_RECEIVER,
ERR_TX_LOCAL_WALLET_NOT_AVAILABLE,
ERR_TX_MISSING_CHAIN_INFO,
ERR_TX_MISSING_CUSTOM_CHAIN,
ERR_TX_MISSING_CUSTOM_CHAIN_ID,
ERR_TX_MISSING_GAS,
ERR_TX_NO_CONTRACT_ADDRESS,
ERR_TX_NOT_FOUND,
ERR_TX_OUT_OF_GAS,
ERR_TX_POLLING_TIMEOUT,
ERR_TX_RECEIPT_MISSING_BLOCK_NUMBER,
ERR_TX_RECEIPT_MISSING_OR_BLOCKHASH_NULL,
ERR_TX_REVERT_INSTRUCTION,
ERR_TX_REVERT_TRANSACTION,
ERR_TX_REVERT_WITHOUT_REASON,
ERR_TX_SEND_TIMEOUT,
ERR_TX_SIGNING,
ERR_TX_UNABLE_TO_POPULATE_NONCE,
ERR_TX_UNSUPPORTED_EIP_1559,
ERR_TX_UNSUPPORTED_TYPE,
ERR_TX_REVERT_TRANSACTION_CUSTOM_ERROR,
ERR_TX_INVALID_PROPERTIES_FOR_TYPE,
} from '../error_codes.js';
import { InvalidValueError, BaseWeb3Error } from '../web3_error_base.js';
export class TransactionError<ReceiptType = TransactionReceipt> extends BaseWeb3Error {
public code = ERR_TX;
public constructor(message: string, public receipt?: ReceiptType) {
super(message);
}
public toJSON() {
return { ...super.toJSON(), receipt: this.receipt };
}
}
export class RevertInstructionError extends BaseWeb3Error {
public code = ERR_TX_REVERT_INSTRUCTION;
public constructor(public reason: string, public signature: string) {
super(`Your request got reverted with the following reason string: ${reason}`);
}
public toJSON() {
return { ...super.toJSON(), reason: this.reason, signature: this.signature };
}
}
export class TransactionRevertInstructionError<
ReceiptType = TransactionReceipt,
> extends BaseWeb3Error {
public code = ERR_TX_REVERT_TRANSACTION;
public constructor(
public reason: string,
public signature?: string,
public receipt?: ReceiptType,
public data?: string,
) {
super(
`Transaction has been reverted by the EVM${
receipt === undefined ? '' : `:\n ${BaseWeb3Error.convertToString(receipt)}`
}`,
);
}
public toJSON() {
return {
...super.toJSON(),
reason: this.reason,
signature: this.signature,
receipt: this.receipt,
data: this.data,
};
}
}
/**
* This error is used when a transaction to a smart contract fails and
* a custom user error (https://blog.soliditylang.org/2021/04/21/custom-errors/)
* is able to be parsed from the revert reason
*/
export class TransactionRevertWithCustomError<
ReceiptType = TransactionReceipt,
> extends TransactionRevertInstructionError<ReceiptType> {
public code = ERR_TX_REVERT_TRANSACTION_CUSTOM_ERROR;
public constructor(
public reason: string,
public customErrorName: string,
public customErrorDecodedSignature: string,
public customErrorArguments: Record<string, unknown>,
public signature?: string,
public receipt?: ReceiptType,
public data?: string,
) {
super(reason);
}
public toJSON() {
return {
...super.toJSON(),
reason: this.reason,
customErrorName: this.customErrorName,
customErrorDecodedSignature: this.customErrorDecodedSignature,
customErrorArguments: this.customErrorArguments,
signature: this.signature,
receipt: this.receipt,
data: this.data,
};
}
}
export class NoContractAddressFoundError extends TransactionError {
public constructor(receipt: TransactionReceipt) {
super("The transaction receipt didn't contain a contract address.", receipt);
this.code = ERR_TX_NO_CONTRACT_ADDRESS;
}
public toJSON() {
return { ...super.toJSON(), receipt: this.receipt };
}
}
export class ContractCodeNotStoredError extends TransactionError {
public constructor(receipt: TransactionReceipt) {
super("The contract code couldn't be stored, please check your gas limit.", receipt);
this.code = ERR_TX_CONTRACT_NOT_STORED;
}
}
export class TransactionRevertedWithoutReasonError<
ReceiptType = TransactionReceipt,
> extends TransactionError<ReceiptType> {
public constructor(receipt?: ReceiptType) {
super(
`Transaction has been reverted by the EVM${
receipt === undefined ? '' : `:\n ${BaseWeb3Error.convertToString(receipt)}`
}`,
receipt,
);
this.code = ERR_TX_REVERT_WITHOUT_REASON;
}
}
export class TransactionOutOfGasError extends TransactionError {
public constructor(receipt: TransactionReceipt) {
super(
`Transaction ran out of gas. Please provide more gas:\n ${JSON.stringify(
receipt,
undefined,
2,
)}`,
receipt,
);
this.code = ERR_TX_OUT_OF_GAS;
}
}
export class UndefinedRawTransactionError extends TransactionError {
public constructor() {
super(`Raw transaction undefined`);
this.code = ERR_RAW_TX_UNDEFINED;
}
}
export class TransactionNotFound extends TransactionError {
public constructor() {
super('Transaction not found');
this.code = ERR_TX_NOT_FOUND;
}
}
export class InvalidTransactionWithSender extends InvalidValueError {
public code = ERR_TX_INVALID_SENDER;
public constructor(value: unknown) {
super(value, 'invalid transaction with invalid sender');
}
}
export class InvalidTransactionWithReceiver extends InvalidValueError {
public code = ERR_TX_INVALID_RECEIVER;
public constructor(value: unknown) {
super(value, 'invalid transaction with invalid receiver');
}
}
export class InvalidTransactionCall extends InvalidValueError {
public code = ERR_TX_INVALID_CALL;
public constructor(value: unknown) {
super(value, 'invalid transaction call');
}
}
export class MissingCustomChainError extends InvalidValueError {
public code = ERR_TX_MISSING_CUSTOM_CHAIN;
public constructor() {
super(
'MissingCustomChainError',
'If tx.common is provided it must have tx.common.customChain',
);
}
}
export class MissingCustomChainIdError extends InvalidValueError {
public code = ERR_TX_MISSING_CUSTOM_CHAIN_ID;
public constructor() {
super(
'MissingCustomChainIdError',
'If tx.common is provided it must have tx.common.customChain and tx.common.customChain.chainId',
);
}
}
export class ChainIdMismatchError extends InvalidValueError {
public code = ERR_TX_CHAIN_ID_MISMATCH;
public constructor(value: { txChainId: unknown; customChainId: unknown }) {
super(
JSON.stringify(value),
// https://github.com/ChainSafe/web3.js/blob/8783f4d64e424456bdc53b34ef1142d0a7cee4d7/packages/web3-eth-accounts/src/index.js#L176
'Chain Id doesnt match in tx.chainId tx.common.customChain.chainId',
);
}
}
export class ChainMismatchError extends InvalidValueError {
public code = ERR_TX_CHAIN_MISMATCH;
public constructor(value: { txChain: unknown; baseChain: unknown }) {
super(JSON.stringify(value), 'Chain doesnt match in tx.chain tx.common.basechain');
}
}
export class HardforkMismatchError extends InvalidValueError {
public code = ERR_TX_HARDFORK_MISMATCH;
public constructor(value: { txHardfork: unknown; commonHardfork: unknown }) {
super(JSON.stringify(value), 'hardfork doesnt match in tx.hardfork tx.common.hardfork');
}
}
export class CommonOrChainAndHardforkError extends InvalidValueError {
public code = ERR_TX_INVALID_CHAIN_INFO;
public constructor() {
super(
'CommonOrChainAndHardforkError',
'Please provide the common object or the chain and hardfork property but not all together.',
);
}
}
export class MissingChainOrHardforkError extends InvalidValueError {
public code = ERR_TX_MISSING_CHAIN_INFO;
public constructor(value: { chain: string | undefined; hardfork: string | undefined }) {
super(
'MissingChainOrHardforkError',
`When specifying chain and hardfork, both values must be defined. Received "chain": ${
value.chain ?? 'undefined'
}, "hardfork": ${value.hardfork ?? 'undefined'}`,
);
}
}
export class MissingGasError extends InvalidValueError {
public code = ERR_TX_MISSING_GAS;
public constructor(value: {
gas: Numbers | undefined;
gasPrice: Numbers | undefined;
maxPriorityFeePerGas: Numbers | undefined;
maxFeePerGas: Numbers | undefined;
}) {
super(
`gas: ${value.gas ?? 'undefined'}, gasPrice: ${
value.gasPrice ?? 'undefined'
}, maxPriorityFeePerGas: ${value.maxPriorityFeePerGas ?? 'undefined'}, maxFeePerGas: ${
value.maxFeePerGas ?? 'undefined'
}`,
'"gas" is missing',
);
}
}
export class TransactionGasMismatchError extends InvalidValueError {
public code = ERR_TX_GAS_MISMATCH;
public constructor(value: {
gas: Numbers | undefined;
gasPrice: Numbers | undefined;
maxPriorityFeePerGas: Numbers | undefined;
maxFeePerGas: Numbers | undefined;
}) {
super(
`gas: ${value.gas ?? 'undefined'}, gasPrice: ${
value.gasPrice ?? 'undefined'
}, maxPriorityFeePerGas: ${value.maxPriorityFeePerGas ?? 'undefined'}, maxFeePerGas: ${
value.maxFeePerGas ?? 'undefined'
}`,
'transaction must specify legacy or fee market gas properties, not both',
);
}
}
export class InvalidGasOrGasPrice extends InvalidValueError {
public code = ERR_TX_INVALID_LEGACY_GAS;
public constructor(value: { gas: Numbers | undefined; gasPrice: Numbers | undefined }) {
super(
`gas: ${value.gas ?? 'undefined'}, gasPrice: ${value.gasPrice ?? 'undefined'}`,
'Gas or gasPrice is lower than 0',
);
}
}
export class InvalidMaxPriorityFeePerGasOrMaxFeePerGas extends InvalidValueError {
public code = ERR_TX_INVALID_FEE_MARKET_GAS;
public constructor(value: {
maxPriorityFeePerGas: Numbers | undefined;
maxFeePerGas: Numbers | undefined;
}) {
super(
`maxPriorityFeePerGas: ${value.maxPriorityFeePerGas ?? 'undefined'}, maxFeePerGas: ${
value.maxFeePerGas ?? 'undefined'
}`,
'maxPriorityFeePerGas or maxFeePerGas is lower than 0',
);
}
}
export class Eip1559GasPriceError extends InvalidValueError {
public code = ERR_TX_INVALID_FEE_MARKET_GAS_PRICE;
public constructor(value: unknown) {
super(value, "eip-1559 transactions don't support gasPrice");
}
}
export class UnsupportedFeeMarketError extends InvalidValueError {
public code = ERR_TX_INVALID_LEGACY_FEE_MARKET;
public constructor(value: {
maxPriorityFeePerGas: Numbers | undefined;
maxFeePerGas: Numbers | undefined;
}) {
super(
`maxPriorityFeePerGas: ${value.maxPriorityFeePerGas ?? 'undefined'}, maxFeePerGas: ${
value.maxFeePerGas ?? 'undefined'
}`,
"pre-eip-1559 transaction don't support maxFeePerGas/maxPriorityFeePerGas",
);
}
}
export class InvalidTransactionObjectError extends InvalidValueError {
public code = ERR_TX_INVALID_OBJECT;
public constructor(value: unknown) {
super(value, 'invalid transaction object');
}
}
export class InvalidNonceOrChainIdError extends InvalidValueError {
public code = ERR_TX_INVALID_NONCE_OR_CHAIN_ID;
public constructor(value: { nonce: Numbers | undefined; chainId: Numbers | undefined }) {
super(
`nonce: ${value.nonce ?? 'undefined'}, chainId: ${value.chainId ?? 'undefined'}`,
'Nonce or chainId is lower than 0',
);
}
}
export class UnableToPopulateNonceError extends InvalidValueError {
public code = ERR_TX_UNABLE_TO_POPULATE_NONCE;
public constructor() {
super('UnableToPopulateNonceError', 'unable to populate nonce, no from address available');
}
}
export class Eip1559NotSupportedError extends InvalidValueError {
public code = ERR_TX_UNSUPPORTED_EIP_1559;
public constructor() {
super('Eip1559NotSupportedError', "Network doesn't support eip-1559");
}
}
export class UnsupportedTransactionTypeError extends InvalidValueError {
public code = ERR_TX_UNSUPPORTED_TYPE;
public constructor(value: unknown) {
super(value, 'unsupported transaction type');
}
}
export class TransactionDataAndInputError extends InvalidValueError {
public code = ERR_TX_DATA_AND_INPUT;
public constructor(value: { data: HexString | undefined; input: HexString | undefined }) {
super(
`data: ${value.data ?? 'undefined'}, input: ${value.input ?? 'undefined'}`,
'You can\'t have "data" and "input" as properties of transactions at the same time, please use either "data" or "input" instead.',
);
}
}
export class TransactionSendTimeoutError extends BaseWeb3Error {
public code = ERR_TX_SEND_TIMEOUT;
public constructor(value: { numberOfSeconds: number; transactionHash?: Bytes }) {
super(
`The connected Ethereum Node did not respond within ${
value.numberOfSeconds
} seconds, please make sure your transaction was properly sent and you are connected to a healthy Node. Be aware that transaction might still be pending or mined!\n\tTransaction Hash: ${
value.transactionHash ? value.transactionHash.toString() : 'not available'
}`,
);
}
}
function transactionTimeoutHint(transactionHash?: Bytes) {
return `Please make sure your transaction was properly sent and there no pervious pending transaction for the same account. However, be aware that it might still be mined!\n\tTransaction Hash: ${
transactionHash ? transactionHash.toString() : 'not available'
}`;
}
export class TransactionPollingTimeoutError extends BaseWeb3Error {
public code = ERR_TX_POLLING_TIMEOUT;
public constructor(value: { numberOfSeconds: number; transactionHash: Bytes }) {
super(
`Transaction was not mined within ${
value.numberOfSeconds
} seconds. ${transactionTimeoutHint(value.transactionHash)}`,
);
}
}
export class TransactionBlockTimeoutError extends BaseWeb3Error {
public code = ERR_TX_BLOCK_TIMEOUT;
public constructor(value: {
starterBlockNumber: number;
numberOfBlocks: number;
transactionHash?: Bytes;
}) {
super(
`Transaction started at ${value.starterBlockNumber} but was not mined within ${
value.numberOfBlocks
} blocks. ${transactionTimeoutHint(value.transactionHash)}`,
);
}
}
export class TransactionMissingReceiptOrBlockHashError extends InvalidValueError {
public code = ERR_TX_RECEIPT_MISSING_OR_BLOCKHASH_NULL;
public constructor(value: {
receipt: TransactionReceipt;
blockHash: Bytes;
transactionHash: Bytes;
}) {
super(
`receipt: ${JSON.stringify(
value.receipt,
)}, blockHash: ${value.blockHash?.toString()}, transactionHash: ${value.transactionHash?.toString()}`,
`Receipt missing or blockHash null`,
);
}
}
export class TransactionReceiptMissingBlockNumberError extends InvalidValueError {
public code = ERR_TX_RECEIPT_MISSING_BLOCK_NUMBER;
public constructor(value: { receipt: TransactionReceipt }) {
super(`receipt: ${JSON.stringify(value.receipt)}`, `Receipt missing block number`);
}
}
export class TransactionSigningError extends BaseWeb3Error {
public code = ERR_TX_SIGNING;
public constructor(errorDetails: string) {
super(`Invalid signature. "${errorDetails}"`);
}
}
export class LocalWalletNotAvailableError extends InvalidValueError {
public code = ERR_TX_LOCAL_WALLET_NOT_AVAILABLE;
public constructor() {
super(
'LocalWalletNotAvailableError',
`Attempted to index account in local wallet, but no wallet is available`,
);
}
}
export class InvalidPropertiesForTransactionTypeError extends BaseWeb3Error {
public code = ERR_TX_INVALID_PROPERTIES_FOR_TYPE;
public constructor(
validationError: Web3ValidationErrorObject[],
txType: '0x0' | '0x1' | '0x2',
) {
const invalidPropertyNames: string[] = [];
validationError.forEach(error =>
invalidPropertyNames.push(
// These errors are erroneously reported, error
// has type Web3ValidationErrorObject, but eslint doesn't recognize it
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call
(error.keyword.match(/data.(.+)/) as string[])[1],
),
);
super(
`The following properties are invalid for the transaction type ${txType}: ${invalidPropertyNames.join(
', ',
)}`,
);
}
}