javaproxyweb3-java

How to pass bytes type parameters to a function in java web3


I am trying to deploy a proxy contract and it has the following constructor

constructor(address beacon, bytes memory data,string memory assetId) These are the inputs I am trying to pass to this constructor

  1. beacon - 0xa7cF300E62EA5c7eA611e71Fb30A41182AC3ACDD
  2. data - 0x000000000000000000000000a7cf300e
  3. assetId -"1"

The following is the code I have written

                    FunctionEncoder.encodeConstructor(
                            Arrays.asList(
                                    new Address(beaconAddress),
                                    new DynamicBytes("0x000000000000000000000000a7cf300e".getBytes()),
                                    new Utf8String("1")));

The ideal output should be something like this

000000000000000000000000a7cf300e62ea5c7ea611e71fb30a41182ac3acdd000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000010000000000000000000000000a7cf300e0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000

But what I recieve is something like this

000000000000000000000000a7cf300e62ea5c7ea611e71fb30a41182ac3acdd000000000000000000000000000000000000000000000000000000
000000006000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000223078303030303030303030303030303030303030303030303030613763663330306500000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000013100000000000000000000000000000000000000000000000000000000000000

What I think has happened is that the data field in the constructor has been converted to a string of bytes. So what I am looking for is a solution/method to pass bytes type parameters to a function. There is a way to do this win web3.js. I a wondering anyone knows a way to do this in web3-java.

Thanks in advance


Solution

  • Instead of directly passing the bytes to the encodeConstructor function use the following method

    byte[] myBytes = parseHexBinary("0x000000000000000000000000a7cf300e");

    parseHexBinary is taken from the DatatypeConverter class.

    Then pass this byte array to the encodeConstructor as below

    String encodedConstructor =
                        FunctionEncoder.encodeConstructor(
                                Arrays.asList(
                                        new Address(beaconAddress),
                                        new DynamicBytes(myBytes),
                                        new Utf8String("1")));