以太坊Web3j命令行生成Java版本的智能合约

注意

以下内容为Mac环境下测试的,windows也是类似的。

准备工具

solc环境安装
web3j命令行工具安装

  • 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 .sol --bin --abi --optimize -o
例如:
solc SimpleStorage.sol --bin --abi --optimize -o /Users/zx/Desktop

.sol:是sol合约文件所在的目录,我的sol在Mac桌面上。
:编译生成的bin、abi文件的输出目录

比如,我这里就在桌面上生成了2个文件SimpleStorage.binSimpleStorage.abi

生成 .java文件(web3j)

web3j solidity generate [--javaTypes|--solidityTypes] /path/to/.bin /path/to/.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/.bin:上一步生成的bin文件的目录
/path/to/.abi:上一步生成的abi文件的目录
/path/to/src/main/java:生成的java文件的输出目录
com.your.organisation.name:生成的java文件的包名

生成的java文件

/**
 * 

Auto generated code. *

Do not modify! *

Please use the web3j command line tools, * or the org.web3j.codegen.SolidityFunctionWrapperGenerator in the * codegen module to update. * *

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 set(BigInteger num) { final Function function = new Function( FUNC_SET, Arrays.asList(new Uint256(num)), Collections.>emptyList()); return executeRemoteCallTransaction(function); } public RemoteCall get() { final Function function = new Function(FUNC_GET, Arrays.asList(), Arrays.>asList(new TypeReference() {})); return executeRemoteCallSingleValueReturn(function, BigInteger.class); } @Deprecated public static RemoteCall deploy(Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { return deployRemoteCall(SimpleStorage.class, web3j, credentials, gasPrice, gasLimit, BINARY, ""); } @Deprecated public static RemoteCall 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调用刚才生成的这个合约的一些方法,具体的细节,我们下一篇再讲解。

你可能感兴趣的:(以太坊Web3j命令行生成Java版本的智能合约)