Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience method for conversion from BlockId to u64 #916

Merged
merged 4 commits into from
Jun 17, 2024

Conversation

jmcph4
Copy link
Contributor

@jmcph4 jmcph4 commented Jun 17, 2024

Motivation

Consider the case where we need to convert a BlockId into a u64. Currently, this is achievable by the following:

fn block_id_to_u64(block_id: BlockId) -> Option<u64> {
    if let BlockId::Number(number_or_tag) = block_id {
        number_or_tag.as_number()
    } else {
        None
    }
}

This works but given that the BlockId::as_block_hash method already exists, this seems like a logical inclusion into Alloy itself.

/// Returns the block hash if it is [BlockId::Hash]
pub const fn as_block_hash(&self) -> Option<BlockHash> {
match self {
Self::Hash(hash) => Some(hash.block_hash),
Self::Number(_) => None,
}
}

Solution

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

Copy link
Member

@onbjerg onbjerg left a comment

Choose a reason for hiding this comment

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

makes sense

crates/eips/src/eip1898.rs Outdated Show resolved Hide resolved
@onbjerg onbjerg added the enhancement New feature or request label Jun 17, 2024
Co-authored-by: Oliver <onbjerg@users.noreply.github.com>
@onbjerg onbjerg merged commit c55f7db into alloy-rs:main Jun 17, 2024
22 checks passed
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
* Add `as_block_number`

* Rename to `as_u64`

* Add tests for `BlockId::as_u64`

* Update crates/eips/src/eip1898.rs

Co-authored-by: Oliver <onbjerg@users.noreply.github.com>

---------

Co-authored-by: Oliver <onbjerg@users.noreply.github.com>
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