hyperledger-web3j / web3j

Lightweight Java and Android library for integration with Ethereum clients
https://www.web3labs.com/web3j-sdk
Other
5.12k stars 1.69k forks source link

合约方法返回数组错误 Contract method returned array error #1130

Closed dengyanyu closed 3 years ago

dengyanyu commented 4 years ago

geth version 1.9.8-unstable solidity code:

 pragma solidity >=0.4.22 <0.6.0;
 pragma experimental ABIEncoderV2;

 contract testzn {

     function test1() public view returns(string[] memory keys, string[] memory values) 
     {
         keys = new string[](2);
         values = new string[](2);

         keys[0] = "在执行案件";
         values[0] = "没有记录";

         keys[1] = "zaizhixinganjian";
         values[1] = "meiyoujilu";
     }

     function test2(string[] memory keys ,string[] memory values) public view returns( string[] memory ,string[] memory) 
     {

         return (keys,values);
     }

      function test3() public view returns(string[] memory ,string[] memory ) 
     {
        string[] memory   keys = new string[](2);
        string[] memory  values = new string[](2);

         keys[0] = "在执行案件";
         values[0] = "没有记录";

         keys[1] = "zaizhixinganjian";
         values[1] = "meiyoujilu";
         return (keys,values);
     }

}

i call the test2 when i use param ["中","国"],["你","好"] return error ,as img image 我经过解析java逻辑 对照参数发现 结果如下: image

i found when i read the array firt element must forward 128 offset,i can get right char , image

i have a bad idea ,if i want back ["1","2","3"] in Contract, i use ["0","1","2","3"] instead, i give up "0" when i get it in java.

but when i use a long array param for test , i get other error

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0 at java.util.Collections$EmptyList.get(Collections.java:4454) at test.x.SampleContract$6.call(SampleContract.java:89) at test.x.SampleContract$6.call(SampleContract.java:1) at org.web3j.protocol.core.RemoteCall.send(RemoteCall.java:42) at test.x.QklUtil.getJzywHc(QklUtil.java:140) at test.x.QklUtil.main(QklUtil.java:221)

if i use it the wrong way??

AlexandrouR commented 4 years ago

@dengyanyu Hey, could you please replace the Chinese characters with English ones. Thanks

josh-richardson commented 4 years ago

We should also mention that we don't support ABIEncoderV2 yet.

dengyanyu commented 4 years ago

@josh-richardson thank you~
we use utf8 encode ,so it just not support in array struct encode? if i want use web3j decode ABIEncoderV2 ,can you give me some way for it? thank you

We should also mention that we don't support ABIEncoderV2 yet.

dengyanyu commented 4 years ago

@josh-richardson thank you~ we use utf8 encode ,so it just not support in array struct encode? if i want use web3j decode ABIEncoderV2 ,can you give me some way for it? thank you

We should also mention that we don't support ABIEncoderV2 yet.

so i just do it sample ~ , i decode it by this ,when the DefaultFunctionReturnDecoder.build be call , if outputParameters is dynamic_array use my self code all DefaultFunctionReturnDecoder.only_dynamic_array_return method, it just for myself ,you must correction In your own projects, you can refer to this : en:https://solidity.readthedocs.io/en/v0.6.0/abi-spec.html cn:https://learnblockchain.cn/docs/solidity/abi-spec.html image

where i modify it~ ,
image

DefaultFunctionReturnDecoder.zip

dengyanyu commented 4 years ago

also i found thing in abi encode . here,
image the source code of encoding is clac len by java string.length ,but if i use utf8 should use the byte[].length , so There may be a mistake here , It's also possible that I used the wrong method~~~