| 知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
neo@netkiller ~/ethereum/solidity % cat netkiller.sol
pragma solidity ^0.4.18;
contract Netkiller {
string name;
int num;
function Netkiller() public{
name = "default";
num = 1;
}
function setName(string _name) public{
name = _name;
}
function getName() public view returns(string){
return name;
}
function setNum(int n) public{
num = n;
}
function addNum(int m) public view returns(int res){
res = m + num;
}
}
編譯智能合約
$ solc /path/to/<smart-contract>.sol --bin --abi --optimize -o output/ $ web3j solidity generate /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
$ solc netkiller.sol --bin --abi --optimize -o output/
$ web3j solidity generate output/Netkiller.bin output/Netkiller.abi -p cn.netkiller.ethereum.contract -o java
_ _____ _ _
| | |____ (_) (_)
__ _____| |__ / /_ _ ___
\ \ /\ / / _ \ '_ \ \ \ | | | / _ \
\ V V / __/ |_) |.___/ / | _ | || (_) |
\_/\_/ \___|_.__/ \____/| |(_)|_| \___/
_/ |
|__/
Generating cn.netkiller.ethereum.contract.Netkiller ... File written to java
neo@netkiller ~/ethereum/solidity % ll java/cn/netkiller/ethereum/contract/Netkiller.java
-rw-rw-r-- 1 neo neo 5.9K Feb 3 23:02 java/cn/netkiller/ethereum/contract/Netkiller.java
啟動以太坊,並開始挖礦。注意參數 --mine --minerthreads 1 ,你也可以啟動後在JavaScript 控制檯鐘啟動挖礦。
neo@netkiller ~ % geth --networkid 123456 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --mine --minerthreads 1
package cn.netkiller.ethereum;
import java.math.BigInteger;
import org.web3j.crypto.Credentials;
import org.web3j.crypto.WalletUtils;
import org.web3j.protocol.Web3j;
import org.web3j.protocol.http.HttpService;
import org.web3j.tx.Contract;
import org.web3j.tx.ManagedTransaction;
import cn.netkiller.ethereum.contract.Netkiller;
public class ContractTest {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String walletfile = "/Users/neo/Downloads/UTC--2018-01-20T04-04-06.786586541Z--83fda0ba7e6cfa8d7319d78fa0e6b753a2bcb5a6";
Web3j web3j = Web3j.build(new HttpService("http://172.16.0.1:8545"));
Credentials credentials = WalletUtils.loadCredentials("", walletfile);
Netkiller contract = Netkiller.deploy(web3j, credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT).send();
System.out.println(contract.isValid());
if (contract.isValid()) {
System.out.println("---");
String contractAddress = contract.getContractAddress();
System.out.println(contractAddress);
System.out.println("---");
String result = contract.getName().send();
System.out.println(result);
contract.setName("Netkiller").send();
System.out.println(contract.getName().send());
System.out.println("---");
contract.setNum(BigInteger.valueOf(8)).send();
System.out.println(contract.addNum(BigInteger.valueOf(8)).send());
System.out.println("---");
} else {
System.out.println("Deploy ERROR !!!");
}
}
}
運行結果
true --- 0xef872f1b344a4b7c765c7d765a3cc82b741777a9 --- default Netkiller --- 16 ---
在程序運行是,去看 geth 打印的日誌,有如下記錄打印
INFO [02-04|00:04:43] Submitted transaction fullhash=0x9f70ccb600294d2dd6dda08d090362131b107d42a692f27dd4a3b7548dbaf22c recipient=0xEF872F1b344a4B7C765c7D765a3cC82b741777a9