【问题标题】:How do I compile a piece of Solidity Code using eris' javascript?如何使用 eris 的 javascript 编译一段 Solidity 代码?
【发布时间】:2017-02-18 01:10:46
【问题描述】:

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

【问题讨论】:

    标签: node.js ethereum solidity smartcontracts


    【解决方案1】:

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

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

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

    606060405260788060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa1146039576035565b6002565b34600257605160048080359060200190919050506067565b6040518082815260200191505060405180910390f35b60006007820290506073565b91905056
    

    还有接口(@98​​7654322@):

    [{"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,这应该可以解决。

    【讨论】:

    • "我不确定我是否自下而上回答了您的问题。如果问题是您需要有效的 JSON-HTTP 提供程序,您可以运行本地 geth 节点并指向 RPC 端口,默认为 localhost:8545。” -- 我想这可能是我的问题的答案。
    • 我试过使用浏览器编译器。虽然代码在创建新合约时接受了编译后的源代码,但我在使用合约中的方法时遇到了错误。
    【解决方案2】:

    我最终通过使用他们规定的编译器解决了我自己在 eris 链上的问题。但是我似乎找不到像 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=6060604052610399806100126000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806317d7de7c1461005a57806360fe47b1146100d55780636d4ce63c146100ed578063c47f00271461011057610058565b005b61006760048050506102dd565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156100c75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100eb6004808035906020019091905050610166565b005b6100fa6004805050610174565b6040518082815260200191505060405180910390f35b6101646004808035906020019082018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050909091905050610186565b005b806000600050819055505b50565b60006000600050549050610183565b90565b8060016000509080519060200190828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106101d557805160ff1916838001178555610206565b82800160010185558215610206579182015b828111156102055782518260005055916020019190600101906101e7565b5b5090506102319190610213565b8082111561022d5760008181506000905550600101610213565b5090565b50503373ffffffffffffffffffffffffffffffffffffffff167f5b55a57845097b0b4cf682f6d089fe6bad81dc28242927eca133f4b1c4d28b448260405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600f02600301f150905090810190601f1680156102cc5780820380516001836020036101000a031916815260200191505b509250505060405180910390a25b50565b602060405190810160405280600081526020015060016000508054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561038a5780601f1061035f5761010080835404028352916020019161038a565b820191906000526020600020905b81548152906001019060200180831161036d57829003601f168201915b50505050509050610396565b9056
                                             name=IdisContractsFTW
    ngzhongqin@server2:/prodlib/ERIS/.eris/apps/idi-service$ 
    

    【讨论】:

      【解决方案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
      })
      

      【讨论】:

      • 作为参考,eris是否提供了一个可以用作HttpProvider的节点?
      • 没有。我们正在探索在未来提供一个 web3 兼容层。
      猜你喜欢
      • 1970-01-01
      • 2021-07-11
      • 2019-06-22
      • 2018-05-13
      • 1970-01-01
      • 2011-11-08
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      相关资源
      最近更新 更多