Skip to content

Add ability to decode event parameters using ABI reference.#328

Merged
q9f merged 5 commits intoq9f:mainfrom
bogdan:decode-event
Apr 16, 2025
Merged

Add ability to decode event parameters using ABI reference.#328
q9f merged 5 commits intoq9f:mainfrom
bogdan:decode-event

Conversation

@bogdan
Copy link
Contributor

@bogdan bogdan commented Mar 2, 2025

Added Eth::Contract::Event#decode_params method to decode result returned by eth_getLogs or events attached to transaction.

require 'digest'
require 'eth'

# Define the event structure
event = Eth::Contract::Event.new({
  "name" => "TokenMint",
  "inputs" => [
    { "indexed" => true, "name" => "collection", "type" => "address" },
    { "indexed" => true, "name" => "tokenId", "type" => "uint256" },
    { "indexed" => true, "name" => "ipfsHash", "type" => "bytes32" },
    { "indexed" => false, "name" => "to", "type" => "address" }
  ]
})

# Topics (indexed parameters)
topics = [
  "0x56cf26bc53ebe38f9e4d908b15e9c50ad826767b3ae8726088db3772f9f9b61f", # Event signature
  "0x0000000000000000000000005c9cbf795b4d113e0cb34c5eb60ca1f41670d2fb", # collection
  "0x3c9995b18871ee6c45703900fdc22b220944763432b726a2d37e95559b866506", # tokenId
  "0xa02633d596babc5141f89d9f5737410b7559353a4aa3328c2a67668193eaa209"  # ipfsHash
]

# Data (non-indexed parameters)
data = "0x000000000000000000000000f0d00750656f12ab7550bf5039d74691f9e461f0" # to address

# Decode the parameters using the event's `decode_params` method
decoded = event.decode_params(topics, data)

# Output the decoded values
puts "Collection: #{decoded["collection"]}" # Expected: "0x5c9cbf795b4d113e0cb34c5eb60ca1f41670d2fb"
puts "Token ID: #{decoded["tokenId"]}" # Expected: 27410131662392648286045511960347474962097127373970128911820026866366436238598
puts "IPFS Hash: #{decoded["ipfsHash"].unpack1("H*")}" # Expected hex representation of the bytes32 hash
puts "To Address: #{decoded["to"]}" # Expected: "0xf0d00750656f12ab7550bf5039d74691f9e461f0"

bogdan and others added 3 commits March 2, 2025 10:40
- Refactored the `Eth::Contract::Event` class to improve code readability and maintainability.
  - Replaced direct attribute assignments with methods to access event data.
  - Utilized memoization for computed attributes like `input_types`, `inputs`, `event_string`, and `signature`.
  - Simplified the `set_address` method.
- Added a new method `decode_params` to decode event parameters from logs.
  - This method partitions inputs into indexed and non-indexed, then decodes them accordingly.
@q9f q9f added the enhancement New feature or request label Mar 2, 2025
@codecov
Copy link

codecov bot commented Mar 2, 2025

Codecov Report

Attention: Patch coverage is 84.21053% with 6 lines in your changes missing coverage. Please review.

Project coverage is 99.46%. Comparing base (473f133) to head (5ffa51c).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
lib/eth/contract/event.rb 76.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #328      +/-   ##
==========================================
- Coverage   99.57%   99.46%   -0.11%     
==========================================
  Files          81       81              
  Lines        5354     5380      +26     
==========================================
+ Hits         5331     5351      +20     
- Misses         23       29       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

end
end
end
# Provides the {Eth} module.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: something happened here with the formatting but not a blocker

@q9f q9f merged commit 80bad8a into q9f:main Apr 16, 2025
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants