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

Implement From for FilterId and BlockTransactions #655

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
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
18 changes: 15 additions & 3 deletions crates/rpc-types/src/eth/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub struct Block {
impl Block {
/// Converts a block with Tx hashes into a full block.
pub fn into_full_block(self, txs: Vec<Transaction>) -> Self {
Self { transactions: BlockTransactions::Full(txs), ..self }
Self { transactions: txs.into(), ..self }
}
}

Expand Down Expand Up @@ -249,6 +249,18 @@ impl BlockTransactions {
}
}

impl From<Vec<B256>> for BlockTransactions {
fn from(hashes: Vec<B256>) -> Self {
BlockTransactions::Hashes(hashes)
}
}

impl<T> From<Vec<T>> for BlockTransactions<T> {
fn from(transactions: Vec<T>) -> Self {
BlockTransactions::Full(transactions)
}
}

/// An iterator over the transaction hashes of a block.
///
/// See [`BlockTransactions::hashes`].
Expand Down Expand Up @@ -581,7 +593,7 @@ mod tests {
parent_beacon_block_root: None,
},
uncles: vec![B256::with_last_byte(17)],
transactions: BlockTransactions::Hashes(vec![B256::with_last_byte(18)]),
transactions: vec![B256::with_last_byte(18)].into(),
size: Some(U256::from(19)),
withdrawals: Some(vec![]),
other: Default::default(),
Expand Down Expand Up @@ -665,7 +677,7 @@ mod tests {
parent_beacon_block_root: None,
},
uncles: vec![B256::with_last_byte(17)],
transactions: BlockTransactions::Hashes(vec![B256::with_last_byte(18)]),
transactions: vec![B256::with_last_byte(18)].into(),
size: Some(U256::from(19)),
withdrawals: None,
other: Default::default(),
Expand Down
16 changes: 14 additions & 2 deletions crates/rpc-types/src/eth/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,18 @@ pub enum FilterId {
Str(String),
}

impl From<u64> for FilterId {
fn from(num: u64) -> Self {
FilterId::Num(num)
}
}

impl From<String> for FilterId {
fn from(str: String) -> Self {
FilterId::Str(str)
}
}

#[cfg(feature = "jsonrpsee-types")]
impl From<FilterId> for jsonrpsee_types::SubscriptionId<'_> {
fn from(value: FilterId) -> Self {
Expand All @@ -957,8 +969,8 @@ impl From<FilterId> for jsonrpsee_types::SubscriptionId<'_> {
impl From<jsonrpsee_types::SubscriptionId<'_>> for FilterId {
fn from(value: jsonrpsee_types::SubscriptionId<'_>) -> Self {
match value {
jsonrpsee_types::SubscriptionId::Num(n) => FilterId::Num(n),
jsonrpsee_types::SubscriptionId::Str(s) => FilterId::Str(s.into_owned()),
jsonrpsee_types::SubscriptionId::Num(n) => n.into(),
jsonrpsee_types::SubscriptionId::Str(s) => s.into_owned().into(),
}
}
}
Expand Down
Loading