Skip to content

Commit

Permalink
Move alloy-rpc-types serde_helpers mod to standalone crate `alloy…
Browse files Browse the repository at this point in the history
…-serde` (alloy-rs#259)

* Move `alloy-rpc-types` `serde_helpers` mod to standalone crate

* Add header to lib.rs

* format

* Fix serde docs
  • Loading branch information
moricho authored Mar 12, 2024
1 parent 471e75c commit e4612eb
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 26 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ alloy-rpc-client = { version = "0.1.0", default-features = false, path = "crates
alloy-rpc-engine-types = { version = "0.1.0", default-features = false, path = "crates/rpc-engine-types" }
alloy-rpc-trace-types = { version = "0.1.0", default-features = false, path = "crates/rpc-trace-types" }
alloy-rpc-types = { version = "0.1.0", default-features = false, path = "crates/rpc-types" }
alloy-serde = { version = "0.1.0", default-features = false, path = "crates/serde" }
alloy-signer = { version = "0.1.0", default-features = false, path = "crates/signer" }
alloy-signer-aws = { version = "0.1.0", default-features = false, path = "crates/signer-aws" }
alloy-signer-gcp = { version = "0.1.0", default-features = false, path = "crates/signer-gcp" }
Expand Down
4 changes: 2 additions & 2 deletions crates/genesis/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ exclude.workspace = true

[dependencies]
alloy-primitives.workspace = true
alloy-rpc-types.workspace = true
alloy-serde.workspace = true

# serde
serde.workspace = true

[dev-dependencies]
serde_json.workspace = true
serde_json.workspace = true
2 changes: 1 addition & 1 deletion crates/genesis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types::serde_helpers::{
use alloy_serde::{
json_u256::{deserialize_json_ttd_opt, deserialize_json_u256},
num::{u64_hex_or_decimal, u64_hex_or_decimal_opt},
storage::deserialize_storage_map,
Expand Down
15 changes: 13 additions & 2 deletions crates/rpc-engine-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ exclude.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-rpc-types.workspace = true
alloy-serde.workspace = true

ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }
Expand All @@ -28,10 +29,20 @@ jsonrpsee-types = { version = "0.20", optional = true }

[features]
jsonrpsee-types = ["dep:jsonrpsee-types"]
ssz = ["dep:ethereum_ssz" ,"dep:ethereum_ssz_derive", "alloy-primitives/ssz", "alloy-rpc-types/ssz"]
ssz = [
"dep:ethereum_ssz",
"dep:ethereum_ssz_derive",
"alloy-primitives/ssz",
"alloy-rpc-types/ssz",
]

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde", "arbitrary"] }
alloy-primitives = { workspace = true, features = [
"rand",
"rlp",
"serde",
"arbitrary",
] }

arbitrary = { workspace = true, features = ["derive"] }
rand.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-engine-types/src/optimism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub struct OptimismPayloadAttributes {
/// If set, this sets the exact gas limit the block produced with.
#[serde(
skip_serializing_if = "Option::is_none",
deserialize_with = "alloy_rpc_types::serde_helpers::u64_hex_opt::deserialize"
deserialize_with = "alloy_serde::u64_hex_opt::deserialize"
)]
pub gas_limit: Option<u64>,
}
Expand Down
14 changes: 7 additions & 7 deletions crates/rpc-engine-types/src/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ pub struct ExecutionPayloadV1 {
pub receipts_root: B256,
pub logs_bloom: Bloom,
pub prev_randao: B256,
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub block_number: u64,
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub gas_limit: u64,
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub gas_used: u64,
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub timestamp: u64,
pub extra_data: Bytes,
pub base_fee_per_gas: U256,
Expand Down Expand Up @@ -272,11 +272,11 @@ pub struct ExecutionPayloadV3 {

/// Array of hex [`u64`] representing blob gas used, enabled with V3
/// See <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#ExecutionPayloadV3>
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub blob_gas_used: u64,
/// Array of hex[`u64`] representing excess blob gas, enabled with V3
/// See <https://github.com/ethereum/execution-apis/blob/fe8e13c288c592ec154ce25c534e26cb7ce0530d/src/engine/cancun.md#ExecutionPayloadV3>
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub excess_blob_gas: u64,
}

Expand Down Expand Up @@ -664,7 +664,7 @@ pub struct ExecutionPayloadBodyV1 {
#[serde(rename_all = "camelCase")]
pub struct PayloadAttributes {
/// Value for the `timestamp` field of the new payload
#[serde(with = "alloy_rpc_types::serde_helpers::u64_hex")]
#[serde(with = "alloy_serde::u64_hex")]
pub timestamp: u64,
/// Value for the `prevRandao` field of the new payload
pub prev_randao: B256,
Expand Down
8 changes: 7 additions & 1 deletion crates/rpc-trace-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ exclude.workspace = true
[dependencies]
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-rpc-types.workspace = true
alloy-serde.workspace = true

serde.workspace = true
serde_json.workspace = true

[dev-dependencies]
alloy-primitives = { workspace = true, features = ["rand", "rlp", "serde", "arbitrary"] }
alloy-primitives = { workspace = true, features = [
"rand",
"rlp",
"serde",
"arbitrary",
] }

arbitrary = { workspace = true, features = ["derive"] }
proptest.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-trace-types/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! `trace_filter` types and support
use alloy_primitives::Address;
use alloy_rpc_types::serde_helpers::num::u64_hex_or_decimal_opt;
use alloy_serde::num::u64_hex_or_decimal_opt;
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-trace-types/src/geth/call.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types::serde_helpers::num::from_int_or_hex;
use alloy_serde::num::from_int_or_hex;
use serde::{Deserialize, Serialize};

/// The response object for `debug_traceTransaction` with `"tracer": "callTracer"`
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-trace-types/src/geth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub struct DefaultFrame {
/// How much gas was used.
pub gas: u64,
/// Output of the transaction
#[serde(serialize_with = "alloy_rpc_types::serde_helpers::serialize_hex_string_no_prefix")]
#[serde(serialize_with = "alloy_serde::serialize_hex_string_no_prefix")]
pub return_value: Bytes,
/// Recorded traces of the transaction
pub struct_logs: Vec<StructLog>,
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-trace-types/src/geth/pre_state.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use alloy_primitives::{Address, Bytes, B256, U256};
use alloy_rpc_types::serde_helpers::num::from_int_or_hex_opt;
use alloy_serde::num::from_int_or_hex_opt;
use serde::{Deserialize, Serialize};
use std::collections::{btree_map, BTreeMap};

Expand Down
1 change: 1 addition & 0 deletions crates/rpc-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exclude.workspace = true
# ethereum
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-serde.workspace = true

ethereum_ssz_derive = { workspace = true, optional = true }
ethereum_ssz = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types/src/eth/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(missing_docs)]
use crate::serde_helpers::storage::JsonStorageKey;
use alloy_primitives::{Address, Bytes, B256, B512, U256, U64};
use alloy_serde::storage::JsonStorageKey;
use serde::{Deserialize, Serialize};

/// Account information.
Expand Down
2 changes: 1 addition & 1 deletion crates/rpc-types/src/eth/withdrawal.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! Withdrawal type and serde helpers.
use crate::serde_helpers::u64_hex;
use alloy_primitives::{Address, U256};
use alloy_rlp::{RlpDecodable, RlpEncodable};
use alloy_serde::u64_hex;
use serde::{Deserialize, Serialize};

/// Multiplier for converting gwei to wei.
Expand Down
3 changes: 1 addition & 2 deletions crates/rpc-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

mod eth;
pub mod serde_helpers;

pub use alloy_serde as serde_helpers;
pub use eth::*;
pub use serde_helpers::*;
27 changes: 27 additions & 0 deletions crates/serde/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "alloy-serde"
description = "Alloy serde helpers"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
license.workspace = true
homepage.workspace = true
authors.workspace = true
repository.workspace = true
exclude.workspace = true

[dependencies]
# ethereum
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }

serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true

[dev-dependencies]
alloy-primitives = { workspace = true, features = [
"rand",
"rlp",
"serde",
"arbitrary",
] }
3 changes: 3 additions & 0 deletions crates/serde/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# alloy-serde

Serde related helpers for Alloy.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
//! Serde helpers for primitive types.
//! Alloy serde helpers for primitive types.
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/alloy.jpg",
html_favicon_url = "https://raw.githubusercontent.com/alloy-rs/core/main/assets/favicon.ico"
)]
#![warn(
missing_copy_implementations,
missing_debug_implementations,
missing_docs,
unreachable_pub,
clippy::missing_const_for_fn,
rustdoc::all
)]
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![deny(unused_must_use, rust_2018_idioms)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

use alloy_primitives::B256;
use serde::Serializer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::str::FromStr;
///
///
/// ```rust
/// use alloy_rpc_types::num::U64HexOrNumber;
/// use alloy_serde::num::U64HexOrNumber;
/// let number_json = "100";
/// let hex_json = "\"0x64\"";
///
Expand Down Expand Up @@ -113,7 +113,7 @@ pub mod u64_hex_opt {

/// serde functions for handling primitive `u64` as [U64]
pub mod u64_hex_or_decimal {
use crate::serde_helpers::num::U64HexOrNumber;
use crate::num::U64HexOrNumber;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Deserializes an `u64` accepting a hex quantity string with optional 0x prefix or
Expand All @@ -133,7 +133,7 @@ pub mod u64_hex_or_decimal {

/// serde functions for handling primitive optional `u64` as [U64]
pub mod u64_hex_or_decimal_opt {
use crate::serde_helpers::num::U64HexOrNumber;
use crate::num::U64HexOrNumber;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// Deserializes an `u64` accepting a hex quantity string with optional 0x prefix or
Expand Down
File renamed without changes.
File renamed without changes.

0 comments on commit e4612eb

Please sign in to comment.