Learn how to interact with a Ethereum Blockchain using HTTP RPC-JSON requests
Learn how to interact with a Ethereum Blockchain using HTTP RPC-JSON requests
Step 1: In this exercise, we will run a local blockchain test network using Ganache and interact with it using JSON-RPC requests using Thunder Client VS Code extension.
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: Open VS Code by clicking 'VS Code' to carry forward the interactions with this network.
Step 7: We have pre-installed the Thunder Client extension. To install it on your system you can simply open extensions and search for 'Thunder Client' and install it from there.
Step 8: We have created a github gist with the api calls which we will use in this lab (You can check this gist by visiting the given link).
Step 9: We will import the collection mentioned in the previous step from our gist. Click on the extension section as shown in the screenshot below.
Step 10: Then click 'collections' tab and then on its 'Hamburger Menu'.
Step 11: Click on 'Import from URL'. Copy-Paste the Gist URL provided to the URL field and press Enter. The collection will be imported.
Step 12: On successful import, you should be able to see 'blockchain-interaction-commands' collection. You can click on it to view all requests.
Step 13: Let's make our first API call by clicking 'fetch-accounts.' The API call details are already set, so we can just click the 'send' button. The response on the right side will show the available accounts along with their addresses.
Step 14: Fetch the current block number of our network by selecting the 'current-blocknumber' request and clicking on 'Send' button
Step 15: Check client version of the Web3 library by using the 'get-web3-client-version' request.
Step 16: Check Ethereum protocol version being used by using the 'current-ethereum-protocol-version'request.
Step 17: The API calls we have made till now had no parameters, i.e. we were just calling the methods and getting the output.
Step 18: Now we will make an API call that has parameters. We will use the 'get-balance' API call to get the balance of the first account in our network (see we have already provided the address as the first parameter). You can use check with some other address the from the response of the 'fetch-accounts' API call. The balance you get is the hexadecimal representation of the value in Wei.
Step 19: Send 10 Ethers from one address to another address using 'send-transaction' API call notice we have 3 parameters , 'from' 'to' and 'value'. The value is hex representation of 10 ethers. You can change the addresses in the 'from' and 'to' to do some more transactions. The response of this transaction is a hexadecimal Transaction Hash. We will use this Hash in the upcoming steps.
Step 20: [Optional] Hexadecimal value shown in the request can be converted to decimal(Wei) by running this simple command on terminal. You will have to open a terminal for this.
Step 21: [Optional] Alternately, you can also use an online website like the one provided for this conversion.
Step 22: We will now retrieve transaction details using the Transaction Hash. Open 'get-transaction-by-hash' request and replace the placeholder [TRANSACTION_HASH] with the hash value received in the previous 'send-transaction' step.
Step 23: Similarly, get transaction receipt for the transaction using 'get-transaction-receipt' API call. Make sure to pass the Transaction Hash as parameter.
Step 24: As we know, GasPrice is the price a user is willing to pay for executing a transaction. Use 'get-gas-price' API call to get the current GasPrice. This value changes in real blockchain netw orks depending on the network congestion but in test networks like Ganache, it is fixed.
Step 25: We can also get the number of transactions in a block using 'get-transaction-count' API call. Here we have passed the address of first block as parameter, but you can also try with other block addresses.
Step 26: We learned how to interact with blckchain network or node using HTTP JSON-RPC requests.