如何使用eris的javascript编译一个Solidity代码?

我正在查看代码示例,如https://github.com/eris-ltd/eris-contracts.js

var myAbi = [...]; var myCompiledCode = "..."; // Create a factory for the contract with the JSON interface 'myAbi'. var myContractFactory = contractManager.newContractFactory(myAbi); // To create a new instance and simultaneously deploy a contract use `new`: var myNewContract; myContractFactory.new({data: myCompiledCode}, function(error, contract){ if (error) { // Something. throw error; } myNewContract = contract; }); 

但我不知道如何编译。 我知道eris-contracts.js是build立在web3.js上的,但是我不确定在实例化web3对象时我需要input什么东西。

 var edbFactory = require('eris-db'); var Web3 = require('web3'); var web3 = new Web3(); web3.setProvider(new web3.providers.HttpProvider('http://simplechain:1337/rpc')); var edb = edbFactory.createInstance("http://simplechain:1337/rpc"); var source = "" + "contract test {\n" + " function multiply(uint a) returns(uint d) {\n" + " return a * 7;\n" + " }\n" + "}\n"; var compiled = web3.eth.compile.solidity(source); console.log(compiled); 

我来自厄里斯。 对不起,我们的文档不是很清楚。

编译Solidity的最简单方法是使用Solidity编译器的JavaScript绑定 。

$ npm install solc –save

 const Solidity = require('solc') var source = "" + "contract test {\n" + " function multiply(uint a) returns(uint d) {\n" + " return a * 7;\n" + " }\n" + "}\n"; const compiled = Solidity.compile(source, 1).contracts.test const abi = JSON.parse(compiled.interface) const contractFactory = contractManager.newContractFactory(abi) contractFactory.new({data: compiled.bytecode}, (error, contract) => { // use contract here }) 

我从来没有使用过eris,但是如果你的问题是如何使用javascript编译这个合约:

 pragma solidity ^0.4.0; contract test { function multiply(uint a) returns(uint d) { return a * 7; } } 

你尝试过浏览器的坚实 ? 它立即编译您的浏览器中的固体代码。 上面的可靠代码的编译合同是:

 606060405260788060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146039576035565b6002565b34600257605160048080359060200190919050506067565b6040518082815260200191505060405180910390f35b60006007820290506073565b91905056 

和界面( ABI ):

 [{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"type":"function"}] 

要使用web3js进行部署,请使用以下代码:

 /* the test contract interface */ var testContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"payable":false,"type":"function"}]); /* deploy it with web3, here: on ethereum */ var test = testContract.new( { from: web3.eth.accounts[0], data: '606060405260788060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146039576035565b6002565b34600257605160048080359060200190919050506067565b6040518082815260200191505060405180910390f35b60006007820290506073565b91905056', gas: 4700000 }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } }) 

我不确定是否自下而上回答你的问题。 如果问题是您需要有效的JSON-HTTP提供程序,则可以运行本地geth节点并指向RPC端口,默认情况下该端口是localhost:8545

对不起,我不能回答这个与eris一致,但是如果你想用web3js编译可靠性,这应该工作。

我最终通过使用规定的编译器在eris链上解决了我自己的问题。 但我似乎无法find像web3提供的js编译器。 在ubuntu上安装eris-compiler

 sudo apt-get install golang export GOPATH=$HOME/go export PATH=$PATH:$GOROOT/bin:$GOPATH/bin go get github.com/eris-ltd/eris-compilers/cmd/eris-compilers sudo add-apt-repository ppa:ethereum/ethereum sudo add-apt-repository ppa:ethereum/ethereum-dev sudo apt-get update sudo apt-get install lllc sc solc sudo apt-get install solc 

编译源码

 eris-compilers --debug compile -s -u compilers.monax.io -p 10120 idi.sol 

编译的产品将是这样的:

 ngzhongqin@server2:/prodlib/ERIS/.eris/apps/idi-service$ eris-compilers -- debug compile -s -u compilers.monax.io -p 10120 idi.sol Cached Item(s) cached?=false Could not find cached object, compiling... Response abi=[{"constant":true,"inputs":[],"name":"getName","outputs":[{"name":"retVal","type":"string"}],"type":"function"},{"constant":false,"inputs":[{"name":"x","type":"uint256"}],"name":"set","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"get","outputs":[{"name":"retVal","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"}],"name":"setName","outputs":[],"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":false,"name":"_name","type":"string"}],"name":"SetName","type":"event"}] bin= name=IdisContractsFTW ngzhongqin@server2:/prodlib/ERIS/.eris/apps/idi-service$