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

时间:2016-10-09 13:41:28

标签: node.js ethereum solidity smartcontracts

我正在查看代码示例,如图所示 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);

3 个答案:

答案 0 :(得分:3)

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

编译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
})

答案 1 :(得分:1)

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

pragma solidity ^0.4.0;

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

你试过browser-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编译可靠性,那么这应该可以解决。

答案 2 :(得分:1)

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

 $('.marketing_event_id').editable({
            emptytext: ".....",
            container: 'body',  <---- THIS IS WHAT I ADDED TO MAKE IT WORK!!!!
            url: "ajax_xeditable_update.php?table=appointments",
            source: promo_codes,
            select2: {
                width: 200,
                placeholder: 'Select promotion code...',
                allowClear: true,
            }
        });

编译源

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