我的稳固性合同如下:
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。
有关详细信息,请参阅文档。
下面是Java中的一个例子(在Spring Boot下)祝你好运http://blockchainers.org/index.php/2016/09/22/static-type-safety-for-dapps-without-javascript/