Ethereum Client communication with the default JavaScript Console

I have discussed in my previous article how you can develop a single blockchain node. Therefore it is time to communicate with our private node using the default javascript console which comes with the geth.

How to communicate with geth’s javascript console.

The default way to communicate with the blockchain node is javascript console provided by the geth client. If you remember in our previous article, we have mentioned that our first account will be treated as a coinbase account so that it will receive all the mining rewards. That way we have at least one account which can start the transaction with other accounts.

1- Open the Geth JavaScript Console:

I am assuming that your node is still working which we have discussed in our previous article.

Now open another terminal and write “geth attach” command. This command will attach the javascript console of the geth to the existing running geth client which is acting as a node in your computer. Below you can see how the welcoming screen would be.

a1

2- Check the account address:

As you know the default settings of our ethereum is continuously mining the empty blocks so that way our coinbase account will receive the mining rewards. To check what is the address of the account is then write

> eth.account[0]

The value or hash will be the same as what you had in your keystore folder. Previously we had created three different accounts so you can pass the value as 1 or 2 to see the addresses of other accounts.

These javascript commands give flexibility even to transact with each other accounts.

3 – Check the balance in the account

To check the Ether amount you can simply type

> eth.getBalance(eth.accounts[0])

The above query will return you the ether amount, but it is not in readable format. In our case, we can see that the first address has 1.308e+21 as a balance.

a2

4 – Check the Ether balance in the account

The web3 library provides a function to see how much ethers are in the account. Just write the following command

> web3.fromWei(eth.getBalance(eth.accounts[0]),”ether”)
a3

As above image, we can see the coinbase account or miner account has 1449 Ethers, but after another empty block mined, it turns into 1452 Ethers. So it looks like our account 0 is rich enough to send some money to the account number 1.

5 – Send some ethers from account 0 (coinbase) to account 1

To send any amount of ether to another account is very simple in Geth Javascript console. Just write following command

> eth.sendTransaction(from:eth.accounts[0],to:eth.accounts[1],value:web3.toWei(123,”ether”)

You will see a long hash of the transaction, which will be signed by the account[0] (our coinbase account) and it has sent the 123 ethers to the account number 1.

a5

6 – Check the transaction info on the private node console

Now check the running node console, and you will find the transaction which denotes as TX with the “1” as value. You can also see the utilization of the gas with the amount 21000 for this transaction. Below is the snapshot of our transaction as compared to the empty transaction underneath or before it. You can also see “from” and “to” addresses with their respective hashes.

a6

7 – Send some ethers from account 1 to account 2

Now let’s try to send some Ethers from account 1 to account 2

> eth.sendTransaction(from:eth.accounts[1],to:eth.accounts[2],value:web3.toWei(32,”ether”)

Error: authentication needed: password or unlock
This error shows that the account which must be an initiator of the transaction must have proper rights or must be logged in.

Here try to understand essential technical information. Whenever any account initiates the transaction, then that particular account needs to sign the transaction by its private key. In our case, the account number 0 is a coinbase account. Secondly, it is already unlocked at the startup time of the private node.

So for account number 1, we must unlock manually on the javascript console by providing its keystore password. If you do not understand what public or private key concept is? Then I would highly recommend you to read the PKI cryptography using good old savior google or Wikipedia.

a7

To unlock an account just write following command followed by the password of your keystore file.

> personal.unlockAccount(eth.accounts[1])
a8

Once the account number 1 is unlocked, you can send any transaction from that account to any other account. As you can see now, 32 Ethers have been sent to account number 2.

8 – Gas charges and remaining balance

a9

Here account number 0 is incremented with more ethers because it is a coinbase account.

But if you look at the account number 1 which had only 132 Ethers before now has 99.999622 Ethers, but the account number 2 has now 32 Ethers which were successfully sent.

Look carefully at the amount which was deducted from account number 1 which is now 32.000378 instead of 32. Because the sender must also pay the gas price, therefore, 0.000378 has been paid from account number 1.

Read the below calculation to understand the Ethers distribution.

 

Account 1 = 132.00 Ether

Account 2 = 0 Ether

Account 1 Sent 132 ether to Account 2

Account 1 paid 0.000378 Ethers for the gas charges

Account 1 balance is now 99.999622

Note:- The transaction result or output will take some time to display on the console because it collects all the transactions to fill the existing block. Afterward, it will initiate the mining process. So do not worry if after the transaction you see unchanged balance like in our case it will remain zero for sometime.

Congrats, now you can use command line javascript console to do transaction between the accounts. In my next blog, I will discuss how you can use MetaMask to do the same transactions with more ease and better visual experience.