我的 solidity 合约如下:
contract SimpleStorage {
uint storedData;
function set(uint x) {
storedData = x;
}
function get() constant returns (uint retVal) {
return storedData;
}}
生成的abi如下:
[ { "constant": false, "inputs": [ { "name": "x", "type": "uint256" } ], "name": "set", "outputs": [], "type": "function" }, { "constant": true, "inputs": [], "name": "get", "outputs": [ { "name": "retVal", "type": "uint256", "value": "0" } ], "type": "function" } ]
并被 https://github.com/ethereum/wiki/wiki/JSON-RPC 引用,
如何使用java(不是js)调用get函数并获取值?
最佳答案
web3j迎合这个用例。它从 Solidity 编译的二进制文件和 ABI 文件生成 Java 智能合约包装器。
一旦您使用 web3j 生成了包装器代码,您就可以进行部署,然后调用上述合约示例中的方法,如下所示:
SimpleStorage simpleStorage = SimpleStorage.deploy(
<web3j>, <credentials>, GAS_PRICE, GAS_LIMIT,
BigInteger.ZERO); // ether value of contract
TransactionReceipt transactionReceipt = simpleStorage.set(
new Uint256(BigInteger.valueOf(1000))),
.get();
Uint256 result = simpleStorage.get()
.get();
注意:额外的 get()
是因为 web3j 在与以太坊客户端交互时返回 Java Futures。
参见 docs了解更多信息。
关于java - 如何使用java从solidity合约中获取值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38733656/