From 2c94c6f8e848a6aa7dac6b38a95953f27d752e3d Mon Sep 17 00:00:00 2001 From: Justin Starry Date: Wed, 31 Mar 2021 15:15:04 +0800 Subject: [PATCH] fix: handle empty rpc batch requests properly (#16254) --- web3.js/src/connection.ts | 3 +++ web3.js/test/connection.test.ts | 10 +++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/web3.js/src/connection.ts b/web3.js/src/connection.ts index 8419d3f53..f8de37cb2 100644 --- a/web3.js/src/connection.ts +++ b/web3.js/src/connection.ts @@ -722,6 +722,9 @@ function createRpcRequest(client: RpcClient): RpcRequest { function createRpcBatchRequest(client: RpcClient): RpcBatchRequest { return (requests: RpcParams[]) => { return new Promise((resolve, reject) => { + // Do nothing if requests is empty + if (requests.length === 0) resolve([]); + const batch = requests.map((params: RpcParams) => { return client.request(params.methodName, params.args); }); diff --git a/web3.js/test/connection.test.ts b/web3.js/test/connection.test.ts index 2ba430aa9..437fea11d 100644 --- a/web3.js/test/connection.test.ts +++ b/web3.js/test/connection.test.ts @@ -894,7 +894,7 @@ describe('Connection', () => { ], }); - const result = await connection.getParsedConfirmedTransactions([ + let result = await connection.getParsedConfirmedTransactions([ confirmedTransaction, confirmedTransaction, ]); @@ -913,6 +913,14 @@ describe('Connection', () => { if (result[1] !== null) { expect(result[1].transaction.signatures).not.to.be.null; } + + result = await connection.getParsedConfirmedTransactions([]); + if (!result) { + expect(result).to.be.ok; + return; + } + + expect(result).to.be.empty; }); it('get confirmed transaction', async () => {