Skip to content

Commit

Permalink
feat(alloy-rpc-types-eth): Optional serde (#1276)
Browse files Browse the repository at this point in the history
* feat: optional serde

* feat: optional serde

* fix: tests

* fix: enable serde for alloy-rpc-types-trace

* fix: non-attr serde
  • Loading branch information
refcell authored Sep 12, 2024
1 parent 9362494 commit 8c5aff5
Show file tree
Hide file tree
Showing 23 changed files with 622 additions and 334 deletions.
2 changes: 1 addition & 1 deletion crates/contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ workspace = true
alloy-network.workspace = true
alloy-network-primitives.workspace = true
alloy-provider.workspace = true
alloy-rpc-types-eth.workspace = true
alloy-rpc-types-eth = { workspace = true, features = ["serde"] }
alloy-transport.workspace = true

alloy-dyn-abi = { workspace = true, features = ["std"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ alloy-eips = { workspace = true, features = ["serde"] }
alloy-json-rpc.workspace = true
alloy-network-primitives.workspace = true
alloy-primitives.workspace = true
alloy-rpc-types-eth.workspace = true
alloy-rpc-types-eth = { workspace = true, features = ["std", "serde"] }
alloy-signer.workspace = true
alloy-serde.workspace = true
alloy-sol-types.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ alloy-signer-local = { workspace = true, optional = true }
alloy-rpc-client.workspace = true
alloy-rpc-types-admin = { workspace = true, optional = true }
alloy-rpc-types-anvil = { workspace = true, optional = true }
alloy-rpc-types-eth.workspace = true
alloy-rpc-types-eth = { workspace = true, features = ["serde"] }
alloy-rpc-types-trace = { workspace = true, optional = true }
alloy-rpc-types-txpool = { workspace = true, optional = true }
alloy-rpc-types-engine = { workspace = true, optional = true }
Expand Down
16 changes: 9 additions & 7 deletions crates/rpc-types-eth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ rustdoc-args = ["--cfg", "docsrs"]
workspace = true

[dependencies]
alloy-eips.workspace = true
alloy-serde.workspace = true
alloy-consensus.workspace = true
alloy-network-primitives.workspace = true
alloy-rlp = { workspace = true, features = ["arrayvec", "derive"] }
alloy-primitives = { workspace = true, features = ["rlp", "serde"] }
alloy-consensus = { workspace = true, features = ["serde"] }
alloy-eips = { workspace = true, features = ["serde"] }
alloy-primitives = { workspace = true, features = ["rlp"] }

itertools.workspace = true
derive_more = { workspace = true, features = ["display"] }

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

# `no_std` compatibility
cfg-if.workspace = true
Expand Down Expand Up @@ -59,8 +60,9 @@ similar-asserts.workspace = true
assert_matches.workspace = true

[features]
default = ["std"]
default = ["std", "serde"]
std = ["alloy-primitives/std", "alloy-consensus/std", "alloy-eips/std"]
serde = ["dep:serde", "dep:serde_json", "alloy-primitives/serde", "alloy-consensus/serde", "alloy-eips/serde"]
arbitrary = [
"std",
"dep:arbitrary",
Expand Down
27 changes: 16 additions & 11 deletions crates/rpc-types-eth/src/account.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use alloy_primitives::{Address, Bytes, B256, B512, U256};
use alloy_serde::storage::JsonStorageKey;
use serde::{Deserialize, Serialize};

use alloc::{string::String, vec::Vec};

// re-export account type for `eth_getAccount`
pub use alloy_consensus::Account;

/// Account information.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct AccountInfo {
/// Account name
pub name: String,
}

/// Data structure with proof for one single storage-entry
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct EIP1186StorageProof {
/// Storage key.
pub key: JsonStorageKey,
Expand All @@ -27,8 +28,9 @@ pub struct EIP1186StorageProof {
}

/// Response for EIP-1186 account proof `eth_getProof`
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct EIP1186AccountProofResponse {
/// The account address.
pub address: Address,
Expand All @@ -37,7 +39,7 @@ pub struct EIP1186AccountProofResponse {
/// The hash of the code of the account.
pub code_hash: B256,
/// The account nonce.
#[serde(with = "alloy_serde::quantity")]
#[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity"))]
pub nonce: u64,
/// The hash of the storage of the account.
pub storage_hash: B256,
Expand All @@ -48,22 +50,24 @@ pub struct EIP1186AccountProofResponse {
}

/// Extended account information (used by `parity_allAccountInfo`).
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ExtAccountInfo {
/// Account name
pub name: String,
/// Account meta JSON
pub meta: String,
/// Account UUID (`None` for address book entries)
#[serde(skip_serializing_if = "Option::is_none")]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub uuid: Option<String>,
}

/// account derived from a signature
/// as well as information that tells if it is valid for
/// the current chain
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub struct RecoveredAccount {
/// address of the recovered account
pub address: Address,
Expand All @@ -76,6 +80,7 @@ pub struct RecoveredAccount {
}

#[test]
#[cfg(feature = "serde")]
fn test_eip_1186_account_without_storage_proof() {
let response = r#"{
"address":"0xc36442b4a4522e871399cd717abdd847ab11fe88",
Expand Down
Loading

0 comments on commit 8c5aff5

Please sign in to comment.