File tree Expand file tree Collapse file tree 1 file changed +20
-0
lines changed
Expand file tree Collapse file tree 1 file changed +20
-0
lines changed Original file line number Diff line number Diff 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!(
You can’t perform that action at this time.
0 commit comments