Skip to content

Commit

Permalink
adds feature-gated serde traits to TransactionMetadata (#77)
Browse files Browse the repository at this point in the history
* adds feature-gated serde traits to TransactionMetadata

* update changelog and serde version

---------

Co-authored-by: eric <>
  • Loading branch information
ebrightfield authored Jul 2, 2024
1 parent 3fa14bf commit 446167f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- Add `error.rs` and new `LiteSVMError` type ([#62](https://github.com/LiteSVM/litesvm/pull/62)).
- Add more logging for users to make debugging errors easier ([#62](https://github.com/LiteSVM/litesvm/pull/62)).
- Add `inner_instructions` to `TransactionMetadata` ([#75](https://github.com/LiteSVM/litesvm/pull/75)).
- Add feature-flagged `serde` traits to `TransactionMetadata` ([#77](https://github.com/LiteSVM/litesvm/pull/77)).

### Changed

Expand Down
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ log = "0.4.21"
solana-vote-program = "~1.18"
solana-stake-program = "~1.18"
solana-config-program = "~1.18"
serde = { version = "1.0", optional = true }

[dev-dependencies]
spl-token = "3.5.0"
Expand All @@ -33,7 +34,7 @@ criterion = "0.5"
tokio = "1.35"
test-log = "0.2.15"
solana-config-program = "~1.18"
serde = "1.0.197"
serde = "1.0"

[features]
internal-test = []
Expand Down
3 changes: 3 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use solana_sdk::{
};

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct TransactionMetadata {
#[cfg_attr(feature = "serde", serde(with = "crate::utils::serde_with_str"))]
pub signature: Signature,
pub logs: Vec<String>,
pub inner_instructions: InnerInstructionsList,
Expand All @@ -17,6 +19,7 @@ pub struct TransactionMetadata {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct FailedTransactionMetadata {
pub err: TransactionError,
pub meta: TransactionMetadata,
Expand Down
2 changes: 2 additions & 0 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ use solana_sdk::{

pub mod inner_instructions;
pub mod rent;
#[cfg(feature = "serde")]
pub mod serde_with_str;

/// Create a blockhash from the given bytes
pub fn create_blockhash(bytes: &[u8]) -> Hash {
Expand Down
24 changes: 24 additions & 0 deletions src/utils/serde_with_str.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use {
serde::{de, Deserializer, Serializer},
serde::{Deserialize, Serialize},
std::str::FromStr,
};

pub fn serialize<T, S>(t: &T, serializer: S) -> Result<S::Ok, S::Error>
where
T: ToString,
S: Serializer,
{
t.to_string().serialize(serializer)
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
where
T: FromStr,
D: Deserializer<'de>,
<T as FromStr>::Err: std::fmt::Debug,
{
let s: String = String::deserialize(deserializer)?;
s.parse()
.map_err(|e| de::Error::custom(format!("Parse error: {e:?}")))
}

0 comments on commit 446167f

Please sign in to comment.