Skip to content

Commit

Permalink
fixup(serde): Fix serde conditionals on eip6110/7002 using cfg_eval
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanschneider committed Dec 13, 2024
1 parent 4e5eae5 commit a56d2e9
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/eips/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ ethereum_ssz = { workspace = true, optional = true }

# arbitrary
arbitrary = { workspace = true, features = ["derive"], optional = true }
cfg_eval = "0.1.2"


[dev-dependencies]
Expand Down
7 changes: 4 additions & 3 deletions crates/eips/src/eip6110.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ pub const MAINNET_DEPOSIT_CONTRACT_ADDRESS: Address =
pub const DEPOSIT_REQUEST_TYPE: u8 = 0x00;

/// This structure maps onto the deposit object from [EIP-6110](https://eips.ethereum.org/EIPS/eip-6110).
#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature="serde", cfg_eval::cfg_eval, serde_as)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ssz", derive(ssz_derive::Encode, ssz_derive::Decode))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
Expand All @@ -25,12 +25,12 @@ pub struct DepositRequest {
/// Withdrawal credentials
pub withdrawal_credentials: B256,
/// Amount of ether deposited in gwei
#[serde_as(as = "DisplayFromStr")]
#[cfg_attr(feature="serde", serde_as(as = "DisplayFromStr"))]
pub amount: u64,
/// Deposit signature
pub signature: FixedBytes<96>,
/// Deposit index
#[serde_as(as = "DisplayFromStr")]
#[cfg_attr(feature="serde", serde_as(as = "DisplayFromStr"))]
pub index: u64,
}

Expand All @@ -40,6 +40,7 @@ mod tests {
use alloy_primitives::hex;

#[test]
#[cfg(feature = "serde")]
fn test_serde_deposit_request() {
// Sample JSON input representing a deposit request
let json_data = r#"{"pubkey":"0x8e01a8f21bdc38991ada53ca86d6c78d874675a450a38431cc6aa0f12d5661e344784c56c8a211f7025224d1303ee801","withdrawal_credentials":"0x010000000000000000000000af6df504f08ddf582d604d2f0a593bc153c25dbd","amount":"18112749083033600","signature":"0xb65f3db79405544528d6d92040282f29171f4ff6e5abb2d59f9ee1f1254aced2a7000f87bc2684f543e913a7cc1007ea0e97289b349c553eecdf253cd3ef5814088ba3d4ac286f2634dac3d026d9a01e4c166dc75e249d626a0f1c180dab75ce","index":"13343631333247680512"}"#;
Expand Down
5 changes: 3 additions & 2 deletions crates/eips/src/eip7002.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ pub const WITHDRAWAL_REQUEST_TYPE: u8 = 0x01;
/// Represents an execution layer triggerable withdrawal request.
///
/// See [EIP-7002](https://eips.ethereum.org/EIPS/eip-7002).
#[cfg_attr(feature = "serde", serde_as)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
#[cfg_attr(feature="serde", cfg_eval::cfg_eval, serde_as)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "ssz", derive(ssz_derive::Encode, ssz_derive::Decode))]
#[cfg_attr(any(test, feature = "arbitrary"), derive(arbitrary::Arbitrary))]
Expand All @@ -34,7 +34,7 @@ pub struct WithdrawalRequest {
/// Validator public key.
pub validator_pubkey: FixedBytes<48>,
/// Amount of withdrawn ether in gwei.
#[serde_as(as = "DisplayFromStr")]
#[cfg_attr(feature="serde", serde_as(as = "DisplayFromStr"))]
pub amount: u64,
}

Expand All @@ -45,6 +45,7 @@ mod tests {
use core::str::FromStr;

#[test]
#[cfg(feature = "serde")]
fn test_serde_withdrawal_request() {
// Sample JSON input representing a withdrawal request
let json_data = r#"{
Expand Down

0 comments on commit a56d2e9

Please sign in to comment.