hedera-sdk-js icon indicating copy to clipboard operation
hedera-sdk-js copied to clipboard

ContractFunctionParameters fails with complex types like boolean arrays

Open MiguelLZPF opened this issue 5 months ago • 1 comments

Description

We encountered an issue using ContractFunctionParameters().add<SolidityType>(). While it works for most basic types, it fails when dealing with complex types, such as an array of booleans, and doesn't generate the correct structure.

We were unable to find any documentation or examples dealing with complex types like arrays of booleans. If there is a way to correctly handle these complex types using ContractFunctionParameters, it is not clearly documented.

Expected Behavior

ContractFunctionParameters should correctly handle complex types such as boolean arrays.

Actual Behavior

ContractFunctionParameters fails to generate the correct structure for complex types.

Steps to reproduce

Example Code:

Here's the code we initially tried:

// Create ContractFunctionParameters and add the parameters
// Convert boolean array to byte array
const byteArray = actives.map((val) => (val ? 0x01 : 0x00));
const functionParameters = new ContractFunctionParameters()
  .addBytes32Array(
    roles.map((role) => new Uint8Array(Buffer.from(role, 'hex'))),
  )
  .addBytes(Buffer.from(byteArray))
  .addAddress(targetId.toString());
const transaction = new ContractExecuteTransaction()
  .setContractId(address.toContractId().toString())
  .setGas(gas)
  .setFunction(FUNCTION_NAME, functionParameters);

We had to switch to the following approach to make it work:

```typescript
const functionDataEncodedHex = factoryInstance.interface.encodeFunctionData(
  FUNCTION_NAME,
  [roles, actives, targetId.toString()],
);
const functionDataEncoded = new Uint8Array(
  Buffer.from(functionDataEncodedHex.slice(2), 'hex'),
);
const transaction = new ContractExecuteTransaction()
  .setContractId(address.toContractId().toString())
  .setGas(gas)
  .setFunctionParameters(functionDataEncoded);

Additional context

We checked the available documentation, but only found standard examples for basic contract calls.

Hedera network

testnet

Version

v2.33.0

Operating system

Linux

MiguelLZPF avatar Sep 25 '24 12:09 MiguelLZPF