Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds feature-gated serde traits to TransactionMetadata #77

Merged
merged 2 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:?}")))
}