From d3689094d1b7d4db50526d749c059c4c1d779f5c Mon Sep 17 00:00:00 2001 From: Willem Wyndham Date: Sun, 26 May 2024 16:31:39 -0400 Subject: [PATCH] feat: add From and (#370) --- src/curr/mod.rs | 1 + src/curr/transaction_conversions.rs | 34 +++++++++++++++++++++++++++++ src/next/mod.rs | 1 + src/next/transaction_conversions.rs | 34 +++++++++++++++++++++++++++++ tests/tx_small.rs | 16 ++++++++++++++ 5 files changed, 86 insertions(+) create mode 100644 src/curr/transaction_conversions.rs create mode 100644 src/next/transaction_conversions.rs diff --git a/src/curr/mod.rs b/src/curr/mod.rs index ec2f1e37..cf84e11d 100644 --- a/src/curr/mod.rs +++ b/src/curr/mod.rs @@ -6,6 +6,7 @@ mod str; mod scval_conversions; pub use scval_conversions::*; +mod transaction_conversions; mod scval_validations; pub use scval_validations::*; diff --git a/src/curr/transaction_conversions.rs b/src/curr/transaction_conversions.rs new file mode 100644 index 00000000..a821a51f --- /dev/null +++ b/src/curr/transaction_conversions.rs @@ -0,0 +1,34 @@ +use super::{ + FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope, + TransactionV1Envelope, VecM, +}; + +impl From for TransactionEnvelope { + fn from(tx: Transaction) -> Self { + TransactionEnvelope::Tx(TransactionV1Envelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From for TransactionEnvelope { + fn from(tx: FeeBumpTransaction) -> Self { + TransactionEnvelope::TxFeeBump(FeeBumpTransactionEnvelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From<&FeeBumpTransaction> for TransactionEnvelope { + fn from(tx: &FeeBumpTransaction) -> Self { + tx.clone().into() + } +} + +impl From<&Transaction> for TransactionEnvelope { + fn from(tx: &Transaction) -> Self { + tx.clone().into() + } +} diff --git a/src/next/mod.rs b/src/next/mod.rs index ec2f1e37..cf84e11d 100644 --- a/src/next/mod.rs +++ b/src/next/mod.rs @@ -6,6 +6,7 @@ mod str; mod scval_conversions; pub use scval_conversions::*; +mod transaction_conversions; mod scval_validations; pub use scval_validations::*; diff --git a/src/next/transaction_conversions.rs b/src/next/transaction_conversions.rs new file mode 100644 index 00000000..a821a51f --- /dev/null +++ b/src/next/transaction_conversions.rs @@ -0,0 +1,34 @@ +use super::{ + FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope, + TransactionV1Envelope, VecM, +}; + +impl From for TransactionEnvelope { + fn from(tx: Transaction) -> Self { + TransactionEnvelope::Tx(TransactionV1Envelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From for TransactionEnvelope { + fn from(tx: FeeBumpTransaction) -> Self { + TransactionEnvelope::TxFeeBump(FeeBumpTransactionEnvelope { + tx, + signatures: VecM::default(), + }) + } +} + +impl From<&FeeBumpTransaction> for TransactionEnvelope { + fn from(tx: &FeeBumpTransaction) -> Self { + tx.clone().into() + } +} + +impl From<&Transaction> for TransactionEnvelope { + fn from(tx: &Transaction) -> Self { + tx.clone().into() + } +} diff --git a/tests/tx_small.rs b/tests/tx_small.rs index 9ef91bc0..b564d75a 100644 --- a/tests/tx_small.rs +++ b/tests/tx_small.rs @@ -79,6 +79,22 @@ fn test_build_small_tx_with_alloc() -> Result<(), Error> { Ok(()) } +#[cfg(feature = "alloc")] +#[test] +fn convert_reference_of_tx_to_unsigned_transaction_envelope() -> Result<(), Error> { + let tx = &Transaction { + source_account: MuxedAccount::Ed25519(Uint256([0; 32])), + fee: 0, + seq_num: SequenceNumber(1), + cond: Preconditions::None, + memo: Memo::Text("Stellar".as_bytes().try_into()?), + operations: [].to_vec().try_into()?, + ext: TransactionExt::V0, + }; + let _: TransactionEnvelope = tx.into(); + Ok(()) +} + #[cfg(not(feature = "alloc"))] #[test] fn test_build_small_tx_with_alloc() -> Result<(), Error> {