5

I am trying to deploy a contract through my front-end via web3 API. However nothing appear to compile and there is no contract address or ABI to interact with the deployed contract (if it is deployed) and retrieve information (i.e. the string "petros"). I also tried the source code as a one line string. Below is my code:

  var source = "" + 
    "contract demo {\n" +
    "   string public name = 'Petros';\n" +
    "   function changeName(string _newName){\n" +
    "     name = _newName;\n" +
    "   }\n" +
    "}\n"; 

var compiled = web3.eth.compile.solidity(source,
    function(err,res){ //necessary callback function
      console.log(JSON.stringify(res));
});
  • 1
    Now web3.eth.compile.solidity is deprecated it wont work !!!!!!! – Narendranath Reddy Aug 18 '17 at 15:44
  • It works up to go-ethereum v1.5.9. For newer versions you need to compile the solidity code outside of geth – PetrosM Aug 22 '17 at 7:30
4

First check if you have installed the Solc : solc --version Then if your code won't work Try to use the following one instead:

 var source = 'contract demo {string public name = "Petros"; function changeName(string _newName){name = _newName; } }'; 

  var compiled = web3.eth.compile.solidity(source);

   console.log(compiled);
  • Thank you for your input, i do have solc installed. The code your provided won't run since there is no callback function in web3.eth.compile.solidity(). MetaMask Web3 object does not support synchronous methods. However instead of that, my code var compiled = web3.eth.compile.solidity(source,function(err,res){console.log(res)}); compiles correctly even though i don't get a return for the console.log(compiled); command. Maybe i am on the wrong track but i want to deploy a contract and be able to interact with it. for example retrieve the string "petros" – PetrosM Jan 31 '17 at 19:53
  • I edited the question to make myself more clear and correct – PetrosM Feb 1 '17 at 11:43
2

Now the web3.eth.compile.solidity has been removed. It does not work. There is a error if you call it.

Error: The method eth_compileSolidity does not exist/is not available

Please check the page. https://github.com/ethereum/web3.js/issues/1000 And you can use Remix , or solcjs in nodejs , or solc binary. Refer to the doc, http://solidity.readthedocs.io/en/develop/installing-solidity.html

0

ANSWER:

var Web3 = require('web3');
var web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
var source = 'contract demo {string public name = "Petros"; function changeName(string _newName){name = _newName; } }'; 
var compiled = web3.eth.compile.solidity(source);
var abi = compiled['<stdin>:demo'].info.abiDefinition;
  • What was the problem and how your answer fix it ? Thanks! – Laxmana Jul 15 '17 at 15:16
  • @Laxmana, i dont remember exactly what the issue was since a lot of months past and i finished that project but i think it was not connected to an Ethereum node so it couldnt deploy the contract. so by setting the web3 provider it worked. Note that in the newer version of geth the solc compiler is not included in geth so probably this code wont work anyway. – PetrosM Jul 18 '17 at 8:53

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.