2016-08-03 59 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功能 –