27

I tried to compile the following contract within geth:

pragma solidity ^0.4.8;

contract Test {
    uint256 public value;

    function Test() {
        value = 123;    
    }
}

using the following commands on the flattened source:

> var testSource = "pragma solidity ^0.4.8; contract Test { uint256 public value; function Test() { value = 123; } }";
undefined
> var testCompiled = web3.eth.compile.solidity(testSource);
Error: The method eth_compileSolidity does not exist/is not available
    at web3.js:3104:20
    at web3.js:6191:15
    at web3.js:5004:36
    at <anonymous>:1:20

As described in eth_compilers, eth_compileSolidity are gone in go-ethereum 1.6.0 #3793, the ability to compile Solidity source has been removed from within geth.

How can I compile my Solidity source code?

2

6 Answers 6

29

Alternative 1 - Use Remix To Compile, Then Copy-Paste To Deploy

Use Remix from https://ethereum.github.io/browser-solidity:

enter image description here

Copy the Web3 deployment instructions from the Remix page:

var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
var untitled_test = untitled_testContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

And paste it into the geth console with an unlocked account:

> personal.unlockAccount(eth.accounts[0], "{top secret password}");
true
> var untitled_testContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"value","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"}]);
"0xe22dc29e3e05d3206d9636ae7cba87a2827e7b3ef28c6e8e6d2ad49a14ba3ce9"
var untitled_test = untitled_testContract.new(
   {
     from: web3.eth.accounts[0],
     data: '0x60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820a4fac284b98d43538f802082b0db8c67ddd6d72df4e8d9fbccb4cec0e52ea0df0029',
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })



Alternative 2 - Use Solidity Compiler (Linux and OS/X, maybe Windows With Cygwin)

$ more Test.sol 
pragma solidity ^0.4.8;

contract Test {
    uint256 public value;

    function Test() {
        value = 123;    
    }
}

Compile Test.sol in the .json format, assign the data to a JavaScript variable and send the output into a file:

$ echo "var testOutput=`solc --optimize --combined-json abi,bin,interface Test.sol`" > test.js
$ cat test.js
var testOutput={"contracts":{"Test.sol:Test":{"abi":"[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]","bin":"60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029"}},"version":"0.4.9+commit.364da425.Darwin.appleclang"}

In geth, load the contents of test.js:

$ geth console
...
> loadScript("test.js")
true
> testOutput
{
  contracts: {
    Test.sol:Test: {
      abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]",
      bin: "60606040523415600b57fe5b5b607b6000819055505b5b608f806100246000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680633fa4f24514603a575bfe5b3415604157fe5b6047605d565b6040518082815260200191505060405180910390f35b600054815600a165627a7a72305820d0e71d151634ac6ae7626860a17881104022e5cd6d3a088eb8f941d9aa8e3bd20029"
    }
  },
  version: "0.4.9+commit.364da425.Darwin.appleclang"
}

> testOutput.contracts
{
  abi: "[{\"constant\":true,\"inputs\":[],\"name\":\"value\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"type\":\"constructor\"}]",
...
> var testContract = web3.eth.contract(JSON.parse(testOutput.contracts["Test.sol:Test"].abi));
undefined
> personal.unlockAccount(eth.accounts[0], "{top secret password}");
true
> var test = testContract.new({ from: eth.accounts[0], data: "0x" + testOutput.contracts["Test.sol:Test"].bin, gas: 4700000},
  function (e, contract) {
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
  }
);
...
4
  • If I follow your instructions from Alternative 1 I get the error: "err: Error: invalid argument 0: invalid hex". I figured out that the bytecode (bin) causes this error. I also tried Alternative 2 but I get the same error.
    – Bumblebee
    Commented May 15, 2017 at 18:58
  • 2
    Alternative 2 worked and was the only working instruction I could find so far. Big thanks!
    – Jasper
    Commented Jun 12, 2017 at 6:12
  • I was able to compile contract with ethereum.github.io/browser-solidity but when i got code from field WEB3DEPLOY and paste it in myetherwallet.com/#contracts Deploy contract filed it throw an error (red borders) Commented Sep 25, 2017 at 19:28
  • 2
    In a newer version of remix click on "Details" button of "Compile" section and look for WEB3DEPLOY section
    – yname
    Commented Sep 30, 2017 at 23:33
7

I've encountered the same issue recently. There are a variety of workarounds, but here is the method I've employed. This isn't necessarily the best approach.

To generate bytecode:

> solc --bin soliditySource.sol

To generate ABI:

> solc --abi soliditySource.sol

Then, with your newly generated bytecode and abi, in geth 1.6:

 var abi = <abi from solc>;
 var myContract = eth.contract(abi); 
 var bytecode = '0x' + <bytecode from solc>;
 var txDeploy = {from:eth.coinbase, data: bytecode, gas: 1000000}; 
 var myContractPartialInstance = myContract.new(txDeploy); 

 // Mine block containing transaction...

 var myContractInstance = myContract.at(myContractPartialInstance.address);
1
  • 1
    The abi needs to be a JSON object, so convert with JSON.parse. Also the account might need to be unlocked first with eg. personal.unlockAccount(eth.accounts[0]).
    – jordanpg
    Commented Dec 24, 2017 at 16:50
3

Maybe you'll consider downloading the truffle framework instead. With the truffle framework, I'm able to compile but also store my files easily (it does it for me).

Once you have downloaded truffle with the command

npm i -g truffle

you can easily compile using the command truffle compile.

3

After bashing my head against out of date docs for a few days, I've made a super simple version here that might help: https://alanbuxton.wordpress.com/2017/07/19/first-steps-with-ethereum-private-networks-and-smart-contracts-on-ubuntu-16-04/

2
0

Because geth deprecated the command-line compiler interface after version 1.6, I was puzzled for a long time.I wrote a simple tutorial to describe how to compile and deploy a contract after geth1.6. Hope this tutorial will help you. Depoly contract on the private chain

0

Building a smart contract using the command line https://www.ethereum.org/greeter

1
  • Hi and welcome to Ethereum StackExchange. Please do not post questions as answers to other questions. If you need assistance with a problem, please post a new question and someone will hopefully answer it. Commented Apr 10, 2018 at 12:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.