Skip to content

Commit

Permalink
feat: add From<Transaction> and <FeeBumpTransaction> (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal authored May 26, 2024
1 parent 89a8bcd commit d368909
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 0 deletions.
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;

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

0 comments on commit d368909

Please sign in to comment.