Skip to content

Instantly share code, notes, and snippets.

@abelardojarab
Last active January 29, 2018 15:09
Show Gist options
  • Select an option

  • Save abelardojarab/58e97af127bd138fcaa49649a1af9c64 to your computer and use it in GitHub Desktop.

Select an option

Save abelardojarab/58e97af127bd138fcaa49649a1af9c64 to your computer and use it in GitHub Desktop.
Useful Ethereum commands (geth)

Starting geth in console mode

$ ./build/bin/geth --fast --cache=2048 --verbosity "1" --jitvm console

Inside the console

Basic information

> eth.accounts
['0x13bf27ad7781e484a0c68752f3bc949cf4262158']

This is the same when checking your coinbase:

> eth.coinbase
'0x13bf27ad7781e484a0c68752f3bc949cf4262158'

Which allows you to chain commands like so:

> web3.fromWei(eth.getBalance(eth.coinbase), "ether")

Fetch latest blockchain

> eth.getBlock("latest").number

Should return value different than 0. If returned value is 0, then the blockchain has not been downloaded completely. You can check download state with:

> web3.eth.syncing
{
  currentBlock: 4952095,
  highestBlock: 4992475,
  knownStates: 13260,
  pulledStates: 10556,
  startingBlock: 4951442
}

Get balance

> web3.fromWei(eth.getBalance("0xdf3683cc61c530c507018c06f4beff36eec92a1c"), "ether")

geth --fast has an interesting effect: correct results are not provided until the sync is fully complete. Try querying the balance again after eth.syncing returns false.

Get balance (alternative)

> web3.fromWei(web3.eth.getBalance('0x7cacf18f931259c30a7194fa96ab3a44c38b3535'),'ether').toString(10)

List accounts

> eth.accounts
["0xdf3683cc61c530c507018c06f4beff36eec92a1c"]

Unlock account (required before sending money)

> personal.unlockAccount(address, "password", 300)

Change password of account

> geth account update a94f5374fce5edbc8e2a8697c15331677e6ebf0b

Unlocking account a94f5374fce5edbc8e2a8697c15331677e6ebf0b | Attempt 1/3
Passphrase:
0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b
account 'a94f5374fce5edbc8e2a8697c15331677e6ebf0b' unlocked.
Please give a new password. Do not forget this password.
Passphrase:
Repeat Passphrase:
0xa94f5374fce5edbc8e2a8697c15331677e6ebf0b

Sending Ethereum to a different account (alternative)

The basic way of sending a simple transaction of ether with the console is as follows:

> eth.sendTransaction({from:sender, to:receiver, value: amount})

Using the built-in JavaScript, you can easily set variables to hold these values. For example:

> var sender = eth.accounts[0];
> var receiver = eth.accounts[1];
> var amount = web3.toWei(0.01, "ether")

Alternatively, you can compose a transaction in a single line with:

> eth.sendTransaction({from:eth.coinbase, to:eth.accounts[1], value: web3.toWei(0.05, "ether")})
Please unlock account d1ade25ccd3d550a7eb532ac759cac7be09c2719.
Passphrase: 
Account is now unlocked for this session.
'0xeeb66b211e7d9be55232ed70c2ebb1bcc5d5fd9ed01d876fac5cff45b5bf8bf4'

Sending Ethereum to a different account

Before attempting to send I need to unlock the account. This prompts you for your password.

> personal.unlockAccount(eth.coinbase)

Then I send 1 ETH to each account.

> eth.sendTransaction({from:eth.coinbase, to:"0xd60e64afb753583941e1ab42f836ced0d23af2db", value: web3.toWei(1, "ether")})
"0xbea0beb44688f49177c9e2bffa246ee4471d57a9290d5e42b9d959d3e0731d90"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment