Home | 简体中文 | 繁体中文 | 杂文 | 知乎专栏 | Github | OSChina 博客 | 云社区 | 云栖社区 | Facebook | Linkedin | 视频教程 | 打赏(Donations) | About
知乎专栏多维度架构微信号 netkiller-ebook | QQ群:128659835 请注明“读者”

17.13. 智能合约

		
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			
		
		

17.13.1. 载入合约

			
HelloWorld contract = HelloWorld.load(contractAddress,web3j,credentials, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT);