Skip to content

Commit

Permalink
6164 batch request fix (#6166)
Browse files Browse the repository at this point in the history
* allow batch size of 1

* add batch test and update changelog
  • Loading branch information
Alex committed Jun 10, 2023
1 parent af57eae commit f8a2533
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions packages/web3-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,7 @@ Documentation:
[Migration Guide from 1.x](https://docs.web3js.org/guides/web3_upgrade_guide/x/)

## [Unreleased]

### Fixed

- Fixed Batch requests erroring out on one request (#6164)
20 changes: 20 additions & 0 deletions packages/web3-eth/test/integration/batch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ describe('eth', () => {
});

describe('methods', () => {
it('executes one batch request', async () => {
const acc1 = await createTempAccount();

const batch = new web3Eth.BatchRequest();
const request1 = {
id: 10,
method: 'eth_getBalance',
params: [acc1.address, 'latest'],
};
const r1 = batch.add(request1).catch(console.error);
const [response1] = await batch.execute();

// eslint-disable-next-line jest/no-standalone-expect
expect(response1.result).toBeDefined();
// TODO: in future release add test for validation of returned results , ( match balance )
// eslint-disable-next-line jest/no-standalone-expect
expect(Number(hexToNumber(String(response1.result)))).toBeGreaterThan(0);
const [res1] = await Promise.all([r1]);
expect(res1).toBe(response1.result);
});
it('BatchRequest', async () => {
const acc1 = await createTempAccount();
const acc2 = await createTempAccount();
Expand Down
4 changes: 2 additions & 2 deletions packages/web3-utils/src/json_rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const isValidResponse = <Result = unknown, Error = unknown>(
export const isBatchResponse = <Result = unknown, Error = unknown>(
response: JsonRpcResponse<Result, Error>,
): response is JsonRpcBatchResponse<Result, Error> =>
Array.isArray(response) && response.length > 1 && isValidResponse(response);
Array.isArray(response) && response.length > 0 && isValidResponse(response);

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

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

0 comments on commit f8a2533

Please sign in to comment.