Learn basic run a local Ethereum Blockchain using Ganache and interact with it using Geth and eth-cli.
Learn basic run a local Ethereum Blockchain using Ganache and interact with it using Geth and eth-cli.
Step 1: In this exercise, we will run a local blockchain test network using Ganache and interact with it using Geth and eth-cli CLI utilities.
Step 2: Ganache CLI is already installed in the lab and you can easily install it on your machine. For that, you need to install install Node. You can follow official NPM documentation for that.
Step 3: Once Node is installed, you can install ganache-cli.
Step 4: Check ganache-cli version to confirm its installation.
Step 5: Start the blockchain with a set of predefined accounts and private keys. It will also output a list of accounts with their respective private keys and addresses.
Step 6: However, when we do this, Ganache is going to generate different and random accounts/keys every time.
Step 7: By adding a mnemonic phrase to it with '-m' or '--mnemonic' parameter ensures that it generates same accounts/keys every time.
Step 8: [Optional] By default, Ganache funds all addresses with 100 ETH. However, we can use '-e' paramater to define this amout.
Step 9: [Optional] And, like any other command, we can also check ganache options by using help.
Step 10: Ganache runs in foreground. So, let's open a new terminal tab to carry forward the interactions with this network.
Step 11: We will be using 2 different tools: 1. Geth 2. eth-cli
Step 12: Let's take geth tool first. We have already installed geth for you. But you can follow this installation guide to install it on your machine.
Step 13: Check geth version to confirm its installation.
Step 14: Run geth and attach it to our local network provided by Ganache. By default, Ganache runs the HTTP RPC endpoint on port 8545 of localhost.
Step 15: We will now run commands in the prompt provided by geth.
Step 16: List all the available accounts generated by Ganache. You can also match them with the output of ganache-cli command in the previous steps to confirm you are connected to the right network.
Step 17: Check current block number on the Ganache blockchain.
Step 18: Transfer 10 ether from the SENDER_ADDRESS (0x5C0b07b93526CD047C193fAc6d7C0F321AA8901F) to the RECEIVER_ADDRESS (0x8df7729E9fE49B32C3ef62028a38a50a431df3EE). You can replace both values with any account address from the list retrieved in step 11.
Step 19: Previous step will provided a TRANSACTION_HASH for the transfer transaction. This will be beneficial to check the transaction details.
Step 20: Check the transaction details with the transaction id generated in the last step. You can also see a summary of the transaction by switching to the tab with network logs (where Ganache is running).
Step 21: Check the current balance of any accounts. For example, check the sender's (0x5c0b07b93526cd047c193fac6d7c0f321aa8901f) balance.
Step 22: Geth provides a lot other options. Feel free to explore these on your own.
Step 23: Let's exit Geth so we can move to next tool.
Step 24: We have already installed eth-cli for you. But, you can use the given command to install it on your system.
Step 25: Check eth-cli version to confirm its installation. Running 'eth' command also shows the list of available commands. You can get additional help on these buy running eth [COMMAND_NAME] --help
Step 26: Attach eth-cli to the local ganache network. We have named it 'mynet' here but feel free to select name, label and id as per your choice.
Step 27: List all the available networks. You will just see the network we added in last step.
Step 28: Fetch the current balance (in ethers) for an account. Let's check the balance of 0x5c0b07b93526cd047c193fac6d7c0f321aa8901f account
Step 29: Make a transfer of 10 ether (the value specified has to be in Wei and 1 eth = 1000000000000000000 Wei) from the sender-address (0x5c0b07b93526cd047c193fac6d7c0f321aa8901f) to the receiver-address (0x8df7729e9fe49b32c3ef62028a38a50a431df3ee). NOTE: In such transactions, we use Sender's Private Key (0x8c4fdc5e9b1d77d1b22136c65106a7d626c0c5823148701fe48c7542acd8c2e8) and Receiver's Address (0x8df7729e9fe49b32c3ef62028a38a50a431df3ee)
Step 30: Check the details of the transaction by using the transaction hash generated in the last step. You can also see a summary of the transaction by switching to the tab with network logs.
Step 31: We have successfully learned how to create our test Private Blockchain Network with Ganache and interact with it.