web3j无法使用合同功能吗? [英] web3j not working with contract function?

查看:176
本文介绍了web3j无法使用合同功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在我的私人网络中签订的简单合同

This is my simple contract in my private network

contract AB {
    /* This creates an array with all balances */
    mapping (address => uint256) public balanceOf;

    /* Initializes contract with initial supply tokens to the creator of the contract */
    function AB () {
        balanceOf[msg.sender] = 1200;              // Give the creator all initial tokens
    }

    /* Send coins */
    function transfer(address _to, uint256 _value) {
        if (balanceOf[msg.sender] < _value) throw;           // Check if the sender has enough
        if (balanceOf[_to] + _value < balanceOf[_to]) throw; // Check for overflows
        balanceOf[msg.sender] -= _value;                     // Subtract from the sender
        balanceOf[_to] += _value;                            // Add the same to the recipient
    }

    function gettokenBalance(address to)constant returns (uint256){
          return balanceOf[to];
       }
}

我已经使用web3J生成了智能合约包装器,并且有类似的功能

I have generated smart contract wrappers using web3J and there is function like

public Future<Uint256> gettokenBalance(Address to) {
        Function function = new Function("gettokenBalance", 
                Arrays.<Type>asList(to), 
                Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
        return executeCallSingleValueReturnAsync(function);
    }

当我尝试访问合同功能时

when i am trying to access my contract function like

AB newAB = AB.load(contractaddress, web3j, credentials, gasprice, gaslimit);
        Future<Uint256> result = newAB.gettokenBalance(new Address(address));
        LOGGER.info("result:::"+result.get());

它给了我一个异常,例如

it is giving me a exception like

 java.util.concurrent.ExecutionException: java.lang.IndexOutOfBoundsException: Index: 0
    at java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:357) ~[na:1.8.0_91]
    at java.util.concurrent.CompletableFuture.get(CompletableFuture.java:1895) ~[na:1.8.0_91]
    at com.belrium.service.UserWalletService.check(UserWalletService.java:197) ~[classes/:na]
    at com.belrium.controller.UserController.check(UserController.java:119) ~[classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_91]

请帮助.

推荐答案

期货是异步的,因此get()将尝试获取当前仍在计算中的结果值.只有在计算完成后才能起作用.

Futures are asynchronous, so get() will try to fetch the result value, which is currently still being computed. It only works after the computation is done.

我认为Java Future API不支持您想要的.相反,我建议使用CompletableFuture,它具有join()方法,该方法完全可以满足您的需要:等待并获取.

I think the Java Future API does not support what you want. Instead, I can recommend using CompletableFuture, which has a join() method that does exactly what you want: wait and get.

当我从合同生成代码时,我遇到了同样的问题.因此,我放弃了生成器,并将生成的代码中的所有Future替换为CompletableFuture.我认为这是来自web3j的疏忽,尽管也许有另一种方式解决我不知道的问题!

When I generated code from contracts, I had the same problem. So I ditched the generator and replaced all the Futures in my generated code with CompletableFutures. I think it's an oversight from web3j, although maybe there is a different way to deal with this problem that I don't know about!

这篇关于web3j无法使用合同功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆