[英]How to call the smart contract methods from android app using 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.