Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
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)
https://ethereum.gitbooks.io/frontier-guide/content/contract_greeter.html

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.

 

greeter.txt

contract
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,"");

Browse Categories

...