我如何使用eris的javascript编译一个完整的代码?

时间:2022-09-23 01:06:21

I'm looking at the code example as shown in https://github.com/eris-ltd/eris-contracts.js

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

But I've no idea how to do the compilation. I understand that the eris-contracts.js is built on web3.js But I'm not sure what the provider I've got to enter when instantiating web3 object.

但是我不知道怎么编译。我知道这是合同。js构建于web3之上。但是我不确定在实例化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 个解决方案

#1


3  

I'm from Eris. I'm sorry our documentation isn't more clear.

我来自厄里斯。很抱歉,我们的文件不够清楚。

The easiest way to compile Solidity is to use the JavaScript bindings for the Solidity compiler.

编译可靠度的最简单方法是为可靠度编译器使用JavaScript绑定。

$ npm install solc --save

$ npm安装solc—保存

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

#2


1  

I never used eris, but if your question is how to compile this contract using javascript:

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

pragma solidity ^0.4.0;

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

Have you tried the browser-solidity? It instantly compiles the solidity code in your browser. The compiled contract for the solidity code above is:

你试过这种浏览器的可靠性吗?它立即在浏览器中编译实体代码。上述《固体规范》的编制合同如下:

606060405260788060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146039576035565b6002565b34600257605160048080359060200190919050506067565b6040518082815260200191505060405180910390f35b60006007820290506073565b91905056

And the interface (ABI):

和接口(ABI):

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

To deploy it with web3js, use the following code:

要使用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);
    }
 })

I'm unsure if I answered your question bottom-up. If the issue is that you need a valid JSON-HTTP provider, you could run a local geth node and point to the RPC port, which is localhost:8545 by default.

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

I'm sorry I can not answer this in line with eris, but if you want to compile solidity with web3js, this should work out.

很抱歉,我不能用eris来回答这个问题,但是如果您想用web3js编译solid,这应该是可行的。

#3


1  

I eventually solved my own issue on the eris chain by using their prescribed compiler. But I can't seem to find a js compiler like the one offered within web3. To install the eris-compiler on ubuntu

我最终通过使用指定的编译器解决了我自己在eris链上的问题。但我似乎找不到像web3那样的js编译器。在ubuntu上安装ie编译器

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

To compile the source

编译源代码

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

The compiled product will be something like that:

编译后的产品会是这样的:

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$ 

#1


3  

I'm from Eris. I'm sorry our documentation isn't more clear.

我来自厄里斯。很抱歉,我们的文件不够清楚。

The easiest way to compile Solidity is to use the JavaScript bindings for the Solidity compiler.

编译可靠度的最简单方法是为可靠度编译器使用JavaScript绑定。

$ npm install solc --save

$ npm安装solc—保存

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

#2


1  

I never used eris, but if your question is how to compile this contract using javascript:

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

pragma solidity ^0.4.0;

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

Have you tried the browser-solidity? It instantly compiles the solidity code in your browser. The compiled contract for the solidity code above is:

你试过这种浏览器的可靠性吗?它立即在浏览器中编译实体代码。上述《固体规范》的编制合同如下:

606060405260788060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146039576035565b6002565b34600257605160048080359060200190919050506067565b6040518082815260200191505060405180910390f35b60006007820290506073565b91905056

And the interface (ABI):

和接口(ABI):

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

To deploy it with web3js, use the following code:

要使用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);
    }
 })

I'm unsure if I answered your question bottom-up. If the issue is that you need a valid JSON-HTTP provider, you could run a local geth node and point to the RPC port, which is localhost:8545 by default.

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

I'm sorry I can not answer this in line with eris, but if you want to compile solidity with web3js, this should work out.

很抱歉,我不能用eris来回答这个问题,但是如果您想用web3js编译solid,这应该是可行的。

#3


1  

I eventually solved my own issue on the eris chain by using their prescribed compiler. But I can't seem to find a js compiler like the one offered within web3. To install the eris-compiler on ubuntu

我最终通过使用指定的编译器解决了我自己在eris链上的问题。但我似乎找不到像web3那样的js编译器。在ubuntu上安装ie编译器

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

To compile the source

编译源代码

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

The compiled product will be something like that:

编译后的产品会是这样的:

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$