solidity踩坑日记之java通过web3j调用智能合约传递数组参数的坑

最近在写solidity智能合约,用java与solidity交互过程中,需要传递数组参数到智能合约,但是用web3j转换后的数组参数去调用智能合约接口一直返回错误信息,在万能的互联网上翻阅了大量资料后,终于解决,在此记录一下:

首先java项目需要引入web3j的依赖包:

        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>4.5.18</version>
            <exclusions>
                <exclusion>
                    <artifactId>okhttp</artifactId>
                    <groupId>com.squareup.okhttp</groupId>
                </exclusion>
            </exclusions>
        </dependency>

合约demo代码如下:

    function arrTest(bytes32[] memory arr) public view  returns(string memory) {
        return bytes32ToString(arr[0]);
    }

    function bytes32ToString(bytes32 bname) view public returns(string memory){
        // 此处要加上memory
        // 先将有效字符计算出来
        bytes memory bytesChar = new bytes(bname.length);
        uint charCount = 0;
        for(uint i = 0;i < bname.length; i++){
            bytes1 char = bname[i];
            if(char != 0){
                charCount++;
            }    
        }
        // 新建数组,指定长度为有效字节长度
        bytes memory bytesName = new bytes(charCount);
        for(uint j = 0;j < charCount;j++){
            bytesName[j] = bname[j];
        }
        return string(bytesName);
    }

java调用合约方法代码如下:

    public static Bytes32 stringToBytes32(String string) {
        byte[] byteValue = string.getBytes();
        byte[] byteValueLen32 = new byte[32];
        System.arraycopy(byteValue, 0, byteValueLen32, 0, byteValue.length);
        return new Bytes32(byteValueLen32);
    }

    public RemoteCall<Utf8String> arrTest(List<String> data) {
        //首先把String转换成Bytes32
        Bytes32[] _dataBytes32 = new Bytes32[data.size()];
        List<Uint> _dataUint = new ArrayList<>();

        for(int i = 0 ; i < data.size() ; i++) {
            _dataBytes32[i] = stringToBytes32(data.get(i));
        }
        //需要使用DynamicArray 进行参数传递其他传递方式均出现错误 如: 直接传入参数dataBytes32 的数组,程序报错 ;List形式传参,程序报错
        DynamicArray<Bytes32> inputDataByte32 = new DynamicArray<Bytes32>(Bytes32.class , _dataBytes32);
        final Function function = new Function(
                "arrTest",
                Arrays.<Type>asList(
                        //错误示例
                        //dataBytes32
                        //正确示例
                        inputDataByte32
                ),
                Arrays.asList(
                        new TypeReference<Utf8String>() {
                        }
                )
        );
        return executeRemoteCallSingleValueReturn(function);
    }

通过各种尝试,发现java调用合约带数组的参数,需要用 DynamicArray 作为中转调用。

欢迎交流区块链问题,qq群组: 786937587

 

 

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值