4
var input = "contract x { function g() {} }";
var output = solc.compile(input, 1); // 1 activates the optimiser
for (var contractName in output.contracts) {
    // code and ABI that are needed by web3
    console.log(contractName + ': ' + output.contracts[contractName].bytecode);
    console.log(contractName + '; ' + JSON.parse( output.contracts[contractName].interface));
}

Everything is working out, but when I get lost the last part, I'm unsure of what values to input in the first parentheses, and the //code and ABI that are needed by web3. Can you please help?

for (var contractName in output.contracts) {
    // code and ABI that are needed by web3
    console.log(contractName + ': ' + output.contracts[contractName].bytecode);
    console.log(contractName + '; ' + JSON.parse( output.contracts[contractName].interface));
}
  • "unsure of what values to input in the first parentheses" Hi, what's the question, which parentheses? If talking about for (var contractName in output.contracts) there is nothing to change there. The comment // is also just a comment, no need to write any code there. – eth Apr 22 '16 at 0:54
6

Source Code, Bytecode and Application Binary Interface

If you plug your source code contract x { function g() {} } into the left hand pane of the Solidity Online Compiler, you will see on the right hand side a Bytecode text box which contains the binary representation of your source code. This is what output.contracts[contractName].bytecode will contain, and from the online compiler is:

606060405260478060106000396000f360606040526000357c010000000000000000000000000000000000000000000000000000000090048063e2179b8e146037576035565b005b604260048050506044565b005b5b56

You will also find an Interface text box on the right hand side containing the Application Binary Interface. This is what output.contracts[contractName].interface will contain, and from the online compiler this is:

[{"constant":false,"inputs":[],"name":"g","outputs":[],"type":"function"}]


Compilation of Source Code

The following statement assigns your contract source to the variable input:

var input = "contract x { function g() {} }";

The following statement calls the Solidity compiler to compile your source code and store the resulting information into the variable output:

var output = solc.compile(input, 1); // 1 activates the optimiser


Other Information

I was searching for a bit more information on Solidity and node.js, and found your example from chriseth/browser-solidity . Remember to install your Solidity compiler for use by node.js using the statement:

npm install solc
|improve this answer|||||
0

Took me a little while to figure this one out and @BokkyPooBah's answer certainly helped. However here is some code that works for me right now.

var contract = output.contracts[':' + myContractName];
var abi = JSON.parse(contract.interface);
var code = '0x' + contract.bytecode;

Note that:

  • The contract name will be prefixed with : in the contracts hash (this doesn't matter if looping through property names as in the example, but is a pain when you're interested in a particular contract)

  • The interface returned in the compiled contract is a JSON string and not an array as required by web3.

  • The bytecode field is missing the 0x hex prefix that is required by web3.

Bonus note: I found the solc-js compiler orders of magnitude slower than using the binary compiler through web3 and a running node so I'm not sure I would recommend using it. In fact I think I would recommend just using the solc binary directly.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.