Ng Zhong Qin Ng Zhong Qin - 1 month ago 23
Node.js Question

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

I'm looking at the code example as shown in
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.

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

Answer

I never used eris, but if your question is how to compile this contract using 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):

[{"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:

/* 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.

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.

Comments