Solidity编译器和简单调试

栏目: 服务器 · 编程工具 · 发布时间: 6年前

内容简介:1 安装搜索docker的Solc镜像docker search –no-trunc ethereum/solc

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中添加如下

1web3j.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") 3public 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 调用 智能合约 编译器的 代码

1String soliditySrcCode =MapUtils.getString(preParam, "soliditySrcCode"); 2if(StringUtils.isBlank(soliditySrcCode)) { 3 param.put(ContractConstant.RETURN_CODE, ContractConstant.ERROR_CODE); 4 param.put(ContractConstant.RETURN_MSG, ContractConstant.NOSRCCODE); 5 return param; 6} 7SolidityCompiler solidityCompiler = SolidityCompiler.getInstance(getLog(),contractConfig.getSolcCmd()); 8byte[] source = soliditySrcCode.getBytes(StandardCharsets.UTF_8); 9CompilerResult compilerResult = solidityCompiler.compileSrc(source, 10SolidityCompiler.Options.ABI, SolidityCompiler.Options.BIN); 11param.put(ContractConstant.RETURN_CODE, ContractConstant.SUCCESS_CODE); 12param.put(ContractConstant.RETURN_MSG, ContractConstant.SUCCESS_MSG); 13if(compilerResult.isFailed()) { 14 param.put(ContractConstant.RETURN_CODE, ContractConstant.ERROR_CODE); 15 param.put(ContractConstant.RETURN_MSG, compilerResult.getErrors()); 16}

2.3 调用编译智能合约源文件的代码

2.3.1 编写智能合约源码

1contract SampleRecipientSuccess { 2 address public from; 3 uint256 public value; 4 address public tokenContract; 5 bytes public extraData; 6 event ReceivedApproval(uint256 _value); 7 function receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData) { 8 from = _from; 9 value = _value; 10 tokenContract = _tokenContract; 11 extraData = _extraData; 12 ReceivedApproval(_value); 13 } 14}

2.3.2 通过HTTP调用智能合约的J2EE组件

1String contractPath="/SampleRecipientSuccess.sol"; 2 String contractString = FileUtils.readFileToString(new File(contractPath),StandardCharsets.UTF_8); 3 HashMap<String, Object> hashMap = new HashMap<String,Object>(); 4 hashMap.put("soliditySrcCode", contractString); 5 String url = "http://192.168.3.43:18080/ContractCompile/compileContractCmd"; 6 ResponseEntity<Map> postForEntity = getRestTemplate().postForEntity(url, hashMap, Map.class); 7 Map body = postForEntity.getBody(); 8 String returnCode = MapUtils.getString(body, ContractConstant.RETURN_CODE); 9 if(ContractConstant.SUCCESS_CODE.equals(returnCode)) { 10 String result = MapUtils.getString(body, "result"); 11 Map<String, Object> parseResult = parseResult(result); 12 System.out.println(AppObjectUtil.toJson(parseResult)); 13 }

2.3.3 智能合约编译器组件返回的编译数据

1{ 2 "abis": [{ 3 "constant": true, 4 "inputs": [], 5 "name": "value", 6 "outputs": [{ 7 "name": "", 8 "type": "uint256" 9 }], 10 "payable": false, 11 "type": "function" 12 }, { 13 "constant": true, 14 "inputs": [], 15 "name": "tokenContract", 16 "outputs": [{ 17 "name": "", 18 "type": "address" 19 }], 20 "payable": false, 21 "type": "function" 22 }, { 23 "constant": true, 24 "inputs": [], 25 "name": "extraData", 26 "outputs": [{ 27 "name": "", 28 "type": "bytes" 29 }], 30 "payable": false, 31 "type": "function" 32 }, { 33 "constant": false, 34 "inputs": [{ 35 "name": "_from", 36 "type": "address" 37 }, { 38 "name": "_value", 39 "type": "uint256" 40 }, { 41 "name": "_tokenContract", 42 "type": "address" 43 }, { 44 "name": "_extraData", 45 "type": "bytes" 46 }], 47 "name": "receiveApproval", 48 "outputs": [], 49 "payable": false, 50 "type": "function" 51 }, { 52 "constant": true, 53 "inputs": [], 54 "name": "from", 55 "outputs": [{ 56 "name": "", 57 "type": "address" 58 }], 59 "payable": false, 60 "type": "function" 61 }, { 62 "anonymous": false, 63 "inputs": [{ 64 "indexed": false, 65 "name": "_value", 66 "type": "uint256" 67 }], 68 "name": "ReceivedApproval", 69 "type": "event" 70 }], 71 "bin": 72 "6060604052341561000c57fe5b5b6103d38061001c6000396000f300606060405263ffffffff60e 73 060020a6000350416633fa4f245811461004d57806355a373d61461006f578063609d33341461009 74 b5780638f4ffcb11461012b578063d5ce338914610199575bfe5b341561005557fe5b61005d6101c 75 5565b60408051918252519081900360200190f35b341561007757fe5b61007f6101cb565b6040805 76 1600160a060020a039092168252519081900360200190f35b34156100a357fe5b6100ab6101da565 77 b6040805160208082528351818301528351919283929083019185019080838382156100f1575b805 78 1825260208311156100f157601f1990920191602091820191016100d1565b5050509050908101906 79 01f16801561011d5780820380516001836020036101000a031916815260200191505b50925050506 80 0405180910390f35b341561013357fe5b604080516020600460643581810135601f8101849004840 81 285018401909552848452610197948235600160a060020a039081169560248035966044359093169 82 594608494929391019190819084018382808284375094965061026895505050505050565b005b341 83 56101a157fe5b61007f6102f8565b60408051600160a060020a03909216825251908190036020019 84 0f35b60015481565b600254600160a060020a031681565b600380546040805160206002600185161 85 5610100026000190190941693909304601f810184900484028201840190925281815292918301828 86 280156102605780601f1061023557610100808354040283529160200191610260565b82019190600 87 0526020600020905b81548152906001019060200180831161024357829003601f168201915b50505 88 0505081565b60008054600160a060020a0380871673fffffffffffffffffffffffffffffffffffff 89 fff19928316179092556001859055600280549285169290911691909117905580516102bd9060039 90 06020840190610307565b506040805184815290517f2db24179b782aab7c5ab64add7f84d4f6c845 91 d0779695371f29be1f658d043cd9181900360200190a15b50505050565b600054600160a060020a0 92 31681565b828054600181600116156101000203166002900490600052602060002090601f0160209 93 00481019282601f1061034857805160ff1916838001178555610375565b828001600101855582156 94 10375579182015b8281111561037557825182559160200191906001019061035a565b5b506103829 95 29150610386565b5090565b6103a491905b80821115610382576000815560010161038c565b50905 96 65b905600a165627a7a723058209522849948e8cc25a7d6717d5c10836c97c36425936be5edf3992 97 06b3e5d7fa30029" 98}

总结

通过J2EE组件的接口调用,可以为大多数基于 java 的区块链 应用 提供了便利,可以利用J2EE成熟稳定的框架无缝集成到项目中,也是为了以后安卓开发和联盟链提供在线编译智能合约功能,如果是私有的局域网络的企业级联盟链,可以发布该智能合约的J2EE组件到该局域网的机器上去,可以实现联盟中的智能合约统一的编译器,便于快速升级编译器。

感谢 HPB 团队整理。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Charlotte's Web

Charlotte's Web

E. B. White / Puffin Classics / 2010-6-3 / GBP 6.99

This is the story of a little girl named Fern who loved a little pig named Wilbur and of Wilbur's dear friend, Charlotte A. Cavatica, a beautiful large grey spider. With the unlikely help of Templeton......一起来看看 《Charlotte's Web》 这本书的介绍吧!

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具