Deploying an Ethereum Smart Contract with the eth.rb Ruby Gem

ยท

Introduction

Eth.rb is the maintained Ethereum library for the Ruby language, replacing the deprecated ethereum.rb. This tutorial guides you through deploying a smart contract on the Ethereum Goerli testnet.

Prerequisites

Prepare a Smart Contract

Use the simple storage contract. Create simplestorage.sol:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.4.16 <0.9.0;
contract SimpleStorage {
    uint storedData;
    function set(uint x) public {
        storedData = x;
    }
    function get() public view returns (uint retVal) {
        return storedData;
    }
}

Console Walkthrough

  1. Clone the eth.rb repository:

    git clone https://github.com/q9f/eth.rb
  2. Run the Ruby console:

    ruby bin/console
  3. Import your Chainstack node:

    client = Eth::Client.create 'CHAINSTACK_NODE_URL'
  4. Import your private key:

    deployer_account = Eth::Key.new priv: 'PRIVATE_KEY'
  5. Set gas parameters:

    client.gas_limit = 200000
    client.max_fee_per_gas = 41000000000
    client.max_priority_fee_per_gas = 40000000000
  6. Import and deploy the contract:

    contract = Eth::Contract.from_file(file: 'contracts/simplestorage.sol')
    client.deploy_and_wait(contract, sender_key: deployer_account, gas_limit: 200000)
  7. Interact with the contract:

    simplestorage_abi = '[{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"retVal","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}]'
    simplestorage_contract = Eth::Contract.from_abi(name: "SimpleStorage", address: "CONTRACT_ADDRESS", abi: simplestorage_abi)
    client.transact_and_wait(simplestorage_contract, "set", 1234, sender_key: deployer_account)
    client.call(simplestorage_contract, "get")

Script Walkthrough

  1. Install the gem:

    gem install eth
  2. Create deploy.rb:

    require 'eth'
    client = Eth::Client.create 'CHAINSTACK_NODE_URL'
    deployer_account = Eth::Key.new priv: 'PRIVATE_KEY'
    client.max_fee_per_gas = 41000000000
    client.max_priority_fee_per_gas = 40000000000
    contract = Eth::Contract.from_file(file: 'contracts/simplestorage.sol')
    response = client.deploy_and_wait(contract, sender_key: deployer_account, gas_limit: 200000)
    puts response
  3. Run the script:

    ruby deploy.rb
  4. Create transact.rb:

    require 'eth'
    client = Eth::Client.create 'CHAINSTACK_NODE_URL'
    simplestorage_abi = '[{"inputs":[],"name":"get","outputs":[{"internalType":"uint256","name":"retVal","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"x","type":"uint256"}],"name":"set","outputs":[],"stateMutability":"nonpayable","type":"function"}]'
    simplestorage_contract = Eth::Contract.from_abi(name: "SimpleStorage", address: "CONTRACT_ADDRESS", abi: simplestorage_abi)
    response = client.transact_and_wait(simplestorage_contract, "set", 12345, sender_key: deployer_account)
    puts response
  5. Run the script:

    ruby transact.rb
  6. Create call.rb:

    require 'eth'
    client = Eth::Client.create 'CHAINSTACK_NODE_URL'
    simplestorage_contract = Eth::Contract.from_abi(name: "SimpleStorage", address: "CONTRACT_ADDRESS", abi: simplestorage_abi)
    response = client.call(simplestorage_contract, "get")
    puts response
  7. Run the script:

    ruby call.rb

FAQ

What is eth.rb?

Eth.rb is a Ruby gem for interacting with the Ethereum blockchain, enabling smart contract deployment and transactions.

How do I get Goerli ether?

You can obtain Goerli ether from testnet faucets like Chainstack's multi-chain faucet.

What is the gas limit?

The gas limit is the maximum amount of gas you're willing to spend on a transaction.

๐Ÿ‘‰ Explore more about Ethereum development

Conclusion

You've learned to deploy and interact with a smart contract using Ruby and eth.rb. This opens up Ethereum development for Rubyists.

๐Ÿ‘‰ Start building with Chainstack today