Skip to content

Commit

Permalink
feat: add From<Transaction> and <FeeBumpTransaction>
Browse files Browse the repository at this point in the history
  • Loading branch information
willemneal committed May 22, 2024
1 parent 89a8bcd commit 3bf31d7
Show file tree
Hide file tree
Showing 5 changed files with 85 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()
}
}
15 changes: 15 additions & 0 deletions tests/tx_small.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ fn test_build_small_tx_with_alloc() -> Result<(), Error> {
},
signatures: [].try_into()?,
});
}

#[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(())
}

Expand Down

0 comments on commit 3bf31d7

Please sign in to comment.