I am running a local private Ethereum blockchain using:
geth --networkid 60098 --genesis CustomGenesis.json --nodiscover --maxpeers 0 --rpc --rpccorsdomain "http://localhost:3000" --datadir "./data" console
It runs fine. I can mine some coins to fill the coinbase account and see a balance in it.
My problem is that my contract deployment transaction does not record into the blockchain. I tried the Greeter tutorial from the docs at GitHub wiki.
Relevant statements that I ran from that article are:
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)
Compiles fine, and then I used this command to deploy the contract:
var _greeting = "Hello World!"
var greeterContract = web3.eth.contract(greeterCompiled.greeter.info.abiDefinition);
var practice_acct = '0x0ff4e0dfc7f4b94b4ab6a83e922fe1ac9ac1a625'; //verified to have sufficient coins
var greeter = greeterContract.new(_greeting,{from: practice_acct, data: greeterCompiled.greeter.code, gas: 4000000}, function(e, contract){ if(!e) { if(!contract.address) { console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined..."); } else { console.log("Contract mined! Address: " + contract.address); console.log(contract); } } })
Then I get this back after entering my passphrase and trying to output the greeter:
Unlock account 85b1a00f429811eed0f56ceff9ae19de047ab9fe
Passphrase:
undefined
greeter
{
address: undefined
}
I even tried directly pasting the ‘Web3 deploy’ code from the online Solidity compiler using the tutorial source. Still no address returning.
Any ideas where my problem might be?
Best Answer
Update Apr 23 2017
geth
1.6.0 has a breaking change to remove access to the Solidity compiler from withingeth
.The workaround is detailed in How to compile Solidity contracts within geth with the v1.6.0 **BREAKING CHANGE**?
Update Feb 04 2017
Solidity 0.4.9 has a breaking change. The workaround is detailed in Unable to define greeterContract in the Greeter tutorial. Breaking change in Solidity 0.4.9! where you have to use refer to variable like
greeterCompiled.<stdin>:greeter
withgreeterCompiled["<stdin>:greeter"]
.Here's a step-by-step guide to get this contract deployed to a dev blockchain.
Create A Coinbase Account
Window #1 - Run Geth Mining Instance With Unlocked Account
This geth instance will mine the blockchain. We will unlock the coinbase account so this instance will be able to execute transactions, and run the mining on one CPU thread. Enter your password created in the previous step at the
Passphrase:
prompt.Window #2 - Run Second Geth Instance Attaching To The First
EDIT 30/05/2016 - Updated command as IPC path behaviour has changed between
geth
v1.3.6 and 1.4.5-stable.Open a second terminal and run a second geth instance that attaches to the first geth instance. Checking the balance of the coinbase account (eth.accounts[0]) will show an increasing balance as the first geth instance continues mining.
Compile the code in the question and display the compiled code.
Send the transaction to insert the contract code into the blockchain. You will see that the contract transaction has been successfully mined.
You will see in window #1 the following message that shows the transaction being included into a mined block #352. Your greeter contract is now in the blockchain:
In window #2, call the greeter.greet() method that will extract the message from the blockchain and display it:
If you exit from your window #2 geth instance and want to re-run the contract inserted in the steps above, start up the geth instance again (window #2 attach version) and run the following commands. Replace the address at the
at(...)
function with your contract address:EDIT 30/05/2016
If You Are Not Getting The Results Above
var greeterSource = 'contract mortal { address ow...
togreeter2.greet()
in Window #1.If you are not getting the
Contract mined! Address: 0x083628160c1...
message:a. Check that
geth
is mining successfully. You should see the message🔨 Mined block (#...
in your Window #1.b. If you are not getting the
🔨 Mined block (#...
message, try running the commandminer.start(1)
. This will start the CPU mining on one thread.c. Check your account balance using
web3.fromWei(eth.getBalance(eth.accounts[0]).toNumber(), "ether")
as you will need some ethers to send the transaction.d. If all fails, try setting
debug.verbosity(4)
and see if you can work out what is happening. You can try 5 or 6 as well, but there will be more message flashing past your screen.