Search code examples
web3-java

How do I encode an array of Byte32 values for Web3j to pass to my smart contract?


Contract function is defined as:

  function createAggregate (string memory key, bytes32[2] memory part_array) public returns (bytes32)

and have incoming a list of parts, defined as...

    List<Bytes32> elements

so was trying to use:

    List<Type> items = new ArrayList<Type>();
    items.add(...);                    // user reference
    items.add(new DynamicArray<>(elements));

    final Function function = new Function("createAggregate", 
             items, 
             Arrays.asList(new TypeReference<Bytes32>() {})
            );
    ...

But this does not work, seems to be an encoding issue - what is the right what of encoding the Bytes32 ? (This seems to work fine for an array of strings)


Solution

  • Sort of solved this with the following (although was really looking for a more dynamic size solution)

    new StaticArray2(Bytes32.class, Utils.typeMap(elements, Bytes32.class));