From da10ed52f85a0b8b23f64449c906d6c6120c65a9 Mon Sep 17 00:00:00 2001 From: svetoslav-nikol0v <136077184+svetoslav-nikol0v@users.noreply.github.com> Date: Thu, 16 Nov 2023 15:05:50 +0200 Subject: [PATCH] Fix for "Reduce of empty array with no initial value" error in ContractFunctionParameters array methods (#2016) * adding an initial value of the reduce method + tests Signed-off-by: svetoslav-nikol0v * remove .only Signed-off-by: svetoslav-nikol0v * adding test for when methods fail Signed-off-by: svetoslav-nikol0v * using const Signed-off-by: svetoslav-nikol0v --------- Signed-off-by: svetoslav-nikol0v --- src/contract/ContractFunctionParameters.js | 2 +- ...ntractFunctionParametersIntegrationTest.js | 99 +++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) diff --git a/src/contract/ContractFunctionParameters.js b/src/contract/ContractFunctionParameters.js index 2e62bd8e22..9a3b90aec2 100644 --- a/src/contract/ContractFunctionParameters.js +++ b/src/contract/ContractFunctionParameters.js @@ -1451,7 +1451,7 @@ function argumentToBytes(param, ty) { const totalLengthOfValues = values .map((a) => a.length) - .reduce((total, current) => total + current); + .reduce((total, current) => total + current, 0); switch (ty.ty) { case ArgumentType.uint8: diff --git a/test/integration/ContractFunctionParametersIntegrationTest.js b/test/integration/ContractFunctionParametersIntegrationTest.js index 87255ee5f3..f7edfb9b98 100644 --- a/test/integration/ContractFunctionParametersIntegrationTest.js +++ b/test/integration/ContractFunctionParametersIntegrationTest.js @@ -9,6 +9,7 @@ import { FileAppendTransaction, FileDeleteTransaction, } from "../../src/exports.js"; +import { REQUIRE_ARRAY_ERROR } from "../../src/util.js"; import IntegrationTestEnv from "./client/NodeIntegrationTestEnv.js"; import BigNumber from "bignumber.js"; import Long from "long"; @@ -484,6 +485,55 @@ describe("ContractFunctionParameters", function () { }); } ); + + it(`addInt${bitSize}Array method should return an empty array`, async function () { + const contractQuery = await new ContractCallQuery() + //Set the gas for the query + .setGas(15000000) + //Set the contract ID to return the request for + .setContractId(newContractId) + //Set the contract function to call + .setFunction( + `returnInt${bitSize}Array`, + new ContractFunctionParameters()[ + `addInt${bitSize}Array` + ]( + // eslint-disable-next-line no-loss-of-precision + [] + ) + ) + //Set the query payment for the node returning the request + //This value must cover the cost of the request otherwise will fail + .setQueryPayment(new Hbar(15)); + + //Submit to a Hedera network + const txResponse = await contractQuery.execute(env.client); + const result = txResponse.getResult([`uint${bitSize}[]`])[0]; + expect(result).to.be.an("array").to.have.length(0); + }); + + it(`addInt${bitSize}Array method should throw an error`, async function () { + try { + await new ContractCallQuery() + //Set the gas for the query + .setGas(15000000) + //Set the contract ID to return the request for + .setContractId(newContractId) + //Set the contract function to call + .setFunction( + `returnInt${bitSize}Array`, + new ContractFunctionParameters()[ + `addInt${bitSize}Array` + ]() + ) + //Set the query payment for the node returning the request + //This value must cover the cost of the request otherwise will fail + .setQueryPayment(new Hbar(15)); + } catch (error) { + expect(error).to.be.instanceOf(Error); + expect(error.message).to.be.equal(REQUIRE_ARRAY_ERROR); + } + }); }); describe(`Tests for addUint${bitSize} method`, function () { @@ -707,6 +757,55 @@ describe("ContractFunctionParameters", function () { }); } ); + + it(`addUint${bitSize}Array method should return an empty array`, async function () { + const contractQuery = await new ContractCallQuery() + //Set the gas for the query + .setGas(15000000) + //Set the contract ID to return the request for + .setContractId(newContractId) + //Set the contract function to call + .setFunction( + `returnUint${bitSize}Array`, + new ContractFunctionParameters()[ + `addUint${bitSize}Array` + ]( + // eslint-disable-next-line no-loss-of-precision + [] + ) + ) + //Set the query payment for the node returning the request + //This value must cover the cost of the request otherwise will fail + .setQueryPayment(new Hbar(15)); + + //Submit to a Hedera network + const txResponse = await contractQuery.execute(env.client); + const result = txResponse.getResult([`uint${bitSize}[]`])[0]; + expect(result).to.be.an("array").to.have.length(0); + }); + + it(`addUint${bitSize}Array method should throw an error`, async function () { + try { + await new ContractCallQuery() + //Set the gas for the query + .setGas(15000000) + //Set the contract ID to return the request for + .setContractId(newContractId) + //Set the contract function to call + .setFunction( + `returnUint${bitSize}Array`, + new ContractFunctionParameters()[ + `addUint${bitSize}Array` + ]() + ) + //Set the query payment for the node returning the request + //This value must cover the cost of the request otherwise will fail + .setQueryPayment(new Hbar(15)); + } catch (error) { + expect(error).to.be.instanceOf(Error); + expect(error.message).to.be.equal(REQUIRE_ARRAY_ERROR); + } + }); }); });