2017-06-23 13 views
0

は、これは私がweb3Jを使用してスマート契約ラッパーを生成している私のプライベートネットワークweb3jは契約機能を使用していませんか?

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]; 
     } 
} 

の中に私の簡単な契約であると私は

ように私の契約機能にアクセスしようとしていたときに

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

のような機能があります

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

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] 

助けてください。

答えて

0

先物が非同期なので、get()は現在計算中の結果値を取得しようとします。計算が完了した後でのみ機能します。

Java Future APIは、あなたが望むものをサポートしていないと思います。代わりに、私はCompletableFutureを使用することをお勧めします。これはjoin()のメソッドを持っています。

契約からコードを生成したとき、私は同じ問題がありました。だから、私はジェネレータを捨てて、CompletableFutureで生成されたコードのFutureをすべて置き換えました。 Web3jの見落としだと思いますが、この問題に対処するには別の方法がありますが、それについてはわかりません。

+0

web3jは、生成されたスマートコントラクトラッパーがAndroid用にJava 1.6互換である必要があるため、通常のFuturesを使用します。 JSON-RPC呼び出しとよく似た同期/非同期の選択肢を提供することを将来のリリースで期待しています。 –

+0

@ConorSvensson Kotlinを見るのは興味深いかもしれません。Android上でサポートされており、古いJavaバージョンをターゲットにすることができます。私はまだそれを試していないが、それは非常に見栄えの良い非同期サポートを持っています。 – Jodiug

関連する問題