diff --git a/crates/consensus/src/receipt/any.rs b/crates/consensus/src/receipt/any.rs index 99547656710..a7bb8b1b824 100644 --- a/crates/consensus/src/receipt/any.rs +++ b/crates/consensus/src/receipt/any.rs @@ -20,7 +20,7 @@ pub struct AnyReceiptEnvelope { #[cfg_attr(feature = "serde", serde(flatten))] pub inner: ReceiptWithBloom, /// The transaction type. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u8_hex"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u8_via_ruint"))] pub r#type: u8, } diff --git a/crates/consensus/src/receipt/receipts.rs b/crates/consensus/src/receipt/receipts.rs index c7e9d69d63b..f4362fe5714 100644 --- a/crates/consensus/src/receipt/receipts.rs +++ b/crates/consensus/src/receipt/receipts.rs @@ -19,7 +19,7 @@ pub struct Receipt { #[cfg_attr(feature = "serde", serde(with = "alloy_serde::quantity_bool"))] pub status: bool, /// Gas used - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub cumulative_gas_used: u128, /// Log send from contracts. pub logs: Vec, diff --git a/crates/consensus/src/transaction/eip1559.rs b/crates/consensus/src/transaction/eip1559.rs index 871a6169e4f..b1972571c93 100644 --- a/crates/consensus/src/transaction/eip1559.rs +++ b/crates/consensus/src/transaction/eip1559.rs @@ -13,17 +13,17 @@ use alloc::vec::Vec; #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct TxEip1559 { /// EIP-155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub chain_id: ChainId, /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub nonce: u64, /// A scalar value equal to the maximum /// amount of gas that should be used in executing /// this transaction. This is paid up-front, before any /// computation is done and may not be increased /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_limit: u128, /// A scalar value equal to the maximum /// amount of gas that should be used in executing @@ -36,7 +36,7 @@ pub struct TxEip1559 { /// 340282366920938463463374607431768211455 /// /// This is also known as `GasFeeCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub max_fee_per_gas: u128, /// Max Priority fee that transaction is paying /// @@ -45,7 +45,7 @@ pub struct TxEip1559 { /// 340282366920938463463374607431768211455 /// /// This is also known as `GasTipCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub max_priority_fee_per_gas: u128, /// The 160-bit address of the message call’s recipient or, for a contract creation /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. diff --git a/crates/consensus/src/transaction/eip2930.rs b/crates/consensus/src/transaction/eip2930.rs index e4f09d0c3e3..0005ee85eb7 100644 --- a/crates/consensus/src/transaction/eip2930.rs +++ b/crates/consensus/src/transaction/eip2930.rs @@ -13,10 +13,10 @@ use alloc::vec::Vec; #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct TxEip2930 { /// Added as EIP-pub 155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub chain_id: ChainId, /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub nonce: u64, /// A scalar value equal to the number of /// Wei to be paid per unit of gas for all computation @@ -25,14 +25,14 @@ pub struct TxEip2930 { /// As ethereum circulation is around 120mil eth as of 2022 that is around /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: /// 340282366920938463463374607431768211455 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_price: u128, /// A scalar value equal to the maximum /// amount of gas that should be used in executing /// this transaction. This is paid up-front, before any /// computation is done and may not be increased /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_limit: u128, /// The 160-bit address of the message call’s recipient or, for a contract creation /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. diff --git a/crates/consensus/src/transaction/eip4844.rs b/crates/consensus/src/transaction/eip4844.rs index be266be80ba..c1258bf8a00 100644 --- a/crates/consensus/src/transaction/eip4844.rs +++ b/crates/consensus/src/transaction/eip4844.rs @@ -335,17 +335,17 @@ impl SignableTransaction for TxEip4844Variant { #[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))] pub struct TxEip4844 { /// Added as EIP-pub 155: Simple replay attack protection - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub chain_id: ChainId, /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub nonce: u64, /// A scalar value equal to the maximum /// amount of gas that should be used in executing /// this transaction. This is paid up-front, before any /// computation is done and may not be increased /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_limit: u128, /// A scalar value equal to the maximum /// amount of gas that should be used in executing @@ -358,7 +358,7 @@ pub struct TxEip4844 { /// 340282366920938463463374607431768211455 /// /// This is also known as `GasFeeCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub max_fee_per_gas: u128, /// Max Priority fee that transaction is paying /// @@ -367,7 +367,7 @@ pub struct TxEip4844 { /// 340282366920938463463374607431768211455 /// /// This is also known as `GasTipCap` - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub max_priority_fee_per_gas: u128, /// The 160-bit address of the message call’s recipient. pub to: Address, @@ -389,7 +389,7 @@ pub struct TxEip4844 { /// Max fee per data gas /// /// aka BlobFeeCap or blobGasFeeCap - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub max_fee_per_blob_gas: u128, /// Input has two uses depending if transaction is Create or Call (if `to` field is None or diff --git a/crates/consensus/src/transaction/legacy.rs b/crates/consensus/src/transaction/legacy.rs index 424b5d8927e..dd08d1d2b27 100644 --- a/crates/consensus/src/transaction/legacy.rs +++ b/crates/consensus/src/transaction/legacy.rs @@ -16,13 +16,13 @@ pub struct TxLegacy { feature = "serde", serde( default, - with = "alloy_serde::u64_hex_or_decimal_opt", + with = "alloy_serde::u64_opt_via_ruint", skip_serializing_if = "Option::is_none", ) )] pub chain_id: Option, /// A scalar value equal to the number of transactions sent by the sender; formally Tn. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub nonce: u64, /// A scalar value equal to the number of /// Wei to be paid per unit of gas for all computation @@ -31,14 +31,14 @@ pub struct TxLegacy { /// As ethereum circulation is around 120mil eth as of 2022 that is around /// 120000000000000000000000000 wei we are safe to use u128 as its max number is: /// 340282366920938463463374607431768211455 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_price: u128, /// A scalar value equal to the maximum /// amount of gas that should be used in executing /// this transaction. This is paid up-front, before any /// computation is done and may not be increased /// later; formally Tg. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u128_via_ruint"))] pub gas_limit: u128, /// The 160-bit address of the message call’s recipient or, for a contract creation /// transaction, ∅, used here to denote the only member of B0 ; formally Tt. diff --git a/crates/eip7547/src/summary.rs b/crates/eip7547/src/summary.rs index f444d4c693f..b47adfc426a 100644 --- a/crates/eip7547/src/summary.rs +++ b/crates/eip7547/src/summary.rs @@ -3,7 +3,7 @@ use alloy_primitives::{Address, B256}; use alloy_rpc_types_engine::PayloadStatusEnum; -use alloy_serde::u64_hex; +use alloy_serde::u64_via_ruint; use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer}; use std::fmt; @@ -83,7 +83,7 @@ pub struct InclusionListSummaryEntryV1 { /// The address of the inclusion list entry. pub address: Address, /// The nonce of the inclusion list entry. - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] pub nonce: u64, } @@ -108,10 +108,10 @@ impl fmt::Display for InclusionListSummaryEntryV1 { #[serde(rename_all = "camelCase")] pub struct InclusionListSummaryV1 { /// The slot of the inclusion list summary. - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] pub slot: u64, /// The proposer index of the inclusion list summary. - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] pub proposer_index: u64, /// The parent hash of the inclusion list summary. pub parent_hash: B256, diff --git a/crates/eips/src/eip1559/basefee.rs b/crates/eips/src/eip1559/basefee.rs index 3bc6b38d0e7..7bd7b5362d6 100644 --- a/crates/eips/src/eip1559/basefee.rs +++ b/crates/eips/src/eip1559/basefee.rs @@ -8,10 +8,10 @@ use crate::{ #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct BaseFeeParams { /// The base_fee_max_change_denominator from EIP-1559 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_via_ruint"))] pub max_change_denominator: u128, /// The elasticity multiplier from EIP-1559 - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_hex_or_decimal"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::num::u128_via_ruint"))] pub elasticity_multiplier: u128, } diff --git a/crates/eips/src/eip4895.rs b/crates/eips/src/eip4895.rs index dc99dfa5909..e96538f076f 100644 --- a/crates/eips/src/eip4895.rs +++ b/crates/eips/src/eip4895.rs @@ -20,15 +20,18 @@ pub const GWEI_TO_WEI: u64 = 1_000_000_000; #[cfg_attr(feature = "ssz", derive(ssz_derive::Encode, ssz_derive::Decode))] pub struct Withdrawal { /// Monotonically increasing identifier issued by consensus layer. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub index: u64, /// Index of validator associated with withdrawal. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex", rename = "validatorIndex"))] + #[cfg_attr( + feature = "serde", + serde(with = "alloy_serde::u64_via_ruint", rename = "validatorIndex") + )] pub validator_index: u64, /// Target address for withdrawn ether. pub address: Address, /// Value of the withdrawal in gwei. - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub amount: u64, } diff --git a/crates/eips/src/eip6110.rs b/crates/eips/src/eip6110.rs index bfc9819dfb5..9c71382a264 100644 --- a/crates/eips/src/eip6110.rs +++ b/crates/eips/src/eip6110.rs @@ -27,11 +27,11 @@ pub struct DepositReceipt { /// Withdrawal credentials pub withdrawal_credentials: B256, /// Amount of ether deposited in gwei - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub amount: u64, /// Deposit signature pub signature: FixedBytes<96>, /// Deposit index - #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_hex"))] + #[cfg_attr(feature = "serde", serde(with = "alloy_serde::u64_via_ruint"))] pub index: u64, } diff --git a/crates/genesis/src/lib.rs b/crates/genesis/src/lib.rs index ccad1c670b7..2b2b6c2c0c3 100644 --- a/crates/genesis/src/lib.rs +++ b/crates/genesis/src/lib.rs @@ -24,7 +24,7 @@ use alloc::collections::BTreeMap; use alloy_primitives::{Address, Bytes, B256, U256}; use alloy_serde::{ - num::{u128_hex_or_decimal, u128_hex_or_decimal_opt, u64_hex, u64_hex_or_decimal_opt}, + num::{u128_opt_via_ruint, u128_via_ruint, u64_opt_via_ruint, u64_via_ruint}, storage::deserialize_storage_map, ttd::deserialize_json_ttd_opt, }; @@ -38,15 +38,15 @@ pub struct Genesis { #[serde(default)] pub config: ChainConfig, /// The genesis header nonce. - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] pub nonce: u64, /// The genesis header timestamp. - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] pub timestamp: u64, /// The genesis header extra data. pub extra_data: Bytes, /// The genesis header gas limit. - #[serde(with = "u128_hex_or_decimal")] + #[serde(with = "u128_via_ruint")] pub gas_limit: u128, /// The genesis header difficulty. pub difficulty: U256, @@ -64,16 +64,16 @@ pub struct Genesis { // should NOT be set in a real genesis file, but are included here for compatibility with // consensus tests, which have genesis files with these fields populated. /// The genesis header base fee - #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_opt_via_ruint")] pub base_fee_per_gas: Option, /// The genesis header excess blob gas - #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_opt_via_ruint")] pub excess_blob_gas: Option, /// The genesis header blob gas used - #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_hex_or_decimal_opt")] + #[serde(default, skip_serializing_if = "Option::is_none", with = "u128_opt_via_ruint")] pub blob_gas_used: Option, /// The genesis block number - #[serde(default, skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt")] + #[serde(default, skip_serializing_if = "Option::is_none", with = "u64_opt_via_ruint")] pub number: Option, } @@ -215,7 +215,7 @@ impl Genesis { #[serde(deny_unknown_fields)] pub struct GenesisAccount { /// The nonce of the account at genesis. - #[serde(skip_serializing_if = "Option::is_none", with = "u64_hex_or_decimal_opt", default)] + #[serde(skip_serializing_if = "Option::is_none", with = "u64_opt_via_ruint", default)] pub nonce: Option, /// The balance of the account at genesis. pub balance: U256, @@ -281,14 +281,14 @@ pub struct ChainConfig { /// The homestead switch block (None = no fork, 0 = already homestead). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub homestead_block: Option, /// The DAO fork switch block (None = no fork). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub dao_fork_block: Option, @@ -298,7 +298,7 @@ pub struct ChainConfig { /// The [EIP-150](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-150.md) hard fork block (None = no fork). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub eip150_block: Option, @@ -309,98 +309,98 @@ pub struct ChainConfig { /// The [EIP-155](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-155.md) hard fork block. #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub eip155_block: Option, /// The [EIP-158](https://github.com/ethereum/EIPs/blob/master/EIPS/eip-158.md) hard fork block. #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub eip158_block: Option, /// The Byzantium hard fork block (None = no fork, 0 = already on byzantium). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub byzantium_block: Option, /// The Constantinople hard fork block (None = no fork, 0 = already on constantinople). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub constantinople_block: Option, /// The Petersburg hard fork block (None = no fork, 0 = already on petersburg). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub petersburg_block: Option, /// The Istanbul hard fork block (None = no fork, 0 = already on istanbul). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub istanbul_block: Option, /// The Muir Glacier hard fork block (None = no fork, 0 = already on muir glacier). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub muir_glacier_block: Option, /// The Berlin hard fork block (None = no fork, 0 = already on berlin). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub berlin_block: Option, /// The London hard fork block (None = no fork, 0 = already on london). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub london_block: Option, /// The Arrow Glacier hard fork block (None = no fork, 0 = already on arrow glacier). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub arrow_glacier_block: Option, /// The Gray Glacier hard fork block (None = no fork, 0 = already on gray glacier). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub gray_glacier_block: Option, /// Virtual fork after the merge to use as a network splitter. #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub merge_netsplit_block: Option, /// Shanghai switch time (None = no fork, 0 = already on shanghai). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub shanghai_time: Option, /// Cancun switch time (None = no fork, 0 = already on cancun). #[serde( skip_serializing_if = "Option::is_none", - deserialize_with = "u64_hex_or_decimal_opt::deserialize" + deserialize_with = "u64_opt_via_ruint::deserialize" )] pub cancun_time: Option, diff --git a/crates/rpc-types-anvil/src/lib.rs b/crates/rpc-types-anvil/src/lib.rs index f8e9f533605..7a57fae059e 100644 --- a/crates/rpc-types-anvil/src/lib.rs +++ b/crates/rpc-types-anvil/src/lib.rs @@ -40,7 +40,7 @@ impl<'de> serde::Deserialize<'de> for Forking { #[serde(rename_all = "camelCase")] struct ForkOpts { json_rpc_url: Option, - #[serde(default, with = "alloy_serde::u64_hex_or_decimal_opt")] + #[serde(default, with = "alloy_serde::u64_opt_via_ruint")] block_number: Option, } @@ -72,7 +72,7 @@ impl<'de> serde::Deserialize<'de> for Forking { #[serde(rename_all = "camelCase")] pub struct NodeInfo { /// The current block number - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub current_block_number: u64, /// The current block timestamp pub current_block_timestamp: u64, @@ -156,14 +156,14 @@ pub enum MineOptions { /// The options for mining Options { /// The timestamp the block should be mined with - #[serde(with = "alloy_serde::u64_hex_or_decimal_opt")] + #[serde(with = "alloy_serde::u64_opt_via_ruint")] timestamp: Option, /// If `blocks` is given, it will mine exactly blocks number of blocks, regardless of any /// other blocks mined or reverted during it's operation blocks: Option, }, /// The timestamp the block should be mined with - #[serde(with = "alloy_serde::u64_hex_or_decimal_opt")] + #[serde(with = "alloy_serde::u64_opt_via_ruint")] Timestamp(Option), } diff --git a/crates/rpc-types-engine/src/exit.rs b/crates/rpc-types-engine/src/exit.rs index 0b289718ad6..0f22bdb790e 100644 --- a/crates/rpc-types-engine/src/exit.rs +++ b/crates/rpc-types-engine/src/exit.rs @@ -14,6 +14,6 @@ pub struct ExitV1 { /// Validator public key. pub pubkey: FixedBytes<48>, /// Amount of withdrawn ether in gwei. - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub amount: u64, } diff --git a/crates/rpc-types-engine/src/optimism.rs b/crates/rpc-types-engine/src/optimism.rs index b50548ff60b..c5692877028 100644 --- a/crates/rpc-types-engine/src/optimism.rs +++ b/crates/rpc-types-engine/src/optimism.rs @@ -17,7 +17,7 @@ pub struct OptimismPayloadAttributes { #[serde(skip_serializing_if = "Option::is_none")] pub no_tx_pool: Option, /// If set, this sets the exact gas limit the block produced with. - #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_hex_opt")] + #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_opt_via_ruint")] pub gas_limit: Option, } diff --git a/crates/rpc-types-engine/src/payload.rs b/crates/rpc-types-engine/src/payload.rs index f36bf4ed003..f5a47ef36d2 100644 --- a/crates/rpc-types-engine/src/payload.rs +++ b/crates/rpc-types-engine/src/payload.rs @@ -137,16 +137,16 @@ pub struct ExecutionPayloadV1 { /// The previous randao of the block. pub prev_randao: B256, /// The block number. - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub block_number: u64, /// The gas limit of the block. - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub gas_limit: u64, /// The gas used of the block. - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub gas_used: u64, /// The timestamp of the block. - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub timestamp: u64, /// The extra data of the block. pub extra_data: Bytes, @@ -283,11 +283,11 @@ pub struct ExecutionPayloadV3 { /// Array of hex [`u64`] representing blob gas used, enabled with V3 /// See - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub blob_gas_used: u64, /// Array of hex[`u64`] representing excess blob gas, enabled with V3 /// See - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub excess_blob_gas: u64, } @@ -774,7 +774,7 @@ pub struct ExecutionPayloadBodyV1 { #[serde(rename_all = "camelCase")] pub struct PayloadAttributes { /// Value for the `timestamp` field of the new payload - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub timestamp: u64, /// Value for the `prevRandao` field of the new payload pub prev_randao: B256, diff --git a/crates/rpc-types-trace/src/filter.rs b/crates/rpc-types-trace/src/filter.rs index 7fd47ca86d2..5652d5d221a 100644 --- a/crates/rpc-types-trace/src/filter.rs +++ b/crates/rpc-types-trace/src/filter.rs @@ -1,6 +1,6 @@ //! `trace_filter` types and support use alloy_primitives::Address; -use alloy_serde::num::u64_hex_or_decimal_opt; +use alloy_serde::num::u64_opt_via_ruint; use serde::{Deserialize, Serialize}; use std::collections::HashSet; @@ -10,10 +10,10 @@ use std::collections::HashSet; #[serde(rename_all = "camelCase")] pub struct TraceFilter { /// From block - #[serde(with = "u64_hex_or_decimal_opt")] + #[serde(with = "u64_opt_via_ruint")] pub from_block: Option, /// To block - #[serde(with = "u64_hex_or_decimal_opt")] + #[serde(with = "u64_opt_via_ruint")] pub to_block: Option, /// From address #[serde(default)] diff --git a/crates/rpc-types-trace/src/geth/call.rs b/crates/rpc-types-trace/src/geth/call.rs index 26b4ae75ff9..76092be2a79 100644 --- a/crates/rpc-types-trace/src/geth/call.rs +++ b/crates/rpc-types-trace/src/geth/call.rs @@ -1,7 +1,6 @@ //! Geth call tracer types. use alloy_primitives::{Address, Bytes, B256, U256}; -use alloy_serde::num::from_int_or_hex; use serde::{Deserialize, Serialize}; /// The response object for `debug_traceTransaction` with `"tracer": "callTracer"`. @@ -12,10 +11,10 @@ pub struct CallFrame { /// The address of that initiated the call. pub from: Address, /// How much gas was left before the call. - #[serde(default, deserialize_with = "from_int_or_hex")] + #[serde(default)] pub gas: U256, /// How much gas was used by the call. - #[serde(default, deserialize_with = "from_int_or_hex", rename = "gasUsed")] + #[serde(default, rename = "gasUsed")] pub gas_used: U256, /// The address of the contract that was called. #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/crates/rpc-types-trace/src/geth/pre_state.rs b/crates/rpc-types-trace/src/geth/pre_state.rs index b60afc20ba1..fb670c1d2aa 100644 --- a/crates/rpc-types-trace/src/geth/pre_state.rs +++ b/crates/rpc-types-trace/src/geth/pre_state.rs @@ -1,7 +1,6 @@ //! Pre-state Geth tracer types. use alloy_primitives::{Address, Bytes, B256, U256}; -use alloy_serde::num::from_int_or_hex_opt; use serde::{Deserialize, Serialize}; use std::collections::{btree_map, BTreeMap}; @@ -126,15 +125,11 @@ impl DiffStateKind { } } -/// Represents the state of an account +/// Represents the state of an account. #[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct AccountState { /// The optional balance of the account. - #[serde( - default, - deserialize_with = "from_int_or_hex_opt", - skip_serializing_if = "Option::is_none" - )] + #[serde(default, skip_serializing_if = "Option::is_none")] pub balance: Option, /// The optional code of the account. #[serde(default, skip_serializing_if = "Option::is_none")] diff --git a/crates/rpc-types-trace/src/otterscan.rs b/crates/rpc-types-trace/src/otterscan.rs index de5066d54a7..b8f459697d7 100644 --- a/crates/rpc-types-trace/src/otterscan.rs +++ b/crates/rpc-types-trace/src/otterscan.rs @@ -96,7 +96,7 @@ pub struct OtsTransactionReceipt { /// Note: the otterscan API sets all log fields to null. #[serde(flatten)] pub receipt: TransactionReceipt, - #[serde(default, with = "alloy_serde::u64_hex_opt")] + #[serde(default, with = "alloy_serde::u64_opt_via_ruint")] pub timestamp: Option, } @@ -110,7 +110,7 @@ pub struct OtsReceipt { #[serde(with = "alloy_serde::quantity_bool")] pub status: bool, /// Gas used - #[serde(with = "alloy_serde::u64_hex")] + #[serde(with = "alloy_serde::u64_via_ruint")] pub cumulative_gas_used: u64, /// Log send from contracts. /// @@ -121,7 +121,7 @@ pub struct OtsReceipt { /// Note: this is set to null pub logs_bloom: Option, /// The transaction type. - #[serde(with = "alloy_serde::num::u8_hex")] + #[serde(with = "alloy_serde::num::u8_via_ruint")] pub r#type: u8, } diff --git a/crates/rpc-types/src/eth/block.rs b/crates/rpc-types/src/eth/block.rs index 788c236640a..61f253e8bc2 100644 --- a/crates/rpc-types/src/eth/block.rs +++ b/crates/rpc-types/src/eth/block.rs @@ -71,16 +71,16 @@ pub struct Header { /// Difficulty pub difficulty: U256, /// Block number - #[serde(default, with = "alloy_serde::num::u64_hex_opt")] + #[serde(default, with = "alloy_serde::num::u64_opt_via_ruint")] pub number: Option, /// Gas Limit - #[serde(default, with = "alloy_serde::num::u128_hex_or_decimal")] + #[serde(default, with = "alloy_serde::num::u128_via_ruint")] pub gas_limit: u128, /// Gas Used - #[serde(default, with = "alloy_serde::num::u128_hex_or_decimal")] + #[serde(default, with = "alloy_serde::num::u128_via_ruint")] pub gas_used: u128, /// Timestamp - #[serde(default, with = "alloy_serde::num::u64_hex")] + #[serde(default, with = "alloy_serde::num::u64_via_ruint")] pub timestamp: u64, /// Total difficulty #[serde(skip_serializing_if = "Option::is_none")] @@ -107,7 +107,7 @@ pub struct Header { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub base_fee_per_gas: Option, /// Withdrawals root hash added by EIP-4895 and is ignored in legacy headers. @@ -117,14 +117,14 @@ pub struct Header { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub blob_gas_used: Option, /// Excess blob gas #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub excess_blob_gas: Option, /// Parent beacon block root diff --git a/crates/rpc-types/src/eth/fee.rs b/crates/rpc-types/src/eth/fee.rs index 4d3e2ea1134..ab4799c1647 100644 --- a/crates/rpc-types/src/eth/fee.rs +++ b/crates/rpc-types/src/eth/fee.rs @@ -39,7 +39,7 @@ pub struct FeeHistory { #[serde( default, skip_serializing_if = "Vec::is_empty", - with = "alloy_serde::num::u128_hex_or_decimal_vec" + with = "alloy_serde::num::u128_vec_via_ruint" )] pub base_fee_per_gas: Vec, /// An array of block gas used ratios. These are calculated as the ratio @@ -56,7 +56,7 @@ pub struct FeeHistory { #[serde( default, skip_serializing_if = "Vec::is_empty", - // with = "alloy_serde::num::u128_hex_or_decimal_vec" + // with = "alloy_serde::num::u128_vec_via_ruint" )] pub base_fee_per_blob_gas: Vec, /// An array of block blob gas used ratios. These are calculated as the ratio of gasUsed and @@ -64,14 +64,14 @@ pub struct FeeHistory { #[serde(default, skip_serializing_if = "Vec::is_empty")] pub blob_gas_used_ratio: Vec, /// Lowest number block of the returned range. - #[serde(default, with = "alloy_serde::num::u64_hex")] + #[serde(default, with = "alloy_serde::num::u64_via_ruint")] pub oldest_block: u64, /// An (optional) array of effective priority fee per gas data points from a single /// block. All zeroes are returned if the block is empty. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_vec_vec_opt" + with = "alloy_serde::num::u128_vec_vec_opt_via_ruint" )] pub reward: Option>>, } diff --git a/crates/rpc-types/src/eth/log.rs b/crates/rpc-types/src/eth/log.rs index 701ffe7c1a8..03df9636f99 100644 --- a/crates/rpc-types/src/eth/log.rs +++ b/crates/rpc-types/src/eth/log.rs @@ -17,20 +17,24 @@ pub struct Log { /// Hash of the block the transaction that emitted this log was mined in pub block_hash: Option, /// Number of the block the transaction that emitted this log was mined in - #[serde(with = "alloy_serde::u64_hex_opt")] + #[serde(with = "alloy_serde::u64_opt_via_ruint")] pub block_number: Option, /// The timestamp of the block as proposed in: /// /// - #[serde(skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_hex_opt", default)] + #[serde( + skip_serializing_if = "Option::is_none", + with = "alloy_serde::u64_opt_via_ruint", + default + )] pub block_timestamp: Option, /// Transaction Hash pub transaction_hash: Option, /// Index of the Transaction in the block - #[serde(with = "alloy_serde::u64_hex_opt")] + #[serde(with = "alloy_serde::u64_opt_via_ruint")] pub transaction_index: Option, /// Log Index in Block - #[serde(with = "alloy_serde::u64_hex_opt")] + #[serde(with = "alloy_serde::u64_opt_via_ruint")] pub log_index: Option, /// Geth Compatibility Field: whether this log was removed #[serde(default)] diff --git a/crates/rpc-types/src/eth/transaction/mod.rs b/crates/rpc-types/src/eth/transaction/mod.rs index 94ac19e4c57..9f7eb4fd9fd 100644 --- a/crates/rpc-types/src/eth/transaction/mod.rs +++ b/crates/rpc-types/src/eth/transaction/mod.rs @@ -38,7 +38,7 @@ pub struct Transaction { /// Hash pub hash: B256, /// Nonce - #[serde(with = "alloy_serde::num::u64_hex")] + #[serde(with = "alloy_serde::num::u64_via_ruint")] pub nonce: u64, /// Block hash #[serde(default, skip_serializing_if = "Option::is_none")] @@ -47,14 +47,14 @@ pub struct Transaction { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u64_hex_opt" + with = "alloy_serde::num::u64_opt_via_ruint" )] pub block_number: Option, /// Transaction Index #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u64_hex_opt" + with = "alloy_serde::num::u64_opt_via_ruint" )] pub transaction_index: Option, /// Sender @@ -67,31 +67,31 @@ pub struct Transaction { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub gas_price: Option, /// Gas amount - #[serde(with = "alloy_serde::num::u128_hex_or_decimal")] + #[serde(with = "alloy_serde::num::u128_via_ruint")] pub gas: u128, /// Max BaseFeePerGas the user is willing to pay. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_fee_per_gas: Option, /// The miner's tip. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_priority_fee_per_gas: Option, /// Configured max fee per blob gas for eip-4844 transactions #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_fee_per_blob_gas: Option, /// Data @@ -102,7 +102,11 @@ pub struct Transaction { #[serde(flatten, skip_serializing_if = "Option::is_none")] pub signature: Option, /// The chain id of the transaction, if any. - #[serde(default, skip_serializing_if = "Option::is_none", with = "alloy_serde::u64_hex_opt")] + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "alloy_serde::u64_opt_via_ruint" + )] pub chain_id: Option, /// Contains the blob hashes for eip-4844 transactions. #[serde(skip_serializing_if = "Option::is_none")] @@ -121,7 +125,7 @@ pub struct Transaction { default, rename = "type", skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u8_hex_opt" + with = "alloy_serde::num::u8_opt_via_ruint" )] pub transaction_type: Option, diff --git a/crates/rpc-types/src/eth/transaction/optimism.rs b/crates/rpc-types/src/eth/transaction/optimism.rs index 0fb20cf9a87..d2f45f9190e 100644 --- a/crates/rpc-types/src/eth/transaction/optimism.rs +++ b/crates/rpc-types/src/eth/transaction/optimism.rs @@ -33,7 +33,7 @@ pub struct OptimismTransactionReceiptFields { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub l1_fee: Option, /// L1 fee scalar for the transaction @@ -43,14 +43,14 @@ pub struct OptimismTransactionReceiptFields { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub l1_gas_price: Option, /// L1 gas used for the transaction #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub l1_gas_used: Option, } diff --git a/crates/rpc-types/src/eth/transaction/receipt.rs b/crates/rpc-types/src/eth/transaction/receipt.rs index 22f31cde721..df30aea3a3b 100644 --- a/crates/rpc-types/src/eth/transaction/receipt.rs +++ b/crates/rpc-types/src/eth/transaction/receipt.rs @@ -22,35 +22,43 @@ pub struct TransactionReceipt> { /// Transaction Hash. pub transaction_hash: B256, /// Index within the block. - #[serde(default, with = "alloy_serde::u64_hex_opt", skip_serializing_if = "Option::is_none")] + #[serde( + default, + with = "alloy_serde::u64_opt_via_ruint", + skip_serializing_if = "Option::is_none" + )] pub transaction_index: Option, /// Hash of the block this transaction was included within. #[serde(default, skip_serializing_if = "Option::is_none")] pub block_hash: Option, /// Number of the block this transaction was included within. - #[serde(default, with = "alloy_serde::u64_hex_opt", skip_serializing_if = "Option::is_none")] + #[serde( + default, + with = "alloy_serde::u64_opt_via_ruint", + skip_serializing_if = "Option::is_none" + )] pub block_number: Option, /// Gas used by this transaction alone. - #[serde(with = "alloy_serde::u128_hex_or_decimal")] + #[serde(with = "alloy_serde::u128_via_ruint")] pub gas_used: u128, /// The price paid post-execution by the transaction (i.e. base fee + priority fee). Both /// fields in 1559-style transactions are maximums (max fee + max priority fee), the amount /// that's actually paid by users can only be determined post-execution - #[serde(with = "alloy_serde::u128_hex_or_decimal")] + #[serde(with = "alloy_serde::u128_via_ruint")] pub effective_gas_price: u128, /// Blob gas used by the eip-4844 transaction /// /// This is None for non eip-4844 transactions #[serde( skip_serializing_if = "Option::is_none", - with = "alloy_serde::u128_hex_or_decimal_opt", + with = "alloy_serde::u128_opt_via_ruint", default )] pub blob_gas_used: Option, /// The price paid by the eip-4844 transaction per blob gas. #[serde( skip_serializing_if = "Option::is_none", - with = "alloy_serde::u128_hex_or_decimal_opt", + with = "alloy_serde::u128_opt_via_ruint", default )] pub blob_gas_price: Option, diff --git a/crates/rpc-types/src/eth/transaction/request.rs b/crates/rpc-types/src/eth/transaction/request.rs index d7d0bd50902..72506007a76 100644 --- a/crates/rpc-types/src/eth/transaction/request.rs +++ b/crates/rpc-types/src/eth/transaction/request.rs @@ -23,35 +23,35 @@ pub struct TransactionRequest { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub gas_price: Option, /// The max base fee per gas the sender is willing to pay. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_fee_per_gas: Option, /// The max priority fee per gas the sender is willing to pay, also called the miner tip. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_priority_fee_per_gas: Option, /// The max fee per blob gas for EIP-4844 blob transactions. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub max_fee_per_blob_gas: Option, /// The gas limit for the transaction. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u128_hex_or_decimal_opt" + with = "alloy_serde::num::u128_opt_via_ruint" )] pub gas: Option, /// The value transferred in the transaction, in wei. @@ -64,14 +64,14 @@ pub struct TransactionRequest { #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u64_hex_opt" + with = "alloy_serde::num::u64_opt_via_ruint" )] pub nonce: Option, /// The chain ID for the transaction. #[serde( default, skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u64_hex_opt" + with = "alloy_serde::num::u64_opt_via_ruint" )] pub chain_id: Option, /// An EIP-2930 access list, which lowers cost for accessing accounts and storages in the list. See [EIP-2930](https://eips.ethereum.org/EIPS/eip-2930) for more information. @@ -82,7 +82,7 @@ pub struct TransactionRequest { default, rename = "type", skip_serializing_if = "Option::is_none", - with = "alloy_serde::num::u8_hex_opt" + with = "alloy_serde::num::u8_opt_via_ruint" )] pub transaction_type: Option, /// Blob versioned hashes for EIP-4844 transactions. diff --git a/crates/rpc-types/src/eth/txpool.rs b/crates/rpc-types/src/eth/txpool.rs index b80bbd66b00..2911e1250d8 100644 --- a/crates/rpc-types/src/eth/txpool.rs +++ b/crates/rpc-types/src/eth/txpool.rs @@ -171,10 +171,10 @@ pub struct TxpoolInspect { #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] pub struct TxpoolStatus { /// number of pending tx - #[serde(with = "alloy_serde::num::u64_hex")] + #[serde(with = "alloy_serde::num::u64_via_ruint")] pub pending: u64, /// number of queued tx - #[serde(with = "alloy_serde::num::u64_hex")] + #[serde(with = "alloy_serde::num::u64_via_ruint")] pub queued: u64, } diff --git a/crates/serde/src/num.rs b/crates/serde/src/num.rs index 6d9f6d2d705..f2e9bf5bdf5 100644 --- a/crates/serde/src/num.rs +++ b/crates/serde/src/num.rs @@ -2,83 +2,9 @@ #[cfg(not(feature = "std"))] use alloc::string::ToString; -use core::str::FromStr; - -use alloy_primitives::{U256, U64}; -use serde::{de, Deserialize, Deserializer, Serialize}; - -/// A `u64` wrapper type that deserializes from hex or a u64 and serializes as hex. -/// -/// -/// ```rust -/// use alloy_serde::num::U64HexOrNumber; -/// let number_json = "100"; -/// let hex_json = "\"0x64\""; -/// -/// let number: U64HexOrNumber = serde_json::from_str(number_json).unwrap(); -/// let hex: U64HexOrNumber = serde_json::from_str(hex_json).unwrap(); -/// assert_eq!(number, hex); -/// assert_eq!(hex.to(), 100); -/// ``` -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)] -pub struct U64HexOrNumber(U64); - -impl U64HexOrNumber { - /// Returns the wrapped u64 - pub fn to(self) -> u64 { - self.0.to() - } - - /// Checks if the wrapped value is zero. - pub fn is_zero(&self) -> bool { - self.0.is_zero() - } -} - -impl From for U64HexOrNumber { - fn from(value: u64) -> Self { - Self(U64::from(value)) - } -} - -impl From for U64HexOrNumber { - fn from(value: U64) -> Self { - Self(value) - } -} -impl From for u64 { - fn from(value: U64HexOrNumber) -> Self { - value.to() - } -} - -impl From for U64 { - fn from(value: U64HexOrNumber) -> Self { - value.0 - } -} - -impl<'de> Deserialize<'de> for U64HexOrNumber { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(untagged)] - enum NumberOrHexU64 { - Hex(U64), - Int(u64), - } - match NumberOrHexU64::deserialize(deserializer)? { - NumberOrHexU64::Int(val) => Ok(val.into()), - NumberOrHexU64::Hex(val) => Ok(val.into()), - } - } -} - -/// serde functions for handling `u8` as [U8](alloy_primitives::U8) -pub mod u8_hex { +/// serde functions for handling `u8` via [U8](alloy_primitives::U8) +pub mod u8_via_ruint { use alloy_primitives::U8; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -96,8 +22,8 @@ pub mod u8_hex { } } -/// serde functions for handling `Option` as [U8](alloy_primitives::U8) -pub mod u8_hex_opt { +/// serde functions for handling `Option` via [U8](alloy_primitives::U8) +pub mod u8_opt_via_ruint { use alloy_primitives::U8; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -118,8 +44,8 @@ pub mod u8_hex_opt { } } -/// serde functions for handling `u64` as [U64] -pub mod u64_hex { +/// serde functions for handling `u64` via [U64](alloy_primitives::U64) +pub mod u64_via_ruint { use alloy_primitives::U64; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -137,8 +63,8 @@ pub mod u64_hex { } } -/// serde functions for handling `Option` as [U64] -pub mod u64_hex_opt { +/// serde functions for handling `Option` via [U64](alloy_primitives::U64) +pub mod u64_opt_via_ruint { use alloy_primitives::U64; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -160,97 +86,8 @@ pub mod u64_hex_opt { } } -/// serde functions for handling primitive `u64` as [U64] -pub mod u64_hex_or_decimal { - use crate::num::U64HexOrNumber; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u64` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - U64HexOrNumber::deserialize(deserializer).map(Into::into) - } - - /// Serializes u64 as hex string - pub fn serialize(value: &u64, s: S) -> Result { - U64HexOrNumber::from(*value).serialize(s) - } -} - -/// serde functions for handling primitive optional `u64` as [U64] -pub mod u64_hex_or_decimal_opt { - use crate::num::U64HexOrNumber; - use serde::{Deserialize, Deserializer, Serialize, Serializer}; - - /// Deserializes an `u64` accepting a hex quantity string with optional 0x prefix or - /// a number - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - match Option::::deserialize(deserializer)? { - Some(val) => Ok(Some(val.into())), - None => Ok(None), - } - } - - /// Serializes u64 as hex string - pub fn serialize(value: &Option, s: S) -> Result { - match value { - Some(val) => U64HexOrNumber::from(*val).serialize(s), - None => s.serialize_none(), - } - } -} - -/// Deserializes the input into an `Option`, using [`from_int_or_hex`] to deserialize the -/// inner value. -pub fn from_int_or_hex_opt<'de, D>(deserializer: D) -> Result, D::Error> -where - D: Deserializer<'de>, -{ - match Option::::deserialize(deserializer)? { - Some(val) => val.try_into_u256().map(Some), - None => Ok(None), - } -} - -/// An enum that represents either a [serde_json::Number] integer, or a hex [U256]. -#[derive(Debug, Deserialize)] -#[serde(untagged)] -pub enum NumberOrHexU256 { - /// An integer - Int(serde_json::Number), - /// A hex U256 - Hex(U256), -} - -impl NumberOrHexU256 { - /// Tries to convert this into a [U256]]. - pub fn try_into_u256(self) -> Result { - match self { - NumberOrHexU256::Int(num) => { - U256::from_str(num.to_string().as_str()).map_err(E::custom) - } - NumberOrHexU256::Hex(val) => Ok(val), - } - } -} - -/// Deserializes the input into a U256, accepting both 0x-prefixed hex and decimal strings with -/// arbitrary precision, defined by serde_json's [`Number`](serde_json::Number). -pub fn from_int_or_hex<'de, D>(deserializer: D) -> Result -where - D: Deserializer<'de>, -{ - NumberOrHexU256::deserialize(deserializer)?.try_into_u256() -} - -/// serde functions for handling primitive `u128` as [U128](alloy_primitives::U128) -pub mod u128_hex_or_decimal { +/// serde functions for handling primitive `u128` via [U128](alloy_primitives::U128) +pub mod u128_via_ruint { use alloy_primitives::U128; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -269,8 +106,8 @@ pub mod u128_hex_or_decimal { } } -/// serde functions for handling primitive optional `u128` as [U128](alloy_primitives::U128) -pub mod u128_hex_or_decimal_opt { +/// serde functions for handling primitive optional `u128` via [U128](alloy_primitives::U128) +pub mod u128_opt_via_ruint { use alloy_primitives::U128; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -295,8 +132,8 @@ pub mod u128_hex_or_decimal_opt { } } -/// serde functions for handling `Vec` as [U128](alloy_primitives::U128) -pub mod u128_hex_or_decimal_vec { +/// serde functions for handling `Vec` via [U128](alloy_primitives::U128) +pub mod u128_vec_via_ruint { #[cfg(not(feature = "std"))] use alloc::vec::Vec; use alloy_primitives::U128; @@ -319,8 +156,8 @@ pub mod u128_hex_or_decimal_vec { } } -/// serde functions for handling `Vec>` as [U128](alloy_primitives::U128) -pub mod u128_hex_or_decimal_vec_vec_opt { +/// serde functions for handling `Vec>` via [U128](alloy_primitives::U128) +pub mod u128_vec_vec_opt_via_ruint { use alloy_primitives::U128; use serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -371,7 +208,7 @@ mod tests { fn test_hex_u64() { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] struct Value { - #[serde(with = "u64_hex")] + #[serde(with = "u64_via_ruint")] inner: u64, } @@ -384,10 +221,10 @@ mod tests { } #[test] - fn test_u128_hex_or_decimal() { + fn test_u128_via_ruint() { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] struct Value { - #[serde(with = "u128_hex_or_decimal")] + #[serde(with = "u128_via_ruint")] inner: u128, } @@ -405,10 +242,10 @@ mod tests { } #[test] - fn test_u128_hex_or_decimal_opt() { + fn test_u128_opt_via_ruint() { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] struct Value { - #[serde(with = "u128_hex_or_decimal_opt")] + #[serde(with = "u128_opt_via_ruint")] inner: Option, } @@ -433,10 +270,10 @@ mod tests { } #[test] - fn test_u128_hex_or_decimal_vec() { + fn test_u128_vec_via_ruint() { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] struct Value { - #[serde(with = "u128_hex_or_decimal_vec")] + #[serde(with = "u128_vec_via_ruint")] inner: Vec, } @@ -449,10 +286,10 @@ mod tests { } #[test] - fn test_u128_hex_or_decimal_vec_vec_opt() { + fn test_u128_vec_vec_opt_via_ruint() { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize)] struct Value { - #[serde(with = "u128_hex_or_decimal_vec_vec_opt")] + #[serde(with = "u128_vec_vec_opt_via_ruint")] inner: Option>>, }