I have a solidity function that returns an array. I'm trying to read the values inside this array from web3j but it gives me stage results.

My solidity function

function getAllSubTokenByTokenID(uint256 _id)
    returns (uint256[] memory)
    return tokens[_id].subTokens;

tokens is a Struct and subTokens an array of uint32

In Web3j:

Function function2 = new Function(
            Arrays.asList(new Uint256(1)),  // Solidity Types in smart contract functions
            Arrays.asList(new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}, new TypeReference<Uint256>() {}));

    String encodedFunction2 = FunctionEncoder.encode(function2);
    org.web3j.protocol.core.methods.response.EthCall response2 = web3j.ethCall(
            Transaction.createEthCallTransaction(credential.getAddress(), "0x1B2858ae498bA0aEd0176Bd68ad4fB950E80D708", encodedFunction2), DefaultBlockParameterName.LATEST)

    List<Type> values = FunctionReturnDecoder.decode(
            response2.getValue(), function2.getOutputParameters());

System.out.println((values.get(0)).getValue()); print always 32

System.out.println((values.get(1)).getValue()); print the array's length

System.out.println((values.get(2)).getValue()); print the first item into the array

If I call the getter inside web3js all wors fine.

If I call this function in web3j

function getSub() external pure returns(uint256[4] memory) {
    uint256[4] memory a = [uint(1),2,3,4];
    return a;

all works fine

  • the second function getSub() return a static array, isn't it? – Majd TL May 13 '19 at 15:00
  • yes, but I tried to create a static array and then populate it with my datas – magiceddy May 13 '19 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.