If you've ever played Mario Kart, then you know how awesome those magic rainbow item boxes are, and what an absolute GAME CHANGER they can be...
giving you powers and tools to help you succeed β¨
In a similar way, Ruby gems serve as seemingly magic packages of code that have tremendous capabilities, and a vast number use cases, all of which can help you create full scale applications much more efficiently, and effectively.
Within the last few weeks, I've spent a lot of time learning how to build out functional backend databases using Ruby in conjunction with Sinatra, a gem which serves as a domain specific language (DSL) that helps build out applications with minimal effort. I've newly shifted to utilizing the Rails gem, which is a full-stack framework that has greatly simplified the process of building out databases and their corresponding frontend features, especially as they come to scale.
Following the rise and evolution of both Web 1.0 and 2.0...
"Web 3.0" exposes us to a new generation of web applications that are fueled by user ownership and decentralization.
The fundamentals of what Web 3 is built upon extends past the scope of this post, but I've created a brief overview of the generalized idea here.
My recent exploration of Ruby as a language, and the frameworks that it can run on, have sparked my curiosity to discover what applications it might have within the Web 3 space. Though there is yet a plethora of use cases to be identified as well as resources for developers to both create and utilize, I have found some gems that serve to enable interaction with the blockchain, and personal wallets.
Web 3.0 Concepts π±
It's crucial to realize that servers are no longer used to host web services and components, as web apps today can render content from consensus protocols like Ethereum, or decentralized storage systems like IPFS.
Notably, integrating such components into web applications can be done in a variety of ways. However, as a web browser is the most popular method of accessing the internet...
the majority of Web 3 material is most easily accessible through browser extensions.
For instance, IPFS Companion is an addon that enables access to IPFS-hosted data through local or remote nodes. Additionally, there are add-ons like MetaMask for blockchains like Ethereum, which allow you to create a wallet from which you can transfer and purchase digital assets and currencies.
The ability for users to manage their Ethereum accounts as well as the many methods of accessing blockchain states are just a few of the advantages that an Ethereum extension provides.
There are a number of individuals who have taken the initiative to create libraries and frameworks for other developers to more effectively build and scale decentralized applications.
In regards to Ruby and Ruby on Rails, the remainder of this post will highlight a couple of gems I've found intriguing, and I plan on writing more about my own experiences using these gems and working through their documentation further in the near future.
Gems π
While serving a similar function, the gems listed below are Ruby gems from which a user can create, sign, broadcast, and connect with Ethereum transactions.
Web3 RPC client for Ethereum
This Ruby Gem is used to connect and communicate with Ethereum node ( geth, parity, etc), having RPC interface. RPC(Remote Procedure Call) is a set of protocols and interfaces that the client interacts with blockchain system.
gem install web3-eth
Connecting to local node ( or by SSH Tunnel )
web3 = Web3::Eth::Rpc.new
If you need to connect to remote host, you can specify host, port and HTTP connection options:
web3 = Web3::Eth::Rpc.new 'node.host.com', 8545, { use_ssl: true, read_timeout: 120 }
Calling eth interface:
>> web3.eth.blockNumber
4376369
>> web3.eth.getBalance '0x829BD824B016326A401d083B33D092293333A830'
3916.6597314456685
>> block = web3.eth.getBlockByNumber 4376369
#<Web3::Eth::Block:0x007f844d6f1138 @block_data={"author"=>"0x829bd824b016326a401d083b33d092293333a830", ...
>> block.timestamp_time
2017-10-17 12:51:36 +0300
>> block.transactions.count
129
>> block.transactions[0].from
"0xb2930b35844a230f00e51431acae96fe543a0347"
>> block.transactions[0].value_eth
0.51896811
Ethereum For Ruby
A straightforward library to build, sign, and broadcast Ethereum transactions. It allows the separation of key and node management. Sign transactions and handle keys anywhere you can run Ruby and broadcast transactions through any local or remote node. Sign messages and recover signatures for authentication.
gem install eth
Build a transaction from scratch:
tx = Eth::Tx.new({
data: hex_data,
gas_limit: 21_000,
gas_price: 3_141_592,
nonce: 1,
to: key2.address,
value: 1_000_000_000_000,
})```
Or decode an encoded raw transaction:
```rb
tx = Eth::Tx.decode hex
Then sign the transaction:
tx.sign key
Get the raw transaction with tx.hex, and broadcast it through any Ethereum node. Or, just get the TXID with tx.hash.
These are just a couple of gems out there that enable the division of key management from node management.
Anywhere Ruby can be executed and transactions can be disseminated across any local or remote node, you can sign transactions and manage keys.
If you're curious, documentation for the original gem can be found here.
Applications π±
The following is a tutorial utilizing the "eth" Ruby gem from QuickNode.
Installing the Eth Gem
Before installing the gem, let us first ensure that Ruby is installed. Simply open a terminal and run:
ruby -v
If this returns a version later than 2.6, you're all set! If the command is not recognized, then you will need to install Ruby. If it is and the version is older than 2.6, you will need to use a newer one.
Once you're ready to move on, we can install the eth gem. This gem will allow us to connect to the Ethereum blockchain network using the Ruby language.
We can install it from the command line
gem install eth
Getting Started with QuickNode
To build on Ethereum, you'll need an API endpoint to talk to on their network. If you'd like to deploy, host, and manage your own infrastructure, you can skip this section.
Otherwise, QuickNode.com. offers a free 7 day trial!
Create an Endpoint
Once youβve signed up, create an endpoint running on the Ethereum network. Then, navigate to the "Get Started" tab and copy the HTTP Provider link:
This will be used to help us connect to the Ethereum network.
Putting It All Together
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)
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.
Run the Script
Execute the script by running the following in your Terminal:
ruby script.rb
After running this command, you will see the latest Ethereum block number returned on the following line. Thatβs it! We've successfully connected to the Ethereum network using Ruby.
This is just one application of the "eth" gem, and upon utilizing more of its features, there are a seemingly infinite number of ways we could put these capabilities to use.
Conclusion π
Web 3 offers up a plethora of new opportunities for individuals from all circles of life, and with ever evolving and incoming resources for developers to explore, the possibilities are endless.
Though this post was primarily focused on interacting with the Ethereum blockchain, the examples which I've covered are merely that--
examples from which I hope anyone reading this might be inspired by the extended use cases of smart contracts, and blockchain technology.
I hope that in some way, this has sparked a flame within you to consider a new perspective, and I appreciate you taking the time to take a step within my mind. If you have any questions, or would like to be in touch, feel free to message me, or comment below.
Top comments (2)
bookmarked for later π₯
Wow, thank you Elliot !!