Skip to content

Commit

Permalink
Fix for "Reduce of empty array with no initial value" error in Contra…
Browse files Browse the repository at this point in the history
…ctFunctionParameters array methods (#2016)

* adding an initial value of the reduce method + tests

Signed-off-by: svetoslav-nikol0v <[email protected]>

* remove .only

Signed-off-by: svetoslav-nikol0v <[email protected]>

* adding test for when methods fail

Signed-off-by: svetoslav-nikol0v <[email protected]>

* using const

Signed-off-by: svetoslav-nikol0v <[email protected]>

---------

Signed-off-by: svetoslav-nikol0v <[email protected]>
  • Loading branch information
svetoslav-nikol0v authored Nov 16, 2023
1 parent 5f75aa7 commit da10ed5
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/contract/ContractFunctionParameters.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
99 changes: 99 additions & 0 deletions test/integration/ContractFunctionParametersIntegrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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);
}
});
});
});

Expand Down

0 comments on commit da10ed5

Please sign in to comment.