web3j对于智能合约有两种方式
1、第一种:直接使用RawTrasaction进行创建
1 // using a raw transaction 2 RawTransaction rawTransaction = RawTransaction.createContractTransaction( 3 <nonce>, 4 <gasPrice>, 5 <gasLimit>, 6 <value>, 7 "0x <compiled smart contract code>"); 8 // send... 9 10 // get contract address 11 EthGetTransactionReceipt transactionReceipt = 12 web3j.ethGetTransactionReceipt(transactionHash).send(); 13 14 if (transactionReceipt.getTransactionReceipt.isPresent()) { 15 String contractAddress = transactionReceipt.get().getContractAddress(); 16 } else { 17 // try again 18 }
2、第二种:将合约代码转换成Java Bean。
(1)首先我们需要一份写好的智能合约。
1 pragma solidity ^0.4.18; 2 3 // Example taken from https://www.ethereum.org/greeter, also used in 4 // https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial#your-first-citizen-the-greeter 5 6 contract mortal { 7 /* Define variable owner of the type address*/ 8 address owner; 9 10 /* this function is executed at initialization and sets the owner of the contract */ 11 function mortal() { owner = msg.sender; } 12 13 /* Function to recover the funds on the contract */ 14 function kill() { if (msg.sender == owner) suicide(owner); } 15 } 16 17 contract greeter is mortal { 18 /* define variable greeting of the type string */ 19 string greeting; 20 21 /* this runs when the contract is executed */ 22 function greeter(string _greeting) public { 23 greeting = _greeting; 24 } 25 26 /* main function */ 27 function greet() constant returns (string) { 28 return greeting; 29 } 30 }
注意:智能合约的代码是solidity写的,有关solidity语法和使用可以查询solidity官网
https://solidity.readthedocs.io/en/v0.4.21/
有可能需要FQ,请自行百度科学上网
(2)进入https://ethereum.github.io/browser-solidity/#optimize=false&version=soljson-v0.4.19+commit.c4cbbb05.js网站,此网站是在线智能合约编译网站,所以比较简便,不需要安装truffle等工具。
点击图示中details按钮,会出现智能合约的ABI和BIN
将BYTECODE中的object复制下来保存成greeter.bin,点击ABI旁边的复制按钮将其保存成greeter.abi
进入https://github.com/web3j/web3j/releases/tag/v3.3.1网站,下载web3j-3.3.1.tar,并解压。
进入目录bin下,将greeter.bin和greeter.abi,复制到目录下,在此目录命令行执行
web3j solidity generate --javaTypes greeter.bin greeter.abi -o src/ -p com.your.organisation.name
web3j solidity generate [--javaTypes|--solidityTypes] /path/to/<smart-contract>.bin /path/to/<smart-contract>.abi -o /path/to/src/main/java -p com.your.organisation.name
-o 是源代码要放的目录,-p是包名,--javaTypes也可以选择--solidityTypes
生成出来Greeter.java
1 import org.web3j.abi.FunctionEncoder; 2 import org.web3j.abi.TypeReference; 3 import org.web3j.abi.datatypes.Function; 4 import org.web3j.abi.datatypes.Type; 5 import org.web3j.abi.datatypes.Utf8String; 6 import org.web3j.crypto.Credentials; 7 import org.web3j.protocol.Web3j; 8 import org.web3j.protocol.core.RemoteCall; 9 import org.web3j.protocol.core.methods.response.TransactionReceipt; 10 import org.web3j.tx.Contract; 11 import org.web3j.tx.TransactionManager; 12 13 import java.math.BigInteger; 14 import java.util.Arrays; 15 import java.util.Collections; 16 17 public class Greeter extends Contract { 18 private static final String BINARY = "6060604052341561000f57600080fd5b6040516103203803806103208339810160405280805160008054600160a060020a03191633600160a060020a03161790559190910190506001818051610059929160200190610060565b50506100fb565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a157805160ff19168380011785556100ce565b828001600101855582156100ce579182015b828111156100ce5782518255916020019190600101906100b3565b506100da9291506100de565b5090565b6100f891905b808211156100da57600081556001016100e4565b90565b6102168061010a6000396000f30060606040526004361061004b5763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166341c0e1b58114610050578063cfae321714610065575b600080fd5b341561005b57600080fd5b6100636100ef565b005b341561007057600080fd5b610078610130565b60405160208082528190810183818151815260200191508051906020019080838360005b838110156100b457808201518382015260200161009c565b50505050905090810190601f1680156100e15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000543373ffffffffffffffffffffffffffffffffffffffff9081169116141561012e5760005473ffffffffffffffffffffffffffffffffffffffff16ff5b565b6101386101d8565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101ce5780601f106101a3576101008083540402835291602001916101ce565b820191906000526020600020905b8154815290600101906020018083116101b157829003601f168201915b5050505050905090565b602060405190810160405260008152905600a165627a7a723058209dd925f8a845985cc97b98b1d11e67cb72915d7316a7eeb4e28cec3f5f398c9f0029"; 19 20 protected Greeter(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { 21 super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit); 22 } 23 24 protected Greeter(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { 25 super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit); 26 } 27 28 public RemoteCall<TransactionReceipt> kill() { 29 Function function = new Function( 30 "kill", 31 Arrays.<Type>asList(), 32 Collections.<TypeReference<?>>emptyList()); 33 return executeRemoteCallTransaction(function); 34 } 35 36 public RemoteCall<String> greet() { 37 Function function = new Function("greet", 38 Arrays.<Type>asList(), 39 Arrays.<TypeReference<?>>asList(new TypeReference<Utf8String>() {})); 40 return executeRemoteCallSingleValueReturn(function, String.class); 41 } 42 43 public static RemoteCall<Greeter> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit, String _greeting) { 44 String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting))); 45 return deployRemoteCall(Greeter.class, web3j, credentials, gasPrice, gasLimit, BINARY, encodedConstructor); 46 } 47 48 public static RemoteCall<Greeter> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit, String _greeting) { 49 String encodedConstructor = FunctionEncoder.encodeConstructor(Arrays.<Type>asList(new org.web3j.abi.datatypes.Utf8String(_greeting))); 50 return deployRemoteCall(Greeter.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, encodedConstructor); 51 } 52 53 public static Greeter load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { 54 return new Greeter(contractAddress, web3j, credentials, gasPrice, gasLimit); 55 } 56 57 public static Greeter load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { 58 return new Greeter(contractAddress, web3j, transactionManager, gasPrice, gasLimit); 59 } 60 }
部署智能合约
1 import org.web3j.crypto.Credentials; 2 import org.web3j.protocol.Web3j; 3 import org.web3j.protocol.http.HttpService; 4 import org.web3j.tx.Contract; 5 6 public class SmartContractTest1 { 7 public static void main(String[] args) throws Exception { 8 Web3j web3j = Web3j.build(new HttpService("https://kovan.infura.io/<your-token>")); 9 String ownAddress = "0xD1c82c71cC567d63Fd53D5B91dcAC6156E5B96B3"; 10 String toAddress = "0x6e27727bbb9f0140024a62822f013385f4194999"; 11 Credentials credentials = Credentials.create("xxxxxxxxxxxxxxxxxxx"); 12 //部署智能合约 13 Greeter greeter = Greeter.deploy(web3j,credentials,Contract.GAS_PRICE,Contract.GAS_LIMIT,"zx").send(); 14 System.out.println(greeter.getContractAddress());
//调用智能合约 15 System.out.println(greeter.greet().send()); 16 17 } 18 }
个人意见第二种因为比较清晰