Skip to content

Commit c55f7db

Browse files
jmcph4onbjerg
andauthored
feat: add BlockId::as_u64 (alloy-rs#916)
* Add `as_block_number` * Rename to `as_u64` * Add tests for `BlockId::as_u64` * Update crates/eips/src/eip1898.rs Co-authored-by: Oliver <[email protected]> --------- Co-authored-by: Oliver <[email protected]>
1 parent f04a704 commit c55f7db

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

crates/eips/src/eip1898.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,14 @@ impl BlockId {
280280
}
281281
}
282282

283+
/// Returns the block number if it is [`BlockId::Number`] and not a tag
284+
pub const fn as_u64(&self) -> Option<u64> {
285+
match self {
286+
Self::Number(x) => x.as_number(),
287+
_ => None,
288+
}
289+
}
290+
283291
/// Returns true if this is [BlockNumberOrTag::Latest]
284292
pub const fn is_latest(&self) -> bool {
285293
matches!(self, Self::Number(BlockNumberOrTag::Latest))
@@ -750,6 +758,18 @@ mod tests {
750758
assert_eq!(serialized, "\"0x1\"");
751759
}
752760

761+
#[test]
762+
fn block_id_as_u64() {
763+
assert_eq!(BlockId::number(123).as_u64(), Some(123));
764+
assert_eq!(BlockId::number(0).as_u64(), Some(0));
765+
assert_eq!(BlockId::earliest().as_u64(), None);
766+
assert_eq!(BlockId::latest().as_u64(), None);
767+
assert_eq!(BlockId::pending().as_u64(), None);
768+
assert_eq!(BlockId::safe().as_u64(), None);
769+
assert_eq!(BlockId::hash(BlockHash::ZERO).as_u64(), None);
770+
assert_eq!(BlockId::hash_canonical(BlockHash::ZERO).as_u64(), None);
771+
}
772+
753773
#[test]
754774
fn can_parse_eip1898_block_ids() {
755775
let num = serde_json::json!(

0 commit comments

Comments
 (0)