Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
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
24 changes: 19 additions & 5 deletions packages/web3-core/src/web3_request_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,14 +241,14 @@ export class Web3RequestManager<
payload,
error as JsonRpcResponse<ResponseType, unknown>,
{ legacy: true, error: true },
),
)
);
}

// TODO: This could be deprecated and removed.
if (isLegacyRequestProvider(provider)) {
return new Promise<JsonRpcResponse<ResponseType>>((resolve, reject) => {
const rejectWithError = (err: unknown) =>
const rejectWithError = (err: unknown) =>{
reject(
this._processJsonRpcResponse(
payload,
Expand All @@ -257,8 +257,9 @@ export class Web3RequestManager<
legacy: true,
error: true,
},
),
);
))
}

const resolveWithResponse = (response: JsonRpcResponse<ResponseType>) =>
resolve(
this._processJsonRpcResponse(payload, response, {
Expand Down Expand Up @@ -288,7 +289,20 @@ export class Web3RequestManager<
const responsePromise = result as unknown as Promise<
JsonRpcResponse<ResponseType>
>;
responsePromise.then(resolveWithResponse).catch(rejectWithError);
responsePromise.then(resolveWithResponse).catch(error => {
try {
// Attempt to process the error response
const processedError = this._processJsonRpcResponse(
payload,
error as JsonRpcResponse<ResponseType, unknown>,
{ legacy: true, error: true }
);
reject(processedError);
Copy link
Contributor

Choose a reason for hiding this comment

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

According to codevov, this line is not checked at unit testing.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

covered

} catch (processingError) {
// Catch any errors that occur during the error processing
reject(processingError);
}
});
}
});
}
Expand Down
31 changes: 31 additions & 0 deletions packages/web3-core/test/unit/web3_request_manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,21 @@ describe('Web3RequestManager', () => {
expect(myProvider.request).toHaveBeenCalledTimes(1);
expect(await pr).toBe('test');
});

it('promise of legacy provider should be error', async () => {
const manager = new Web3RequestManager(undefined, undefined);
const pr = new Promise(resolve => {
resolve('test');
});
const myProvider = {
request: jest.fn().mockImplementation(async () => pr),
} as any;
manager.setProvider(myProvider);
await manager.send(request);
expect(myProvider.request).toHaveBeenCalledTimes(1);
expect(await pr).toBe('test');
});

it('Got a "nullish" response from provider', async () => {
const manager = new Web3RequestManager(undefined, undefined);
const myProvider = {
Expand Down Expand Up @@ -1134,6 +1149,22 @@ describe('Web3RequestManager', () => {
expect(myProvider.request).toHaveBeenCalledWith(payload, expect.any(Function));
});

it('should error in isPromise', async () => {

const manager = new Web3RequestManager();
const myProvider = {
request: jest
.fn()
.mockImplementation((async () => Promise.reject(errorResponse))),
} as any;

jest.spyOn(manager, 'provider', 'get').mockReturnValue(myProvider);

await expect(manager.sendBatch(request)).rejects.toEqual(errorResponse);
expect(myProvider.request).toHaveBeenCalledTimes(1);
expect(myProvider.request).toHaveBeenCalledWith(payload, expect.any(Function));
});

it('should pass request to provider and reject if provider returns error', async () => {
const manager = new Web3RequestManager();
const myProvider = {
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-eth/src/rpc_method_wrappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ export function sendTransaction<
transaction,
transactionFormatted,
});

await sendTxHelper.checkRevertBeforeSending(
transactionFormatted as TransactionCall,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -720,4 +720,4 @@ describe('Web3Eth.sendTransaction', () => {
).rejects.toMatchObject(expectedThrownError);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,16 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
// eslint-disable-next-line import/no-extraneous-dependencies
import hardhat from 'hardhat';

import { performBasicRpcCalls } from './helper';
import { performBasicRpcCalls, failErrorCalls} from './helper';

describe('compatibility with `hardhat` provider', () => {
it('should initialize Web3, get accounts & block number and send a transaction', async () => {
// use the hardhat provider for web3.js
await performBasicRpcCalls(hardhat.network.provider);
await expect(performBasicRpcCalls(hardhat.network.provider)).resolves.not.toThrow();
});
it('should throw on error calls', async () => {
const result = failErrorCalls(hardhat.network.provider);
await expect(result).rejects.toThrow();

})
});
30 changes: 29 additions & 1 deletion packages/web3/test/integration/external-providers/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

import { SupportedProviders } from 'web3-types';
import { Contract } from 'web3-eth-contract';
import Web3 from '../../../src/index';
import { BasicAbi, BasicBytecode } from '../../shared_fixtures/build/Basic';

/**
* Performs basic RPC calls (like `eth_accounts`, `eth_blockNumber` and `eth_sendTransaction`)
Expand Down Expand Up @@ -44,5 +46,31 @@ export async function performBasicRpcCalls(provider: SupportedProviders) {
expect(typeof blockNumber1).toBe('bigint');

// After sending a transaction, the blocknumber is supposed to be greater than or equal the block number before sending the transaction
expect(blockNumber1).toBeGreaterThanOrEqual(blockNumber0);
expect(blockNumber1).toBeGreaterThanOrEqual(blockNumber0);
}

export async function failErrorCalls(provider: SupportedProviders) {
let contract: Contract<typeof BasicAbi>;
const web3 = new Web3(provider);

contract = new web3.eth.Contract(BasicAbi, undefined, {
provider,
});

let deployOptions: Record<string, unknown>;;

// eslint-disable-next-line prefer-const
deployOptions = {
data: BasicBytecode,
arguments: [10, 'string init value'],
};
const accounts = await web3.eth.getAccounts();

const sendOptions = { from: accounts[0], gas: '1000000' };


contract = await contract.deploy(deployOptions).send(sendOptions);

await contract.methods.reverts().send({ from: accounts[0] })

}