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

feat: add From<Transaction> and From<&Transaction> #370

Merged
merged 1 commit into from
May 26, 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 src/curr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod str;

mod scval_conversions;
pub use scval_conversions::*;
mod transaction_conversions;
willemneal marked this conversation as resolved.
Show resolved Hide resolved

mod scval_validations;
pub use scval_validations::*;
Expand Down
34 changes: 34 additions & 0 deletions src/curr/transaction_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::{
FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope,
TransactionV1Envelope, VecM,
};

impl From<Transaction> for TransactionEnvelope {
fn from(tx: Transaction) -> Self {
TransactionEnvelope::Tx(TransactionV1Envelope {
tx,
signatures: VecM::default(),
})
}
}

impl From<FeeBumpTransaction> 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()
}
}
1 change: 1 addition & 0 deletions src/next/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod str;

mod scval_conversions;
pub use scval_conversions::*;
mod transaction_conversions;

mod scval_validations;
pub use scval_validations::*;
Expand Down
34 changes: 34 additions & 0 deletions src/next/transaction_conversions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use super::{
FeeBumpTransaction, FeeBumpTransactionEnvelope, Transaction, TransactionEnvelope,
TransactionV1Envelope, VecM,
};

impl From<Transaction> for TransactionEnvelope {
fn from(tx: Transaction) -> Self {
TransactionEnvelope::Tx(TransactionV1Envelope {
tx,
signatures: VecM::default(),
})
}
}

impl From<FeeBumpTransaction> 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()
}
}
16 changes: 16 additions & 0 deletions tests/tx_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
Loading