How to Connect to the Casper Network using Ruby
Tags: blockchain, ruby, rubygems, dapps
Published: 2022-11-24 18:38:52



## Overview

The Ruby programming language has a huge fanbase. Ruby was developed by its creator with an intention to invent a language developers can enjoy learning and using. Ruby has been largely accepted by developers all around the world since its launch, in fact, the biggest tech communities in many cities consist of Ruby developers. In this guide, we will learn how to connect to the Casper Network using a Ruby gem (or package) called [**casper-ruby-sdk**](https://github.com/saitgulmez/casper-ruby-sdk) .

By using **casper-ruby-sdk**, we can make direct JSON RPC calls to a node from any Ruby application. **casper-ruby-sdk** also boasts numerous other functionalities listed in their official Github [**repo**](https://github.com/saitgulmez/casper-ruby-sdk):

* Get the list of Peer objects

* Current Status of the node

* Get Block Transfers

* Get Block By Hash

* Get Block By Height

* Get Era Info

* Get State Item

* Get Dictionary Item

* Get Account Balance

* Get Auction State

* Put Deploy

* Serialization

* Key Management

In this guide, we’ll take a closer look into how we can install and interact with this package by setting it up locally on our machine and then retrieving the block height from a node on Casper Network. Let’s get started!

**Prerequisites**

* Ruby version >= 3.0.2

* A code editor

* Terminal aka Command Line

## Installing the casper_network 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 3.0.2 or higher, 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 3.0.2, you will need to use a newer one. Note: the version of Ruby that ships with macOS is usually for Apple’s own use and it is best not to change it. You *can* make changes to this version, however, instead, we suggest using **rbenv** or **RVM** (Ruby Version Manager) to manage a separate Ruby version, which will be installed into a sandbox in your home directory. You can make changes to this version without worrying about changing the system’s Ruby version. For more information, read [this external guide from mac.install](https://mac.install.guide/faq/check-ruby-version/index.html).

Once you’re ready to move on, we can install the **casper_network** gem. This gem will allow us to connect to the Casper blockchain Network using the Ruby language. We can install it from the command line using the RubyGems package manager:

    gem install casper_network

**How can we access a block and its attributes in Casper Network?**

Let’s start with a simple example and then continue with a detailed one to show how to access a block and block members.

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:
{% gist https://gist.github.com/saitgulmez/c02341c393b1a3b78ee027de05166afc.js %}
You can choose an IP address among peers list from [https://testnet.cspr.live/tools/peers](https://testnet.cspr.live/tools/peers)

Replace the **YOUR_CASPER_NETWORK_NODE_IP_ADDRESS** with one of these IP addresses available in the [peers](https://testnet.cspr.live/tools/peers) list.

Let us break down the code:

* Line 1: We are importing the **casper_network** gem we installed earlier.

* Line 3: We are creating a new Casper Network RPC **client**, passing in our Casper Network node IP.

* Line 6: **block_hash** that taken from the Casper Network. You can select a block_hash in [here](https://testnet.cspr.live/blocks)

* Line 8: We are getting the latest block data using the **chain_get_block** method and store retrieved data to the **block** object.

* Line 11: We are printing the **era id** in which this block was created.

* Line 14: We are printing the **height** of the block.

## Run the Script

Execute the script by running the following in your Terminal:

    ruby script.rb

After running this command, you will be able to see **era id** and **height** information of the block as outputs. It is done! We’ve successfully connected to the Casper Network by using Ruby.

**Block Structure**

A **block** in Casper Network consists of the following attributes

* **hash**

* **header**

* **body**

* **proofs**

If you want to learn more about block structure and its properties in Casper Network, please click [here](https://docs.casperlabs.io/design/serialization-standard/#serialization-standard-block).

Let’s take a deep dive into the block and block properties in Casper Network with another example in ruby.

Create a new file, **block.rb** and add the following code:
{% gist https://gist.github.com/saitgulmez/eca7a3072512f0a9c9ee7453ba582457.js %}
Then Execute the **block.rb** by running the following command in your terminal:

    ruby block.rb

After running this command, you will be able to see outputs similar to following.

    #<Casper::Entity::Block:0x0000000003a81648>
    53943d39b0ea69dbdd3e255cfafbe0a3d610ccd0e83d86c83ee6d2f623b92e84
    #<Casper::Entity::BlockHeader:0x0000000003a815a8>
    f702dc5f9b3b09bfd019fadd48892f9fab35a07c8da93ae17cf241e15f4e5ec2
    fb3282f8d0b14d72ebdf366ade5851e557a8bcf0fbc4c82a96f1b13add822c80
    81c671e614566b5d22693b9dcf3cd09c008f3b679e4fdc7505c5560d49a23cea
    false
    8be3cba85847dd141720e5634e4706c42e1d7487d96916bf3630131891180b75
    
    2022-11-22T15:58:33.984Z
    7098
    1275281
    1.4.8
    #<Casper::Entity::BlockBody:0x0000000003a81508>
    0157f18d47996f1bbb059f2043bafea9c218d7f50e4d4dd508bc3661844300c3d1
    5cb466be5d7aaf875fb3cb34164f1bc9afb5f7b78435ae0c949be96b01701a9b
    proofs[0]: #<Casper::Entity::BlockProof:0x0000000003a81440>
    public_key: 01018525deae6091abccab6704a0fa44e12c495eec9e8fe6929862e1b75580e715
    signature: 017fabbfb10943e0dcedcb405f5cf5956b7ea62f930a816c6ae9d91a3785dfce98245c89278cf8bdbf5351a99116c381d36b2bc358cb9e421e26a5d2b9b99ea708
    proofs[1]: #<Casper::Entity::BlockProof:0x0000000003a813f0>
    public_key: 0102112a711eb3bee9043ebf036fbdfd4482f0e37d1a65627e09922943713973f8
    signature: 01388784b45165c6d171d9e700ae467185efab89f66233d6490dabf421f20bc5305af36d269dac180c73360888c352fb8527b828eace879043ca92b53af043ee0c
    proofs[2]: #<Casper::Entity::BlockProof:0x0000000003a813c8>
    public_key: 01028e248170a7f328bf7a04696d8f271a1debb54763e05e537eefc1cf24531bc7
    signature: 011b1c673488c7c8e1605eb85bd550e52753c3649a342fdca611ad55c629203f23afa7c5a0c3bdcbaf4b79d0a45adee23f71113db89f72c0f666a8c1b4d7bead07
    proofs[3]: #<Casper::Entity::BlockProof:0x0000000003a813a0>
    public_key: 0106618e1493f73ee0bc67ffbad4ba4e3863b995d61786d9b9a68ec7676f697981
    signature: 01a1c62d9db3e376f9f4ab2c02b75f2574fabe14ddb66639a805c1c26bb8968185224c0a9631574366de1f5cab0c6458482a9035fba7356d8b5d5d18176fc2060c
    .
    .
    .
    .

To summarize, I tried to explain how to access a block and block items in **Casper Network** by using Ruby in this article.


Casper Ruby SDK is an open source project funded by [DEVxDAO](https://www.devxdao.com/). I would like to thank [DEVxDAO](https://www.devxdao.com/) for funding the SDK.

**References**:
[https://casper.network/en-us/](https://casper.network/en-us/)
[https://docs.casperlabs.io/design/serialization-standard/#serialization-standard-block](https://docs.casperlabs.io/design/serialization-standard/#serialization-standard-block)
[https://docs.rs/casper-node/latest/casper_node/](https://docs.rs/casper-node/latest/casper_node/)
[https://github.com/saitgulmez/casper-ruby-sdk](https://github.com/saitgulmez/casper-ruby-sdk)
[https://www.quicknode.com/guides/ethereum-development/how-to-connect-to-the-ethereum-network-using-ruby](https://www.quicknode.com/guides/ethereum-development/how-to-connect-to-the-ethereum-network-using-ruby)