Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in Blockchain by (4.1k points)

I'm new to write a smart contract with Ethereum.

According to an official document, compiling a smart contract needs to remove all the line-breaks in the contract's source-code :

var greeterSource = 'contract mortal { address owner; function mortal() { owner = msg.sender; } function kill() { if (msg.sender == owner) suicide(owner); } } contract greeter is mortal { string greeting; function greeter(string _greeting) public { greeting = _greeting; } function greet() constant returns (string) { return greeting; } }'
var greeterCompiled = web3.eth.compile.solidity(greeterSource)

As I think the removing process is not smart, I want to compile the code itself like:

var greeterCompiled = web3.eth.compile.solidity_infile( "greeter.txt" )
# The function "solidity_infile" does not exists actually,
# but represents what I want to do.



mortal {
    /* Define variable owner of the type address*/
    address owner;

    /* this function is executed at initialization and sets the owner of the contract */
    function mortal() { owner = msg.sender; }

    /* Function to recover the funds on the contract */
    function kill() { if (msg.sender == owner) suicide(owner); }

contract greeter is mortal {
    /* define variable greeting of the type string */
    string greeting;

    /* this runs when the contract is executed */
    function greeter(string _greeting) public {
        greeting = _greeting;

    /* main function */
    function greet() constant returns (string) {
        return greeting;


Does anyone how to do that?

The compiler I'm using is Solidity.

1 Answer

0 votes
by (14.4k points)

You can fetch the contract from a file and load it into var someContractText. After that use the following line of code: 

someContractText = someContractText.replace(/(\r\n|\n|\r)/gm,"");

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories