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
A Goerli node:
- Sign up with Chainstack.
- Deploy a Goerli node.
- Get the node's HTTPS endpoint.
- Installed Ruby.
- An Ethereum account with Goerli ether.
- Installed Solidity compiler.
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
Clone the eth.rb repository:
git clone https://github.com/q9f/eth.rbRun the Ruby console:
ruby bin/consoleImport your Chainstack node:
client = Eth::Client.create 'CHAINSTACK_NODE_URL'Import your private key:
deployer_account = Eth::Key.new priv: 'PRIVATE_KEY'Set gas parameters:
client.gas_limit = 200000 client.max_fee_per_gas = 41000000000 client.max_priority_fee_per_gas = 40000000000Import 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)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
Install the gem:
gem install ethCreate
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 responseRun the script:
ruby deploy.rbCreate
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 responseRun the script:
ruby transact.rbCreate
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 responseRun 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.