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

feat: adds finalized and safe block types #690

Merged
merged 1 commit into from
Apr 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/types/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ where
/// Block Number
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum BlockNumber {
/// Finalized block
Finalized,
/// Safe block
Safe,
/// Latest block
Latest,
/// Earliest block (genesis)
Expand All @@ -154,6 +158,8 @@ impl Serialize for BlockNumber {
BlockNumber::Latest => serializer.serialize_str("latest"),
BlockNumber::Earliest => serializer.serialize_str("earliest"),
BlockNumber::Pending => serializer.serialize_str("pending"),
BlockNumber::Finalized => serializer.serialize_str("finalized"),
BlockNumber::Safe => serializer.serialize_str("safe"),
}
}
}
Expand All @@ -168,6 +174,8 @@ impl<'a> Deserialize<'a> for BlockNumber {
"latest" => Ok(BlockNumber::Latest),
"earliest" => Ok(BlockNumber::Earliest),
"pending" => Ok(BlockNumber::Pending),
"finalized" => Ok(BlockNumber::Finalized),
"safe" => Ok(BlockNumber::Safe),
_ if value.starts_with("0x") => U64::from_str_radix(&value[2..], 16)
.map(BlockNumber::Number)
.map_err(|e| D::Error::custom(format!("invalid block number: {}", e))),
Expand Down Expand Up @@ -330,6 +338,18 @@ mod tests {
let deserialized = serde_json::from_value::<BlockNumber>(serialized).unwrap();
assert_eq!(deserialized, BlockNumber::Pending);

// BlockNumber::Finalized
let serialized = serde_json::to_value(BlockNumber::Finalized).unwrap();
assert_eq!(serialized, "finalized");
let deserialized = serde_json::from_value::<BlockNumber>(serialized).unwrap();
assert_eq!(deserialized, BlockNumber::Finalized);

// BlockNumber::Safe
let serialized = serde_json::to_value(BlockNumber::Safe).unwrap();
assert_eq!(serialized, "safe");
let deserialized = serde_json::from_value::<BlockNumber>(serialized).unwrap();
assert_eq!(deserialized, BlockNumber::Safe);

// BlockNumber::Number
let serialized = serde_json::to_value(BlockNumber::Number(100.into())).unwrap();
assert_eq!(serialized, "0x64");
Expand Down