Installing the eth.rb gem
Let’s first make sure we have Ruby installed on the system. To check just copy-paste and run the following in your terminal/cmd:
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 upgrade to a newer version.
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.
Once you're ready to move on, we can install the eth.rb 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 using the RubyGems package manager:
Let's move forward and install the eth.rb gem, You can install it from the command line using RubyGems, the package manager for Ruby.
gem install eth
gem install eth
You'll also need to install a gem called forwardable:
gem install forwardable
gem install forwardable
Creating and Running a Ruby Script to Fetch Logs
Open your text editor and create a Ruby file log.rb, copy-paste the following in it.
require 'eth'
require 'forwardable'
client = Eth::Client.create 'QUICKNODE_HTTP_PROVIDER_LINK'
contract_address = "CONTRACT_ADDRESS_FROM_REMIX"
contract_name = "Increment"
contract_abi = '[
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "oldValue",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "newValue",
"type": "uint256"
}
],
"name": "ValueChanged",
"type": "event"
},
{
"inputs": [],
"name": "getCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]'
contract = Eth::Contract.from_abi(name: contract_name, address: contract_address, abi: contract_abi)
params =
{
address: contract.address,
fromBlock: "earliest",
toBlock: "latest",
topics: []
}
events = client.eth_get_logs(params)["result"]
event_abi = contract.abi.find {|a| a['name'] == 'ValueChanged'}
event_inputs = event_abi['inputs'].map {|i| i["type"]}
events.each_with_index do |event, index|
transaction_id = event["transactionHash"]
transaction = client.eth_get_transaction_receipt(transaction_id)
logs = transaction.dig('result', 'logs').find { |d| d['data'] != "0x" }
data = logs.fetch('data')
eventlogs = transaction.inspect
block = logs.fetch('blockNumber')
contract_values = Eth::Abi.decode(event_inputs, data )
puts "Transaction: #{index+1}"
puts "Transaction ID: #{transaction_id}"
puts "The Transaction was added to block: #{block.to_i(16)}"
puts "Contract Logs:"
event_abi['inputs'].each_with_index do |value,index|
puts "#{" "* 3} #{value["name"]}: #{contract_values[index]}"
end
puts "Event Logs:"
puts eventlogs
puts "-" * 20
end
require 'eth'
require 'forwardable'
client = Eth::Client.create 'QUICKNODE_HTTP_PROVIDER_LINK'
contract_address = "CONTRACT_ADDRESS_FROM_REMIX"
contract_name = "Increment"
contract_abi = '[
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "oldValue",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "newValue",
"type": "uint256"
}
],
"name": "ValueChanged",
"type": "event"
},
{
"inputs": [],
"name": "getCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]'
contract = Eth::Contract.from_abi(name: contract_name, address: contract_address, abi: contract_abi)
params =
{
address: contract.address,
fromBlock: "earliest",
toBlock: "latest",
topics: []
}
events = client.eth_get_logs(params)["result"]
event_abi = contract.abi.find {|a| a['name'] == 'ValueChanged'}
event_inputs = event_abi['inputs'].map {|i| i["type"]}
events.each_with_index do |event, index|
transaction_id = event["transactionHash"]
transaction = client.eth_get_transaction_receipt(transaction_id)
logs = transaction.dig('result', 'logs').find { |d| d['data'] != "0x" }
data = logs.fetch('data')
eventlogs = transaction.inspect
block = logs.fetch('blockNumber')
contract_values = Eth::Abi.decode(event_inputs, data )
puts "Transaction: #{index+1}"
puts "Transaction ID: #{transaction_id}"
puts "The Transaction was added to block: #{block.to_i(16)}"
puts "Contract Logs:"
event_abi['inputs'].each_with_index do |value,index|
puts "#{" "* 3} #{value["name"]}: #{contract_values[index]}"
end
puts "Event Logs:"
puts eventlogs
puts "-" * 20
end
require 'eth'
require 'forwardable'
client = Eth::Client.create 'QUICKNODE_HTTP_PROVIDER_LINK'
contract_address = "CONTRACT_ADDRESS_FROM_REMIX"
contract_name = "Increment"
contract_abi = '[
{
"inputs": [],
"name": "increment",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"internalType": "uint256",
"name": "oldValue",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "newValue",
"type": "uint256"
}
],
"name": "ValueChanged",
"type": "event"
},
{
"inputs": [],
"name": "getCount",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
}
]'
contract = Eth::Contract.from_abi(name: contract_name, address: contract_address, abi: contract_abi)
params =
{
address: contract.address,
fromBlock: "earliest",
toBlock: "latest",
topics: []
}
events = client.eth_get_logs(params)["result"]
event_abi = contract.abi.find {|a| a['name'] == 'ValueChanged'}
event_inputs = event_abi['inputs'].map {|i| i["type"]}
events.each_with_index do |event, index|
transaction_id = event["transactionHash"]
transaction = client.eth_get_transaction_receipt(transaction_id)
logs = transaction.dig('result', 'logs').find { |d| d['data'] != "0x" }
data = logs.fetch('data')
eventlogs = transaction.inspect
block = logs.fetch('blockNumber')
contract_values = Eth::Abi.decode(event_inputs, data )
puts "Transaction: #{index+1}"
puts "Transaction ID: #{transaction_id}"
puts "The Transaction was added to block: #{block.to_i(16)}"
puts "Contract Logs:"
event_abi['inputs'].each_with_index do |value,index|
puts "#{" "* 3} #{value["name"]}: #{contract_values[index]}"
end
puts "Event Logs:"
puts eventlogs
puts "-" * 20
end
Make sure to replace QUICKNODE_HTTP_PROVIDER_LINK with the Ropsten HTTP Provider, CONTRACT_ADDRESS_FROM_REMIX with the address of the deployed contract from the previous sections, and contract_abi with the abi value you copied from the Remix.
Explanation of the code above
- Line 1-2: Importing required gems.
- Line 4: Define the client using Ropsten endpoint from QuickNode.
- Line 5-6: Define your contract address (copy from Remix) and your contract name, 'Increment'.
- Line 7-48: Replace contract_abi with your ABI, which you copied in the previous section (if you used the same smart contract, you use the same ABI).
- Line 49: Instantiating our contract object and getting the contract.
- Line 51-57: Defining our search parameters using the contract address and earliest and latest blocks.
- Line 58: Getting logs based on our search parameters and storing them in the events variable.
- Line 60: Finding the event ValueChanged from the ABI and storing it in the event_abi variable.
- Line 61: Mapping our event_abi to find the input types and storing the array in our event_inputs variable (we'll use this to decode our smart contract).
For each Transaction in our Logs:
- Line 64: Getting the transaction hash and storing it in the transaction_id variable.
- Line 65: Getting transaction details using the eth_get_transaction_receipt method for transaction hash via our node.
- Line 66: Searching the event logs for results and logs and storing it in a logs variable.
- Line 67: Fetching data from logs and storing it in a data variable.
- Line 68: Getting the transaction details using inspect method and storing it in eventlogs variable.
- Line 69: Fetching blockNumber from logs and storing it in block variable.
- Line 70: Decoding the event_inputs and data and storing it in contract_values variable.
- Line 71-80: Printing results from our search.
- Line 75-77: For each transaction, we print decoded values returned by our smart contract (in this case, the oldValue and newValue that we defined when creating our smart contract).
Now, save log.rb and run it as follows:
$ ruby log.rb
$ ruby log.rb
If everything goes right, your output must look like this:
The output has the transaction, block number, contract logs, and the event details.