Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit f8a2533

Browse files
author
Alex
authored
6164 batch request fix (#6166)
* allow batch size of 1 * add batch test and update changelog
1 parent af57eae commit f8a2533

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

packages/web3-core/CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,7 @@ Documentation:
120120
[Migration Guide from 1.x](https://docs.web3js.org/guides/web3_upgrade_guide/x/)
121121

122122
## [Unreleased]
123+
124+
### Fixed
125+
126+
- Fixed Batch requests erroring out on one request (#6164)

packages/web3-eth/test/integration/batch.test.ts

+20
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,26 @@ describe('eth', () => {
4141
});
4242

4343
describe('methods', () => {
44+
it('executes one batch request', async () => {
45+
const acc1 = await createTempAccount();
46+
47+
const batch = new web3Eth.BatchRequest();
48+
const request1 = {
49+
id: 10,
50+
method: 'eth_getBalance',
51+
params: [acc1.address, 'latest'],
52+
};
53+
const r1 = batch.add(request1).catch(console.error);
54+
const [response1] = await batch.execute();
55+
56+
// eslint-disable-next-line jest/no-standalone-expect
57+
expect(response1.result).toBeDefined();
58+
// TODO: in future release add test for validation of returned results , ( match balance )
59+
// eslint-disable-next-line jest/no-standalone-expect
60+
expect(Number(hexToNumber(String(response1.result)))).toBeGreaterThan(0);
61+
const [res1] = await Promise.all([r1]);
62+
expect(res1).toBe(response1.result);
63+
});
4464
it('BatchRequest', async () => {
4565
const acc1 = await createTempAccount();
4666
const acc2 = await createTempAccount();

packages/web3-utils/src/json_rpc.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ export const isValidResponse = <Result = unknown, Error = unknown>(
9191
export const isBatchResponse = <Result = unknown, Error = unknown>(
9292
response: JsonRpcResponse<Result, Error>,
9393
): response is JsonRpcBatchResponse<Result, Error> =>
94-
Array.isArray(response) && response.length > 1 && isValidResponse(response);
94+
Array.isArray(response) && response.length > 0 && isValidResponse(response);
9595

9696
// internal optional variable to increment and use for the jsonrpc `id`
9797
let requestIdSeed: number | undefined;
@@ -127,4 +127,4 @@ export const toBatchPayload = (requests: JsonRpcOptionalRequest<unknown>[]): Jso
127127

128128
export const isBatchRequest = (
129129
request: JsonRpcBatchRequest | JsonRpcRequest<unknown> | JsonRpcOptionalRequest<unknown>,
130-
): request is JsonRpcBatchRequest => Array.isArray(request) && request.length > 1;
130+
): request is JsonRpcBatchRequest => Array.isArray(request) && request.length > 0;

0 commit comments

Comments
 (0)