Skip to content
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

Merged
merged 69 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
43d93de
Add payload as optional argument for InvalidResponseError
spacesailor24 Feb 22, 2023
db9cb50
Init Transaction Error Scenarios tests
spacesailor24 Feb 22, 2023
8eb82ba
Init ERR_TX_REVERT_TRANSACTION_CUSTOM_ERROR
spacesailor24 Feb 22, 2023
e4a2891
WIP
spacesailor24 Feb 22, 2023
36c9b0d
WIP
spacesailor24 Feb 23, 2023
2378e6a
Update TransactionRevertedWithoutReasonError to handle receipts objec…
spacesailor24 Feb 24, 2023
044fea2
Update sendTransaction revert tests
spacesailor24 Feb 24, 2023
3a12870
Add contractAbi argument to getTransactionError
spacesailor24 Feb 24, 2023
63554f2
sendTransation emits error for ContractExecutionError
spacesailor24 Feb 24, 2023
8c33881
Add ContractExecutionError to SendTransactionEvents. Add contractAbi …
spacesailor24 Feb 24, 2023
853d0b2
Replace use of contractExecutionError sendTransaction event with error
spacesailor24 Feb 24, 2023
bd4ff70
Update contract_methods reverts test
spacesailor24 Feb 24, 2023
75a99f2
Merge branch '4.x' into wyatt/4.x/5840-requstmanger-return-errors
spacesailor24 Feb 24, 2023
d075a86
Update web3-errors test
spacesailor24 Feb 24, 2023
2b59dc6
Update expected error mesage for contract_deploy_test
spacesailor24 Feb 24, 2023
7e830d2
Add expectedErrorObject based on test backend
spacesailor24 Feb 24, 2023
91a4e6f
Correct web3-errors test snapshots
spacesailor24 Feb 24, 2023
ff142e0
fix (#5869)
avkos Feb 24, 2023
8948973
5747 codecov (#5843)
jdevcs Feb 28, 2023
c39aab2
ens system tests (#5849)
nikoulai Feb 28, 2023
3cb0a27
Disconnect function doesn't exists in types of currentProvider (#5865)
avkos Feb 28, 2023
c951bb7
Nikos/fix ens event test (#5880)
nikoulai Mar 1, 2023
b2c21b2
update event emitter geth ipc tests (#5874)
Mar 1, 2023
af78858
4.x issue stale conf (#5878)
jdevcs Mar 2, 2023
77d6fa0
Add documentation for catching provider events (#5886)
Muhammad-Altabba Mar 6, 2023
e5c59af
Fixing tests for WS and IPC
spacesailor24 Feb 24, 2023
de1fe5e
Remove jest specific test code that doesn't work in Cypress
spacesailor24 Feb 24, 2023
f334a9e
Remove jest specific test code that doesn't work in Cypress
spacesailor24 Feb 24, 2023
71e8f1b
Remove jest specific test code
spacesailor24 Feb 24, 2023
de2f1cf
Add toLowerCase for address
spacesailor24 Feb 24, 2023
4205577
Rename TransactionRevertError to TransactionRevertInstructionError
spacesailor24 Feb 25, 2023
a66aaf0
Add beforeAll for Transaction Error Scenario tests
spacesailor24 Feb 25, 2023
284f4ad
Add Transaction Error Scenario tests for call
spacesailor24 Feb 25, 2023
19aadcc
Add support for handleRevert in sendTransaction for geth
spacesailor24 Feb 25, 2023
5376bc8
Update tests and add test to verify event error is same as thrown error
spacesailor24 Feb 25, 2023
662a390
Correct test expected data
spacesailor24 Feb 25, 2023
81be9d8
Remove message from expectedThrownError for sendTransaction tests
spacesailor24 Feb 25, 2023
0f655a5
Add error refactors to sendSignedTransaction and init tests
spacesailor24 Feb 25, 2023
a1d1a56
Remove call Transaction Error Scenario tests
spacesailor24 Feb 25, 2023
d7ce12f
Fix type error
spacesailor24 Feb 25, 2023
dc03c22
Fix nonce issue in tests
spacesailor24 Feb 25, 2023
32add6d
Fix error parsing bug for web3Eth.sendSignedTransaction
spacesailor24 Mar 1, 2023
66b7377
Update expected error for send_signed_transaction error tests
spacesailor24 Mar 1, 2023
b998f8f
Revert ResponseError change to maintain 1.x parity
spacesailor24 Mar 2, 2023
35293d6
Add web3-errors test for TransactionRevertWithCustomError
spacesailor24 Mar 2, 2023
68efd93
Update expected error message for RespnoseError in web3-eth-contract …
spacesailor24 Mar 2, 2023
d6fc658
Add example to dev comment for sendSignedTransaction
spacesailor24 Mar 2, 2023
3d3e7bc
Removed implemented TODO in packages/web3-eth/src/utils/get_transacti…
spacesailor24 Mar 2, 2023
64a3e1f
Update CHANGELOGs
spacesailor24 Mar 2, 2023
fedc918
Update CHANGELOG.md
spacesailor24 Mar 2, 2023
c54d2b9
Init get_transaction_error tests
spacesailor24 Mar 2, 2023
683515c
Add mock to getRevertReason for get_transaction_error tests
spacesailor24 Mar 2, 2023
547f9be
Init getRevertReason tests and parseTransactionError tests
spacesailor24 Mar 2, 2023
9e3c637
Add check for revert before sending tx
spacesailor24 Mar 3, 2023
f4f43f8
Update web3-plugin-example test to account for getRevertReason check
spacesailor24 Mar 7, 2023
d72206c
Turn off checkRevertBeforeSending for sendFewTxes helper
spacesailor24 Mar 7, 2023
2b816bf
Fix test data for send_signed_transaction tests
spacesailor24 Mar 7, 2023
c4180b6
Fix bug in get_transaction_error
spacesailor24 Mar 7, 2023
306b650
Fix send_transaction error test
spacesailor24 Mar 7, 2023
76ee3da
Add checkRevertBeforeSending: false to sendTransaction calls in Contr…
spacesailor24 Mar 7, 2023
bcffc9f
Add checkRevertBeforeSending: false for send tx test helpers
spacesailor24 Mar 7, 2023
2828b41
Remove getRevertReason mock
spacesailor24 Mar 8, 2023
5838adf
A fix at `IpcProvider` and few changes at `SocketProvider` and its de…
Muhammad-Altabba Mar 7, 2023
e79e59f
Fix failed tx test for ganache
spacesailor24 Mar 8, 2023
a2175ff
Update expected transaction errors for ganache
spacesailor24 Mar 9, 2023
5cbb9bf
Fix method handler crashed (#5905)
avkos Mar 8, 2023
e267663
Test fix
spacesailor24 Mar 9, 2023
2662eaf
Use `v1` instead of Short Sha for cloudflare/pages-action (#5909)
Muhammad-Altabba Mar 9, 2023
c216751
Merge branch '4.x' into wyatt/4.x/5840-requstmanger-return-errors
spacesailor24 Mar 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1155,11 +1155,15 @@ should use 4.0.1-alpha.0 for testing.
#### web3-errors

- Added error class `InvalidMethodParamsError` and error code `ERR_INVALID_METHOD_PARAMS = 207` (#5824)
- `request` property to `ResponseError` (#5854)
- `data` property to `TransactionRevertInstructionError` (#5854)
- `TransactionRevertWithCustomError` was added to handle custom solidity errors (#5854)

#### web3-eth

- Added `createAccessList` functionality ( #5780 )
- Added support of `safe` and `finalized` block tags (#5823)
- `contractAbi` option to `SendTransactionOptions` and `SendSignedTransactionOptions` to added the ability to parse custom solidity errors (#5854)

#### web3-eth-abi

Expand Down Expand Up @@ -1211,10 +1215,12 @@ should use 4.0.1-alpha.0 for testing.
#### web3-errors

- The abstract class `Web3Error` is renamed to `BaseWeb3Error` (#5771)
- Renamed `TransactionRevertError` to `TransactionRevertInstructionError` to remain consistent with `1.x` (#5854)

#### web3-eth

- Update imports statements for objects that was moved between web3 packages (#5771)
- `sendTransaction` and `sendSignedTransaction` now errors with (and `error` event emits) the following possible errors: `TransactionRevertedWithoutReasonError`, `TransactionRevertInstructionError`, `TransactionRevertWithCustomError`, `InvalidResponseError`, or `ContractExecutionError` (#5854)

#### web3-eth-contract

Expand Down Expand Up @@ -1257,3 +1263,4 @@ should use 4.0.1-alpha.0 for testing.
#### web3-eth-contract

- Fix contract defaults (#5756)
- Fixed getPastEventsError (#5819)
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,4 @@ provider.on('error', error => {
// the `maxAttempts` is equal to the provided value by the user, or the default value `5`.
}
});
```
```
2 changes: 1 addition & 1 deletion docs/docs/guides/web3_providers_guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,4 @@ provider.on('error', error => {
// the `maxAttempts` is equal to the provided value by the user, or the default value `5`.
}
});
```
```
2 changes: 1 addition & 1 deletion packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ export class Web3RequestManager<
throw new RpcError(rpcErrorResponse);
}
} else if (!Web3RequestManager._isReverted(response)) {
throw new InvalidResponseError<ErrorType>(response);
throw new InvalidResponseError<ErrorType, RequestType>(response, payload);
}
}

Expand Down
4 changes: 4 additions & 0 deletions packages/web3-errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed

- The abstract class `Web3Error` is renamed to `BaseWeb3Error` (#5771)
- Renamed TransactionRevertError to TransactionRevertInstructionError to remain consistent with 1.x
- Using `MaxAttemptsReachedOnReconnectingError` with the same message for 1.x but also adding the `maxAttempts` (#5894)

### Added

- Added error class `InvalidMethodParamsError` and error code `ERR_INVALID_METHOD_PARAMS = 207` (#5824)
- `request` property to `ResponseError` (#5854)
- `data` property to `TransactionRevertInstructionError` (#5854)
- `TransactionRevertWithCustomError` was added to handle custom solidity errors (#5854)
1 change: 1 addition & 0 deletions packages/web3-errors/src/error_codes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const ERR_TX_GAS_MISMATCH = 434;
export const ERR_TX_CHAIN_MISMATCH = 435;
export const ERR_TX_HARDFORK_MISMATCH = 436;
export const ERR_TX_INVALID_RECEIVER = 437;
export const ERR_TX_REVERT_TRANSACTION_CUSTOM_ERROR = 438;

// Connection error codes
export const ERR_CONN = 500;
Expand Down
32 changes: 25 additions & 7 deletions packages/web3-errors/src/errors/response_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

// eslint-disable-next-line max-classes-per-file
import { JsonRpcError, JsonRpcResponse, JsonRpcResponseWithError } from 'web3-types';
import {
JsonRpcError,
JsonRpcPayload,
JsonRpcResponse,
JsonRpcResponseWithError,
} from 'web3-types';
import { BaseWeb3Error } from '../web3_error_base';
import { ERR_INVALID_RESPONSE, ERR_RESPONSE } from '../error_codes';

Expand All @@ -36,11 +41,16 @@ const isResponseWithError = <Error = unknown, Result = unknown>(
const buildErrorMessage = (response: JsonRpcResponse<unknown, unknown>): string =>
isResponseWithError(response) ? response.error.message : '';

export class ResponseError<ErrorType = unknown> extends BaseWeb3Error {
export class ResponseError<ErrorType = unknown, RequestType = unknown> extends BaseWeb3Error {
public code = ERR_RESPONSE;
public data?: ErrorType | ErrorType[];
public request?: JsonRpcPayload<RequestType>;

public constructor(response: JsonRpcResponse<unknown, ErrorType>, message?: string) {
public constructor(
response: JsonRpcResponse<unknown, ErrorType>,
message?: string,
request?: JsonRpcPayload<RequestType>,
) {
super(
message ??
`Returned error: ${
Expand All @@ -55,16 +65,24 @@ export class ResponseError<ErrorType = unknown> extends BaseWeb3Error {
? response.map(r => r.error?.data as ErrorType)
: response?.error?.data;
}

this.request = request;
Copy link
Contributor Author

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 or Insufficient funds is thrown) on what exactly was sent to provider

}

public toJSON() {
return { ...super.toJSON(), data: this.data };
return { ...super.toJSON(), data: this.data, request: this.request };
}
}

export class InvalidResponseError<ErrorType = unknown> extends ResponseError<ErrorType> {
public constructor(result: JsonRpcResponse<unknown, ErrorType>) {
super(result);
export class InvalidResponseError<ErrorType = unknown, RequestType = unknown> extends ResponseError<
ErrorType,
RequestType
> {
public constructor(
result: JsonRpcResponse<unknown, ErrorType>,
request?: JsonRpcPayload<RequestType>,
) {
super(result, undefined, request);
this.code = ERR_INVALID_RESPONSE;

let errorOrErrors: JsonRpcError | JsonRpcError[] | undefined;
Expand Down
59 changes: 53 additions & 6 deletions packages/web3-errors/src/errors/transaction_errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -84,16 +85,21 @@ export class RevertInstructionError extends BaseWeb3Error {
}
}

export class TransactionRevertError extends BaseWeb3Error {
export class TransactionRevertInstructionError<
Copy link
Contributor Author

Choose a reason for hiding this comment

The 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)}`
}`,
);
}

Expand All @@ -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,
};
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ Object {
},
"message": "Returned error: error message",
"name": "InvalidResponseError",
"request": undefined,
}
`;

Expand Down Expand Up @@ -229,6 +230,7 @@ Object {
"innerError": undefined,
"message": "Returned error: error message",
"name": "ResponseError",
"request": undefined,
}
`;

Expand All @@ -239,6 +241,7 @@ Object {
"innerError": undefined,
"message": "Returned error: error message",
"name": "ResponseError",
"request": undefined,
}
`;

Expand Down Expand Up @@ -280,15 +283,14 @@ Object {
}
`;

exports[`errors TransactionRevertError should have valid json structure 1`] = `
exports[`errors TransactionRevertInstructionError should have valid json structure 1`] = `
Object {
"code": 402,
"data": undefined,
"innerError": undefined,
"message": "Transaction has been reverted by the EVM:
{
\\"attr1\\": \\"attr1\\"
}",
"name": "TransactionRevertError",
{\\"attr1\\":\\"attr1\\"}",
"name": "TransactionRevertInstructionError",
"reason": "message",
"receipt": Object {
"attr1": "attr1",
Expand All @@ -297,14 +299,32 @@ Object {
}
`;

exports[`errors TransactionRevertWithCustomError should have valid json structure 1`] = `
Object {
"code": 438,
"customErrorArguments": Object {
"customErrorArgument": "customErrorArgument",
},
"customErrorDecodedSignature": "customErrorDecodedSignature",
"customErrorName": "customErrorName",
"data": "data",
"innerError": undefined,
"message": "Transaction has been reverted by the EVM",
"name": "TransactionRevertWithCustomError",
"reason": "reason",
"receipt": Object {
"attr1": "attr1",
},
"signature": "signature",
}
`;

exports[`errors TransactionRevertedWithoutReasonError should have valid json structure 1`] = `
Object {
"code": 405,
"innerError": undefined,
"message": "Transaction has been reverted by the EVM:
{
\\"attr1\\": \\"attr1\\"
}",
{\\"attr1\\":\\"attr1\\"}",
"name": "TransactionRevertedWithoutReasonError",
"receipt": Object {
"attr1": "attr1",
Expand Down
24 changes: 21 additions & 3 deletions packages/web3-errors/test/unit/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe('errors', () => {
// To disable error for the abstract class

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
const err = new ErrorClass({} as never, {} as never, {} as never);
const err = new ErrorClass({} as never, {} as never, {} as never, {} as never);
errorCodes.push(err.code);
}

Expand Down Expand Up @@ -162,16 +162,34 @@ describe('errors', () => {
});
});

describe('TransactionRevertError', () => {
describe('TransactionRevertInstructionError', () => {
it('should have valid json structure', () => {
expect(
new transactionErrors.TransactionRevertError('message', 'signature', {
new transactionErrors.TransactionRevertInstructionError('message', 'signature', {
attr1: 'attr1',
} as any).toJSON(),
).toMatchSnapshot();
});
});

describe('TransactionRevertWithCustomError', () => {
it('should have valid json structure', () => {
expect(
new transactionErrors.TransactionRevertWithCustomError(
'reason',
'customErrorName',
'customErrorDecodedSignature',
{ customErrorArgument: 'customErrorArgument' },
'signature',
{
attr1: 'attr1',
} as any,
'data',
).toJSON(),
).toMatchSnapshot();
});
});

describe('NoContractAddressFoundError', () => {
it('should have valid json structure', () => {
expect(
Expand Down
9 changes: 7 additions & 2 deletions packages/web3-eth-contract/src/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Copy link
Contributor Author

@spacesailor24 spacesailor24 Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed contractExecutionError event emission from sendTransaction and instead used the existing error event that emits an instance of ContractExecutionError

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);
Expand Down Expand Up @@ -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,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ describe('contract defaults (extra)', () => {
}),
expect.any(Object),
expect.any(Object),
expect.any(Object),
);
});

Expand Down
Loading