1

I want to compile my solidity code (smart contract) inside my node.js API using the solc library to get the ABI and bytecode of my solidity.

What I want in details is an input that can read solidity code "just like a text" from a file. I want to be able to compile multiple smart contracts.

Can anyone provide me with an example?

solc #nodejs #solidity #javascript

1 Answer 1

2

Please read the documentation here and here

var solc = require('solc')
var input = 'contract x { function g() {} }'
// Setting 1 as second parameter activates the optimiser
var optimize = 1
var output = solc.compile(input, optimize)
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)
}

you can read the content of the contract from a file also:

var fs = require("fs");
var input = fs.readFileSync('<file path here>', 'utf8');
// rest of the code

If you have any specific question post your code

2
  • Thanks but what I want is the input that can read the solidity code from a file. I want to create an input for solidity code so I can compile multiple smart contracts. May 23, 2018 at 9:59
  • 2
    read ny updated answer, just use fs (base Node.js module) to read the file
    – qbsp
    May 23, 2018 at 10:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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