Raphaël Messian
Raphaël Messian

Reputation: 112

How can I pass a complex type when calling my smart contract on Hedera?

I have a basic contract that looks like this:

// SPDX-License-Identifier: MIT

pragma solidity ^0.8;
pragma abicoder v2;

contract testStruct {

    struct Example {
        uint256 number;
        address user;
    }

    Example public example;

    function set_struct(Example memory _example) public {
        example = _example;
    }
}

I have tried using this sdk like this.

//Create the transaction
const transaction = new ContractExecuteTransaction()
     .setContractId(newContractId)
     .setGas(100_000_000)
     .setFunction("set_struct", new ContractFunctionParameters()
           .addBytesArray(byte[][] structInBytes)

Some data type are not presented in the SDK. How can I call a smart contract and pass a complex type like a struct ?

Upvotes: 3

Views: 211

Answers (1)

Acehilm
Acehilm

Reputation: 113

If you are using structs as inputs/outputs to your contracts, or data types that aren't supported by the SDKs, you should check this example repo out which shows how to use commonly known libraries (web3.js, ethers.js, web3.j and abiDecode.js) to encode and decode contract function parameters and results https://github.com/hashgraph/hedera-smart-contracts-libs-lab

For example:

// get the constructor parameter
// .slice(2) to remove leading '0x'
const constructParameterAsHexString = abiInterface.encodeDeploy([constructMessage]).slice(2);
// convert to a Uint8Array
const constructorParametersAsUint8Array = Buffer.from(constructParameterAsHexString, 'hex');

Upvotes: 2

Related Questions