1 安装Solidity编译器
1.1 通过docker安装Solc
搜索docker的Solc镜像
docker search —no-trunc ethereum/solc
通过docker安装Solc
docker pull docker.io/ethereum/solc:stable
1.2 运行Solc容器
运行如下命令
docker run —rm -it —privileged=true —net=host -v /home/hpbroot/ethereum_go/contract:/contract —name solc ethereum/solc:stable –version
查看是否成功
2 新建spring boot工程
2.1 通过Eclipse新建工程
首先,新建ContractCompile工程
在springboot配置文件application.properties中添加如下
1 web3j.contract.solcCmd=docker run --rm -it --privileged=true --net=host -v /home/hpbroot/ethereum_go/contract:/contract --name solc ethereum/solc:stable
2 在pom文件添加依赖
3 <dependency>
4 <groupId>org.ethereum</groupId>
5 <artifactId>solcJ-all</artifactId>
6 <version>0.4.10</version>
7 </dependency>
新建ContractConfig类
1 @Component
2 @ConfigurationProperties(prefix = "web3j.contract")
3 public class ContractConfig {
4 private String solcCmd;
5 public String getSolcCmd() {
6 return solcCmd;
7 }
8 public void setSolcCmd(String solcCmd) {
9 this.solcCmd = solcCmd;
10 }
11 }
2.2 调用智能合约编译器的代码
1 String soliditySrcCode =MapUtils.getString(preParam, "soliditySrcCode");
2 if(StringUtils.isBlank(soliditySrcCode)) {
3 param.put(ContractConstant.RETURN_CODE, ContractConstant.ERROR_CODE);
4 param.put(ContractConstant.RETURN_MSG, ContractConstant.N