Now, we'll use the eth gem along with our QuickNode endpoint to create a short script to fetch the latest block number using our node.
Create a new file, script.rb, through your Terminal or directly in your file system. If you choose to use a Terminal, you can use this command:
echo > script.rb
Open script.rb in a code editor of choice and add the following code:
require 'eth'
client = Eth::Client.create 'YOUR_ETHEREUM_NODE_URL'
block_number = client.eth_block_number
puts block_number["result"].to_i(16)
require 'eth'
client = Eth::Client.create 'YOUR_ETHEREUM_NODE_URL'
block_number = client.eth_block_number
puts block_number["result"].to_i(16)
require 'eth'
client = Eth::Client.create 'YOUR_ETHEREUM_NODE_URL'
block_number = client.eth_block_number
puts block_number["result"].to_i(16)
Replace the YOUR_ETHEREUM_NODE_URL with the HTTP provider from the instructions above.
Let us break down the code:
- Line 1: We are importing the eth gem we installed earlier.
- Line 3: We are creating a new Ethereum RPC client, passing in our Ethereum node URL. Visit the official eth.rb Github repo for more information.
- Line 4: We are getting the latest Ethereum block number using the eth_block_number method and storing it in block_number.
- Line 5: We are printing the block number. Note that the returned result is in hexadecimal format, thus we use the to_i(16) function to convert it into an integer, base 16.