7

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

If you have the SolC Solidity Compiler installed, you need now reformat by removing spaces so it fits into a string variable.

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 without stripping the line break manually.

Does anyone know how to do that?

8

My advice would be to use the Browser Solidity:

Just copy paste your contract, and if it compiles without errors you can just copy past the content of Web3 deploy:

var _numProposals = /* var of type uint8 here */ ;
var ballotContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"to","type":"address"}],"name":"delegate","outputs":[],"type":"function"},{"constant":true,"inputs":[],"name":"winningProposal","outputs":[{"name":"winningProposal","type":"uint8"}],"type":"function"},{"constant":false,"inputs":[{"name":"voter","type":"address"}],"name":"giveRightToVote","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"proposal","type":"uint8"}],"name":"vote","outputs":[],"type":"function"},{"inputs":[{"name":"_numProposals","type":"uint8"}],"type":"constructor"}]);
var ballot = ballotContract.new(
   _numProposals,
   {
     from: web3.eth.accounts[0], 
     data: '60606040*deleted-for-lsibility-6', 
     gas: 3000000
   }, function(e, contract){
    console.log(e, contract);
    if (typeof contract.address != 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

Don't forget that :

  • It's javascript ( so address is string )
  • You need to fill your params var _numProposals = /* var of type uint8 here */ ; here.

--

Another solution is Mix IDE :

Click on Deploy then click on Open Compilation Result and it will provide you with a JS file. Inside there are objects representing your contract. You would then need to call web3.eth.contract(Mycontract.abi)

3

Heres a javascript command that will strip newlines

xmlStr = xmlStr .replace(/\n/gi, "");  // strip newlines

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.