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" +

var compiled = web3.eth.compile.solidity(source,
    function(err,res){ //necessary callback function
  • 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

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);

  • 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

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



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

Since web3.eth.compile.solidity has been deprecated, thus, we need to deploy our smart contracts outside of geth. This can be achieved by using solc.

Deploying contract using solc:

npm i solc

Create a file for deploying: deploy_Hello_World.js

const fs = require('fs');
const solc = require('solc');
const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider(http://localhost:8545));
const input = fs.readFileSync('/path/to/HelloWorld.sol');
const output = solc.compile(input.toString(), 1);
const bytecode = output.contracts[':HelloWorld'].bytecode;
const abi = JSON.parse(output.contracts[':HelloWorld'].interface);
const gasEstimate = web3.eth.estimateGas({ data: '0x' + bytecode });
const contract = web3.eth.contract(abi);

contract.new({ data: '0x' + bytecode, from: web3.eth.coinbase, gas: gasEstimate }, (err, res) => {
    if (err) {
    } else {
        if (res.address) {
            console.log('Deployed contract address: ' + res.address);

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.