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

chore: rm Rich type #1195

Merged
merged 1 commit into from
Aug 26, 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
45 changes: 2 additions & 43 deletions crates/rpc-types-eth/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use alloy_network_primitives::{
};
use alloy_primitives::{Address, BlockHash, Bloom, Bytes, B256, B64, U256};
use alloy_serde::WithOtherFields;
use serde::{ser::Error, Deserialize, Serialize, Serializer};
use std::{collections::BTreeMap, ops::Deref};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

pub use alloy_eips::{
calc_blob_gasprice, calc_excess_blob_gas, BlockHashOrNumber, BlockId, BlockNumHash,
Expand Down Expand Up @@ -249,47 +249,6 @@ impl From<Header> for WithOtherFields<Header> {
}
}

/// Value representation with additional info
#[derive(Clone, Debug, PartialEq, Eq, Deserialize)]
pub struct Rich<T> {
/// Standard value.
#[serde(flatten)]
pub inner: T,
/// Additional fields that should be serialized into the `Block` object
#[serde(flatten)]
pub extra_info: BTreeMap<String, serde_json::Value>,
}

impl<T> Deref for Rich<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl<T: Serialize> Serialize for Rich<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if self.extra_info.is_empty() {
return self.inner.serialize(serializer);
}

let inner = serde_json::to_value(&self.inner);
let extras = serde_json::to_value(&self.extra_info);

if let (Ok(serde_json::Value::Object(mut value)), Ok(serde_json::Value::Object(extras))) =
(inner, extras)
{
value.extend(extras);
value.serialize(serializer)
} else {
Err(S::Error::custom("Unserializable structures: expected objects"))
}
}
}

/// BlockOverrides is a set of header fields to override.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default, rename_all = "camelCase", deny_unknown_fields)]
Expand Down
20 changes: 6 additions & 14 deletions crates/rpc-types-trace/src/otterscan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! <https://github.com/otterscan/otterscan/blob/develop/docs/custom-jsonrpc.md>

use alloy_primitives::{Address, Bloom, Bytes, TxHash, B256, U256};
use alloy_rpc_types_eth::{Block, Header, Rich, Transaction, TransactionReceipt, Withdrawal};
use alloy_rpc_types_eth::{Block, Header, Transaction, TransactionReceipt, Withdrawal};
use serde::{
de::{self, Unexpected},
Deserialize, Deserializer, Serialize, Serializer,
Expand Down Expand Up @@ -162,24 +162,16 @@ pub struct BlockDetails {
pub total_fees: U256,
}

impl<T> From<Rich<Block<T>>> for BlockDetails {
fn from(rich_block: Rich<Block<T>>) -> Self {
Self {
block: rich_block.inner.into(),
issuance: Default::default(),
total_fees: U256::default(),
}
impl<T> From<Block<T>> for BlockDetails {
fn from(block: Block<T>) -> Self {
Self { block: block.into(), issuance: Default::default(), total_fees: U256::default() }
}
}

impl BlockDetails {
/// Create a new `BlockDetails` struct.
pub fn new<T>(
rich_block: Rich<Block<T>>,
issuance: InternalIssuance,
total_fees: U256,
) -> Self {
Self { block: rich_block.inner.into(), issuance, total_fees }
pub fn new<T>(block: Block<T>, issuance: InternalIssuance, total_fees: U256) -> Self {
Self { block: block.into(), issuance, total_fees }
}
}

Expand Down