2016-08-03 70 views
1
摆脱坚固合约价值

我坚固的合同是以下几点:如何使用Java

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)得到值

回答

2

web3j迎合了这个非常用例。它使用Solidity编译的二进制文件和ABI文件在Java中生成Smart Contract包装器。

一旦你产生与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未来与Ethereum客户进行交互时。

查看docs了解更多信息。

+0

#Conor我有以下的答案,这是非常好理解,但我面临一些问题,你可以请看看这个,并帮助我https://stackoverflow.com/questions/44719819/web3j-not-working-with -contract功能 –