Skip to content
Afr Schoe edited this page Feb 8, 2022 · 2 revisions

The Ruby Eth gem contains an RPC-Client that can connect to local or remote nodes to interact with any chain.

The Eth::Client.create method will automatically determine what kind of endpoint is provided (IPC, HTTP, or HTTPS).

Let's connect to an Infura HTTPS-endpoint (define your access_token).

infura = Eth::Client.create "https://mainnet.infura.io/v3/#{access_token}"
# => #<Eth::Client::Http:0x000055d43f3ca460 @gas_limit=21000, @host="mainnet.infura.io", @id=0, @max_fee_per_gas=0.2e11, @max_priority_fee_per_gas=0, @port=443, @ssl=true, @uri=#<URI::HTTPS https://mainnet.infura.io/v3/31b...d93>>

It automatically creates an Eth::Client::Http with the attributes @ssl=true and @port=443 to indicate we are using HTTPS. Let's query the balance of the beacon-chain deposit contract.

deposit_contract = Eth::Address.new "0x00000000219ab540356cBB839Cbe05303d7705Fa"
# => #<Eth::Address:0x000055d43f381738 @address="0x00000000219ab540356cBB839Cbe05303d7705Fa">
infura.get_balance deposit_contract
# => 9087314000069000000000069

The get_balance method is a convenient method to easily query an account balance.

Other convenient methods are chain_id, default_account, transfer, and transfer_and_wait which ease sending Ether from one account to another. Let's try this with the Geth developer mode.

geth = Eth::Client.create "/tmp/geth.ipc"
# => #<Eth::Client::Ipc:0x0000558e54f1f788 @gas_limit=21000, @id=0, @max_fee_per_gas=0.2e11, @max_priority_fee_per_gas=0, @path="/tmp/geth.ipc">
geth.chain_id
# => 1337
geth.default_account
# => #<Eth::Address:0x0000558e5500e608 @address="0x1cdbff06dd1436bf04852a45a35aeafba473b848">
geth.get_balance geth.default_account
# => 115792089237316195423570985008687907853269984665640564039457584007913129639927
geth.transfer Eth::Address.new("0c53FFA57Ec554451315c2568d22477dB8e71356"), 25_000 * Eth::Unit::ETHER
# => "0x91686eb63956a760aced0886236a476b69358e068d74d67f1134e0c79ebe326a"

The transfer returns the transaction hash regardless of the transaction status once submitted to the blockchain whereas transfer_and_wait will only return the transaction hash once the transaction is included in a block.

By meta-programming, the Eth:: gem tries to provide all available APIs as methods native to the client.

geth.clique_proposals
# => {"jsonrpc"=>"2.0", "id"=>7, "result"=>{}}
geth.eth_block_number
# => {"jsonrpc"=>"2.0", "id"=>8, "result"=>"0x1"}
geth.miner_start
# => {"jsonrpc"=>"2.0", "id"=>10, "result"=>nil}
geth.txpool_inspect
# => {"jsonrpc"=>"2.0", "id"=>11, "result"=>{"pending"=>{}, "queued"=>{}}}

And so on... Note, that the Eth::Client does not know which APIs are actually available and enabled by the connected endpoint. Additional logic will be required to deal with IOErrors.

For an overview of all natively supported ::API methods, see API documentation. Note that CamelCase methods will be converted to and exposed as snake_case, e.g., eth_blockNumber is eth_block_number.

For the full coverage of the ::Client, check out the API documentation.

Clone this wiki locally