From dc107ce89272b649e970204f291904d90258bd81 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 23 May 2022 14:15:29 -0700 Subject: [PATCH] fix: correctly serialize TxPoolInspectSummary (#1305) * fix: correctly serialize TxPoolInspectSummary previously we'd use the default serde derive, now we serialize it like geth does * chore: add back removed Eq derive --- ethers-core/src/types/txpool.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/ethers-core/src/types/txpool.rs b/ethers-core/src/types/txpool.rs index 5f6ce423..93d7b75e 100644 --- a/ethers-core/src/types/txpool.rs +++ b/ethers-core/src/types/txpool.rs @@ -7,7 +7,7 @@ use serde::{ use std::{collections::BTreeMap, fmt, str::FromStr}; /// Transaction summary as found in the Txpool Inspection property. -#[derive(Debug, Clone, PartialEq, Eq, Serialize)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct TxpoolInspectSummary { /// Recipient (None when contract creation) pub to: Option
, @@ -83,6 +83,24 @@ impl<'de> Deserialize<'de> for TxpoolInspectSummary { } } +/// Implement the `Serialize` trait for `TxpoolInspectSummary` struct so that the +/// format matches the one from geth. +impl Serialize for TxpoolInspectSummary { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + let formatted = format!( + "{:?}: {} wei + {} gas × {} wei", + self.to.unwrap_or_default(), + self.value, + self.gas, + self.gas_price + ); + serializer.serialize_str(&formatted) + } +} + /// Transaction Pool Content /// /// The content inspection property can be queried to list the exact details of all @@ -274,6 +292,10 @@ mod tests { }"#; let deserialized: TxpoolInspect = serde_json::from_str(txpool_inspect_json).unwrap(); assert_eq!(deserialized, expected_txpool_inspect()); + + let serialized = serde_json::to_string(&deserialized).unwrap(); + let deserialized2: TxpoolInspect = serde_json::from_str(&serialized).unwrap(); + assert_eq!(deserialized2, deserialized); } #[test]