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

How do I compile a piece of Solidity Code using eris' javascript?

我正在查看中所示的代码示例 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 是建立在 web3.js 上的 但是我不确定在实例化 web3 对象时必须输入什么提供程序。

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);

我从未使用过 eris,但如果您的问题是如何使用 javascript 编译此合约:

pragma solidity ^0.4.0;

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

你试过browser-solidity了吗?它会立即在您的浏览器中编译 solidity 代码。上面solidity代码的编译合约是:

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编译solidity,这个应该可以。

我最终使用他们指定的编译器解决了我自己在 eris 链上的问题。但我似乎找不到像 web3 中提供的那样的 js 编译器。在 ubuntu

上安装 eris 编译器
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=6060604052610399806100126000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806317d7de7c1461005a57806360fe47b1146100d55780636d4ce63c146100ed578063c47f00271461011057610058565b005b61006760048050506102dd565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156100c75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100eb6004808035906020019091905050610166565b005b6100fa6004805050610174565b6040518082815260200191505060405180910390f35b6101646004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050909091905050610186565b005b806000600050819055505b50565b60006000600050549050610183565b90565b8060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106101d557805160ff1916838001178555610206565b82800160010185558215610206579182015b828111156102055782518260005055916020019190600101906101e7565b5b5090506102319190610213565b8082111561022d5760008181506000905550600101610213565b5090565b50503373ffffffffffffffffffffffffffffffffffffffff167f5b55a57845097b0b4cf682f6d089fe6bad81dc28242927eca133f4b1c4d28b448260405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156102cc5780820380516001836020036101000a031916815260200191505b509250505060405180910390a25b50565b602060405190810160405280600081526020015060016000508054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561038a5780601f1061035f5761010080835404028352916020019161038a565b820191906000526020600020905b81548152906001019060200180831161036d57829003601f168201915b50505050509050610396565b9056
                                         name=IdisContractsFTW
ngzhongqin@server2:/prodlib/ERIS/.eris/apps/idi-service$ 

我来自阋神星。很抱歉我们的文档不够清晰。

编译 Solidity 最简单的方法是使用 JavaScript bindings for the Solidity compiler.

$ 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
})