注意
以下内容为Mac环境下测试的,windows也是类似的。
准备工具
- solc环境
npm install -g solc
- web3j环境
brew tap web3j/web3j
brew install web3j
编写合约
我这里准备了一个例子:SimpleStorage.sol
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData;
function set(uint num){
storedData = num;
}
function get() constant returns (uint retVal){
return storedData;
}
}
编译合约(sol)
solc <contract>.sol --bin --abi --optimize -o <output-dir>
例如:
solc SimpleStorage.sol --bin --abi --optimize -o /Users/zx/Desktop
<contract>.sol
:是sol合约文件所在的目录,我的sol在Mac桌面上。
<output-dir>
:编译生成的bin、abi文件的输出目录
比如,我这里就在桌面上生成了2个文件SimpleStorage.bin
和SimpleStorage.abi
生成 .java文件(web3j)
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
例如:
web3j solidity generate --javaTypes /Users/zx/Desktop/SimpleStorage.bin /Users/zx/Desktop/SimpleStorage.abi -o /Users/zx/Desktop -p com.example.zhouxu.ethwallet.contract
/path/to/<smart-contract>.bin
:上一步生成的bin文件的目录
/path/to/<smart-contract>.abi
:上一步生成的abi文件的目录
/path/to/src/main/java
:生成的java文件的输出目录
com.your.organisation.name
:生成的java文件的包名
生成的java文件
/**
* <p>Auto generated code.
* <p><strong>Do not modify!</strong>
* <p>Please use the <a href="https://docs.web3j.io/command_line.html">web3j command line tools</a>,
* or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the
* <a href="https://github.com/web3j/web3j/tree/master/codegen">codegen module</a> to update.
*
* <p>Generated with web3j version 3.6.0.
*/
public class SimpleStorage extends Contract {
private static final String BINARY = "608060405234801561001057600080fd5b5060bf8061001f6000396000f30060806040526004361060485763ffffffff7c010000000000000000000000000000000000000000000000000000000060003504166360fe47b18114604d5780636d4ce63c146064575b600080fd5b348015605857600080fd5b5060626004356088565b005b348015606f57600080fd5b506076608d565b60408051918252519081900360200190f35b600055565b600054905600a165627a7a7230582023172d55cc4cc6aeec159e78e69150d6a73cadc17eebbf2ddaaffcdf042ce3a50029";
public static final String FUNC_SET = "set";
public static final String FUNC_GET = "get";
@Deprecated
protected SimpleStorage(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, credentials, gasPrice, gasLimit);
}
@Deprecated
protected SimpleStorage(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
super(BINARY, contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
public RemoteCall<TransactionReceipt> set(BigInteger num) {
final Function function = new Function(
FUNC_SET,
Arrays.<Type>asList(new Uint256(num)),
Collections.<TypeReference<?>>emptyList());
return executeRemoteCallTransaction(function);
}
public RemoteCall<BigInteger> get() {
final Function function = new Function(FUNC_GET,
Arrays.<Type>asList(),
Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}));
return executeRemoteCallSingleValueReturn(function, BigInteger.class);
}
@Deprecated
public static RemoteCall<SimpleStorage> deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(SimpleStorage.class, web3j, credentials, gasPrice, gasLimit, BINARY, "");
}
@Deprecated
public static RemoteCall<SimpleStorage> deploy(Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return deployRemoteCall(SimpleStorage.class, web3j, transactionManager, gasPrice, gasLimit, BINARY, "");
}
@Deprecated
public static SimpleStorage load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) {
return new SimpleStorage(contractAddress, web3j, credentials, gasPrice, gasLimit);
}
@Deprecated
public static SimpleStorage load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) {
return new SimpleStorage(contractAddress, web3j, transactionManager, gasPrice, gasLimit);
}
}
合约交互
生成合约的java文件之后,就把它放入项目对应的包下面,然后可以利用Web3j和合约交互了。所谓的交互就是java/Android调用刚才生成的这个合约的一些方法,具体的细节,我们下一篇再讲解。
网友评论