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

Errors not caught in 4.x #6623

Merged
merged 12 commits into from
Dec 4, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ describe('contract', () => {
};
});

it('should deploy the contract', async () => {
it.skip('should deploy the contract', async () => {
avkos marked this conversation as resolved.
Show resolved Hide resolved
const acc = await createTempAccount();
const sendOptionsLocal = { from: acc.address, gas: '10000000' };
await expect(
Expand Down Expand Up @@ -120,6 +120,66 @@ describe('contract', () => {
value,
);
});

it('send tokens from the account that does not have ether', async () => {
const tempAccount = await createTempAccount();
const test = await createNewAccount({
unlock: true,
refill: false,
});

let catchError = false;
let catchErrorPromise;
try {
const promiEvent = contractDeployed.methods
.transfer(tempAccount.address, '0x1')
.send({ ...sendOptions, from: test.address });

catchErrorPromise = new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promiEvent.on('error', err => {
// Returned error: insufficient funds for gas * price + value: balance 0, tx cost 25000327300000000, overshot 25000327300000000
resolve(err);
});
});
await promiEvent;
} catch (e) {
// Returned error: insufficient funds for gas * price + value: balance 0, tx cost 25000327300000000, overshot 25000327300000000
catchError = true;
}
expect(await catchErrorPromise).toBeDefined();
expect(catchError).toBe(true);
});
it('send tokens from the account that does not have tokens', async () => {
const tempAccount = await createTempAccount();
const test = await createNewAccount({
unlock: true,
refill: true,
});

let catchError = false;
let catchErrorPromise;
try {
const promiEvent = contractDeployed.methods
.transfer(tempAccount.address, '0x1')
.send({ ...sendOptions, from: test.address });

catchErrorPromise = new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promiEvent.on('error', err => {
// Transaction has been reverted by the EVM
resolve(err);
});
});
await promiEvent;
} catch (e) {
// Transaction has been reverted by the EVM
catchError = true;
}
expect(await catchErrorPromise).toBeDefined();
expect(catchError).toBe(true);
});

it.each([signAndSendContractMethodEIP1559, signAndSendContractMethodEIP2930])(
'should transfer tokens with local wallet %p',
async signAndSendContractMethod => {
Expand Down
2 changes: 2 additions & 0 deletions packages/web3-eth/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
TransactionRevertInstructionError,
TransactionRevertWithCustomError,
InvalidResponseError,
TransactionPollingTimeoutError,
} from 'web3-errors';
import {
FormatType,
Expand Down Expand Up @@ -50,6 +51,7 @@ export type SendTransactionEventsBase<ReturnFormat extends DataFormat, TxType> =
| TransactionRevertedWithoutReasonError<FormatType<TransactionReceipt, ReturnFormat>>
| TransactionRevertInstructionError<FormatType<TransactionReceipt, ReturnFormat>>
| TransactionRevertWithCustomError<FormatType<TransactionReceipt, ReturnFormat>>
| TransactionPollingTimeoutError
| InvalidResponseError
| ContractExecutionError;
};
Expand Down
4 changes: 3 additions & 1 deletion packages/web3-eth/src/utils/send_tx_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import {
ContractExecutionError,
InvalidResponseError,
TransactionPollingTimeoutError,
TransactionRevertedWithoutReasonError,
TransactionRevertInstructionError,
TransactionRevertWithCustomError,
Expand Down Expand Up @@ -153,7 +154,7 @@
!this.options?.ignoreGasPricing &&
isNullish((transactionFormatted as Transaction).gasPrice) &&
(isNullish((transaction as Transaction).maxPriorityFeePerGas) ||
isNullish((transaction as Transaction).maxFeePerGas))

Check warning on line 157 in packages/web3-eth/src/utils/send_tx_helper.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth/src/utils/send_tx_helper.ts#L157

Added line #L157 was not covered by tests
) {
result = {
...transactionFormatted,
Expand Down Expand Up @@ -182,8 +183,8 @@

return trySendTransaction(
this.web3Context,
async (): Promise<string> =>
ethRpcMethods.sendRawTransaction(

Check warning on line 187 in packages/web3-eth/src/utils/send_tx_helper.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth/src/utils/send_tx_helper.ts#L186-L187

Added lines #L186 - L187 were not covered by tests
this.web3Context.requestManager,
signedTransaction.rawTransaction,
),
Expand Down Expand Up @@ -241,9 +242,10 @@
if (
(_error instanceof InvalidResponseError ||
_error instanceof ContractExecutionError ||
_error instanceof TransactionRevertWithCustomError ||
_error instanceof TransactionRevertedWithoutReasonError ||
_error instanceof TransactionRevertInstructionError) &&
_error instanceof TransactionRevertInstructionError ||
_error instanceof TransactionPollingTimeoutError) &&

Check warning on line 248 in packages/web3-eth/src/utils/send_tx_helper.ts

View check run for this annotation

Codecov / codecov/patch

packages/web3-eth/src/utils/send_tx_helper.ts#L245-L248

Added lines #L245 - L248 were not covered by tests
this.promiEvent.listenerCount('error') > 0
) {
this.promiEvent.emit('error', _error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { isHexStrict } from 'web3-validator';
import { Web3Eth, InternalTransaction, transactionSchema } from '../../../src';
import {
closeOpenConnection,
createNewAccount,
createTempAccount,
getSystemTestBackend,
getSystemTestProvider,
Expand All @@ -55,6 +56,74 @@ describe('Web3Eth.sendSignedTransaction', () => {
await closeOpenConnection(web3Eth);
});

describe('Should catch errors', () => {
it('send ether from the account that does not have ether', async () => {
let onErrorReceived = false;
let catchErrorReceived = false;
const from = await createNewAccount({
unlock: true,
refill: false,
});
let pr;
try {
const promiEvent = web3Eth.sendTransaction({
to: tempAcc.address,
from: from.address,
});
pr = new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promiEvent.on('error', () => {
onErrorReceived = true;
resolve(true);
});
});
await promiEvent;
} catch (e) {
catchErrorReceived = true;
}
await pr;
expect(onErrorReceived).toBe(true);
expect(catchErrorReceived).toBe(true);
});
it('send and wait timeout', async () => {
let onErrorReceived = false;
let catchErrorReceived = false;
const from = await createNewAccount({
unlock: true,
refill: true,
});

web3Eth.setConfig({
transactionReceiptPollingInterval: 10,
transactionPollingTimeout: 1000,
});

const currentNonce = await web3Eth.getTransactionCount(from.address);
let pr;
try {
const promiEvent = web3Eth.sendTransaction({
to: tempAcc.address,
from: from.address,
value: '0x1',
nonce: currentNonce + BigInt(1000),
});
pr = new Promise(resolve => {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
promiEvent.on('error', () => {
onErrorReceived = true;
resolve(true);
});
});
await promiEvent;
} catch (e) {
catchErrorReceived = true;
}
await pr;
expect(onErrorReceived).toBe(true);
expect(catchErrorReceived).toBe(true);
});
});

describe('Transaction Types', () => {
it('should send a signed simple value transfer - type 0x0', async () => {
const temp = await createTempAccount();
Expand Down
Loading