web3j icon indicating copy to clipboard operation
web3j copied to clipboard

how to encode struct[][]

Open daxuanxiaoyou opened this issue 3 years ago • 6 comments

Issue_title

how to encode struct[][]

Issue_description

i.e: { "components":[ { "internalType":"uint256", "name":"orderIndex", "type":"uint256" }, { "internalType":"uint256", "name":"itemIndex", "type":"uint256" } ], "internalType":"struct FulfillmentComponent[][]", "name":"", "type":"tuple[][]" }

Issue_context

how to encode this tuple[][]

daxuanxiaoyou avatar Aug 17 '23 12:08 daxuanxiaoyou

i try to this with web3j:

/*

  • offerFulfillments = [[{"orderIndex":0,"itemIndex":0}],[{"orderIndex":1,"itemIndex":0}]] */ List encodedParameters = new ArrayList<>(); encodedParameters.add( new DynamicArray( DynamicArray.class, offerFulfillments.stream().map(ov -> new DynamicArray<>( DynamicStruct.class, ov.stream().map(iv -> new DynamicStruct( new Uint256(iv.getOrderIndex()), new Uint256(iv.getItemIndex())) ).collect(Collectors.toList()) ) ).collect(Collectors.toList()) ) ); return FunctionEncoder.encodeConstructor(encodedParameters);

the encode data is : 0000000000000000000000000000000000000000000000000000000000000020 0000000000000000000000000000000000000000000000000000000000000002 0000000000000000000000000000000000000000000000000000000000000001 -- the 1 0000000000000000000000000000000000000000000000000000000000000020 -- ?? 0000000000000000000000000000000000000000000000000000000000000000 -- 0 0000000000000000000000000000000000000000000000000000000000000000 -- 0 0000000000000000000000000000000000000000000000000000000000000001 -- the 2 0000000000000000000000000000000000000000000000000000000000000020 -- ?? 0000000000000000000000000000000000000000000000000000000000000001 -- 1 0000000000000000000000000000000000000000000000000000000000000000 -- 0

============================================================================================but i try it with web3js:

abi = { "components":[ { "internalType":"uint256", "name":"orderIndex", "type":"uint256" }, { "internalType":"uint256", "name":"itemIndex", "type":"uint256" } ], "internalType":"struct FulfillmentComponent[][]", "name":"", "type":"tuple[][]" };

const functionSignature = web3.eth.abi.encodeFunctionCall(abi, [offerFulfillments]);

the i get the encode data is

0000000000000000000000000000000000000000000000000000000000000020 0000000000000000000000000000000000000000000000000000000000000002 -- total 2 0000000000000000000000000000000000000000000000000000000000000040 -- offset of 1 00000000000000000000000000000000000000000000000000000000000000a0 -- offset of 2 0000000000000000000000000000000000000000000000000000000000000001 -- the 1 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000001 -- the 2 0000000000000000000000000000000000000000000000000000000000000001 0000000000000000000000000000000000000000000000000000000000000000

and web3js is right;

so ,how to get the right data with web3j

daxuanxiaoyou avatar Aug 18 '23 10:08 daxuanxiaoyou

@daxuanxiaoyou result obtained by you with web3j represents:

0000000000000000000000000000000000000000000000000000000000000020 0000000000000000000000000000000000000000000000000000000000000002 - number of elements in the main dynamic array 0000000000000000000000000000000000000000000000000000000000000001 -- number of elements in the first dynamic array (the 1 in your case) 0000000000000000000000000000000000000000000000000000000000000020 -- offset of the element (listed bellow) 0000000000000000000000000000000000000000000000000000000000000000 -- 0 0000000000000000000000000000000000000000000000000000000000000000 -- 0 0000000000000000000000000000000000000000000000000000000000000001 -- number of elements in the second dynamic array (the 2 in your case) 0000000000000000000000000000000000000000000000000000000000000020 -- offset of the element (listed bellow) 0000000000000000000000000000000000000000000000000000000000000001 -- 1 0000000000000000000000000000000000000000000000000000000000000000 -- 0

gtebrean avatar Sep 07 '23 14:09 gtebrean

For Web3J right now in a collection of objects the encode function returns the offsets as mentioned above which seems to be different that the order of offsets returned by web3js

gtebrean avatar Sep 07 '23 14:09 gtebrean

@daxuanxiaoyou I tried to reproduce your web3js result and I couldn't. I encodeFunctionCall used from web3-eth-abi 4.1.1 but it seems is not compiling with your input, json is not according to AbiFunctionFragment format. I assume you used an older version of web3. Please could you share the web3 version and your test?

gtebrean avatar Sep 08 '23 14:09 gtebrean

Thanks,man. Which version resolve this BUG?

daxuanxiaoyou avatar Sep 25 '23 06:09 daxuanxiaoyou

Thanks,man. Which version resolve this BUG?

not released yet, will be part of 4.10.4

gtebrean avatar Sep 25 '23 07:09 gtebrean