簡體   English   中英

Web3j 調用智能合約中的變量

[英]Web3j call a variable in a smart contract

我正在嘗試使用solidity、geth和web3j獲取智能合約中變量的值。

合約 HelloWorld 非常簡單:

pragma solidity ^0.6.10;
   contract HelloWorld {
   uint256 public counter = 5;
  
   function add() public {  //increases counter by 1
       counter++;
   }
 
   function subtract() public { //decreases counter by 1
       counter--;
   }
   
   function getCounter() public view returns (uint256) {
       return counter;
   }
}

web3j 沒有 call() function,只有 send() 令人驚訝。

當我嘗試按照 web3j 說明獲取計數器時:

contract.getCounter().send()

我得到一個交易收據而不是價值 uint256。

有人可以幫忙嗎?

謝謝

將要

需要修改生成的HelloWorld.java文件中的getCounter() function。

public RemoteCall<Type> getCounter() {
    final Function function = new Function(
            FUNC_GETCOUNTER, 
            Arrays.<Type>asList(), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint>() {}));
    return executeRemoteCallSingleValueReturn(function);
}

要獲取該值,請使用以下代碼:

Type message = contract.getCounter().send();
System.out.println(message.getValue()); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM