-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
sendTransaction
and sendSignedTransaction
Error Refactors
#5854
Changes from all commits
43d93de
db9cb50
8eb82ba
e4a2891
36c9b0d
2378e6a
044fea2
3a12870
63554f2
8c33881
853d0b2
bd4ff70
75a99f2
d075a86
2b59dc6
7e830d2
91a4e6f
ff142e0
8948973
c39aab2
3cb0a27
c951bb7
b2c21b2
af78858
77d6fa0
e5c59af
de1fe5e
f334a9e
71e8f1b
de2f1cf
4205577
a66aaf0
284f4ad
19aadcc
5376bc8
662a390
81be9d8
0f655a5
a1d1a56
d7ce12f
dc03c22
32add6d
66b7377
b998f8f
35293d6
68efd93
d6fc658
3d3e7bc
64a3e1f
fedc918
c54d2b9
683515c
547f9be
9e3c637
f4f43f8
d72206c
2b816bf
c4180b6
306b650
76ee3da
bcffc9f
2828b41
5838adf
e79e59f
a2175ff
5cbb9bf
e267663
2662eaf
c216751
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,7 @@ import { | |
ERR_TX_UNABLE_TO_POPULATE_NONCE, | ||
ERR_TX_UNSUPPORTED_EIP_1559, | ||
ERR_TX_UNSUPPORTED_TYPE, | ||
ERR_TX_REVERT_TRANSACTION_CUSTOM_ERROR, | ||
} from '../error_codes'; | ||
import { InvalidValueError, BaseWeb3Error } from '../web3_error_base'; | ||
|
||
|
@@ -84,16 +85,21 @@ export class RevertInstructionError extends BaseWeb3Error { | |
} | ||
} | ||
|
||
export class TransactionRevertError extends BaseWeb3Error { | ||
export class TransactionRevertInstructionError< | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renaming to keep parity with 1.x |
||
ReceiptType = TransactionReceipt, | ||
> extends BaseWeb3Error { | ||
public code = ERR_TX_REVERT_TRANSACTION; | ||
|
||
public constructor( | ||
public reason: string, | ||
public signature?: string, | ||
public receipt?: TransactionReceipt, | ||
public receipt?: ReceiptType, | ||
public data?: string, | ||
) { | ||
super( | ||
`Transaction has been reverted by the EVM:\n ${JSON.stringify(receipt, undefined, 2)}`, | ||
`Transaction has been reverted by the EVM${ | ||
receipt === undefined ? '' : `:\n ${BaseWeb3Error.convertToString(receipt)}` | ||
}`, | ||
); | ||
} | ||
|
||
|
@@ -103,6 +109,43 @@ export class TransactionRevertError extends BaseWeb3Error { | |
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, | ||
}; | ||
} | ||
} | ||
|
@@ -125,10 +168,14 @@ export class ContractCodeNotStoredError extends TransactionError { | |
} | ||
} | ||
|
||
export class TransactionRevertedWithoutReasonError extends TransactionError { | ||
public constructor(receipt: TransactionReceipt) { | ||
export class TransactionRevertedWithoutReasonError< | ||
ReceiptType = TransactionReceipt, | ||
> extends TransactionError<ReceiptType> { | ||
public constructor(receipt?: ReceiptType) { | ||
super( | ||
`Transaction has been reverted by the EVM:\n ${JSON.stringify(receipt, undefined, 2)}`, | ||
`Transaction has been reverted by the EVM${ | ||
receipt === undefined ? '' : `:\n ${BaseWeb3Error.convertToString(receipt)}` | ||
}`, | ||
receipt, | ||
); | ||
this.code = ERR_TX_REVERT_WITHOUT_REASON; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1050,10 +1050,13 @@ export class Contract<Abi extends ContractAbi> | |
options, | ||
contractOptions: modifiedContractOptions, | ||
}); | ||
const transactionToSend = sendTransaction(this, tx, DEFAULT_RETURN_FORMAT); | ||
const transactionToSend = sendTransaction(this, tx, DEFAULT_RETURN_FORMAT, { | ||
// TODO Should make this configurable by the user | ||
checkRevertBeforeSending: false, | ||
}); | ||
|
||
// eslint-disable-next-line no-void | ||
void transactionToSend.on('contractExecutionError', (error: unknown) => { | ||
void transactionToSend.on('error', (error: unknown) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed |
||
if (error instanceof ContractExecutionError) { | ||
// this will parse the error data by trying to decode the ABI error inputs according to EIP-838 | ||
decodeContractErrorData(errorsAbi, error.innerError); | ||
|
@@ -1094,6 +1097,8 @@ export class Contract<Abi extends ContractAbi> | |
newContract.options.address = receipt.contractAddress; | ||
return newContract; | ||
}, | ||
// TODO Should make this configurable by the user | ||
checkRevertBeforeSending: false, | ||
}); | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added request to the error object as I wanted to add sent transaction details to failed transaction errors to give the user more context (e.g. the sending account when the error
Unknown account
orInsufficient funds
is thrown) on what exactly was sent to provider