Skip to content

Commit 18309a5

Browse files
committed
Add list_transactions to ffi
also added a couple easy functions
1 parent d81df18 commit 18309a5

3 files changed

Lines changed: 185 additions & 1 deletion

File tree

orange-sdk/src/ffi/orange/mod.rs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,163 @@
1+
use crate::bitcoin::hashes::Hash;
2+
use crate::ffi::bitcoin_payment_instructions::Amount;
3+
use crate::{PaymentId as OrangePaymentId, Transaction as OrangeTransaction};
4+
use crate::{impl_from_core_type, impl_into_core_type};
5+
16
pub mod config;
27
pub mod error;
38
pub mod wallet;
9+
10+
/// A transaction is a record of a payment made or received. It contains information about the
11+
/// transaction, such as the amount, fee, and status. It is used to track the state of a payment
12+
/// and to provide information about the payment to the user.
13+
#[derive(Debug, Clone, uniffi::Object)]
14+
pub struct Transaction(pub crate::store::Transaction);
15+
16+
impl_from_core_type!(OrangeTransaction, Transaction);
17+
impl_into_core_type!(Transaction, OrangeTransaction);
18+
19+
#[uniffi::export]
20+
impl Transaction {
21+
pub fn id(&self) -> PaymentId {
22+
self.0.id.into()
23+
}
24+
25+
pub fn status(&self) -> TxStatus {
26+
self.0.status.into()
27+
}
28+
29+
pub fn outbound(&self) -> bool {
30+
self.0.outbound
31+
}
32+
33+
/// The amount of the payment
34+
///
35+
/// None if the payment is not yet completed
36+
pub fn amount(&self) -> Option<Amount> {
37+
self.0.amount.map(Into::into)
38+
}
39+
40+
/// The fee paid for the payment
41+
///
42+
/// None if the payment is not yet completed
43+
pub fn fee(&self) -> Option<Amount> {
44+
self.0.fee.map(Into::into)
45+
}
46+
47+
/// The timestamp of the transaction
48+
pub fn secs_since_epoch(&self) -> u64 {
49+
self.0.time_since_epoch.as_secs()
50+
}
51+
52+
/// The type of payment being made
53+
pub fn payment_type(&self) -> PaymentType {
54+
self.0.payment_type.clone().into()
55+
}
56+
}
57+
58+
/// A PaymentId is a unique identifier for a payment. It can be either a Lightning payment or a
59+
/// Trusted payment. It is used to track the state of a payment and to provide information about
60+
/// the payment to the user.
61+
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, uniffi::Object)]
62+
pub enum PaymentId {
63+
Lightning([u8; 32]),
64+
Trusted([u8; 32]),
65+
}
66+
67+
impl From<OrangePaymentId> for PaymentId {
68+
fn from(id: OrangePaymentId) -> Self {
69+
match id {
70+
OrangePaymentId::Lightning(hash) => Self::Lightning(hash),
71+
OrangePaymentId::Trusted(hash) => Self::Trusted(hash),
72+
}
73+
}
74+
}
75+
76+
impl From<OrangePaymentId> for PaymentId {
77+
fn from(id: OrangePaymentId) -> Self {
78+
match id {
79+
OrangePaymentId::Lightning(hash) => Self::Lightning(hash),
80+
OrangePaymentId::Trusted(hash) => Self::Trusted(hash),
81+
}
82+
}
83+
}
84+
85+
/// The status of a transaction. This is used to track the state of a transaction
86+
#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)]
87+
pub enum TxStatus {
88+
/// A pending transaction has not yet been paid.
89+
Pending,
90+
/// A completed transaction has been paid.
91+
Completed,
92+
/// A transaction that has failed.
93+
Failed,
94+
}
95+
96+
impl From<crate::store::TxStatus> for TxStatus {
97+
fn from(status: crate::store::TxStatus) -> Self {
98+
match status {
99+
crate::store::TxStatus::Pending => TxStatus::Pending,
100+
crate::store::TxStatus::Completed => TxStatus::Completed,
101+
crate::store::TxStatus::Failed => TxStatus::Failed,
102+
}
103+
}
104+
}
105+
106+
/// The type of payment being made. This indicates whether the payment is incoming or outgoing,
107+
/// and what method is being used (Lightning, on-chain, etc.)
108+
#[derive(Debug, Clone, uniffi::Enum)]
109+
pub enum PaymentType {
110+
/// An outgoing Lightning payment paying a BOLT 12 offer.
111+
OutgoingLightningBolt12 {
112+
/// The lightning "payment preimage" which represents proof that the payment completed.
113+
/// Will be set for any completed payments.
114+
payment_preimage: Option<[u8; 32]>,
115+
},
116+
/// An outgoing Lightning payment paying a BOLT 11 invoice.
117+
OutgoingLightningBolt11 {
118+
/// The lightning "payment preimage" which represents proof that the payment completed.
119+
/// Will be set for any completed payments.
120+
payment_preimage: Option<[u8; 32]>,
121+
},
122+
/// An outgoing on-chain Bitcoin payment.
123+
OutgoingOnChain {
124+
/// The optional transaction ID of the on-chain payment.
125+
/// Will be set for any completed payments.
126+
txid: Option<[u8; 32]>,
127+
},
128+
/// An incoming on-chain Bitcoin payment.
129+
IncomingOnChain {
130+
/// The optional transaction ID of the on-chain payment.
131+
/// Will be set for any completed payments.
132+
txid: Option<[u8; 32]>,
133+
},
134+
/// An incoming Lightning payment.
135+
IncomingLightning {},
136+
/// A payment that is internal to the trusted service.
137+
TrustedInternal {},
138+
}
139+
140+
impl From<crate::store::PaymentType> for PaymentType {
141+
fn from(payment_type: crate::store::PaymentType) -> Self {
142+
match payment_type {
143+
crate::store::PaymentType::OutgoingLightningBolt12 { payment_preimage } => {
144+
PaymentType::OutgoingLightningBolt12 {
145+
payment_preimage: payment_preimage.map(|p| p.0),
146+
}
147+
},
148+
crate::store::PaymentType::OutgoingLightningBolt11 { payment_preimage } => {
149+
PaymentType::OutgoingLightningBolt11 {
150+
payment_preimage: payment_preimage.map(|p| p.0),
151+
}
152+
},
153+
crate::store::PaymentType::OutgoingOnChain { txid } => {
154+
PaymentType::OutgoingOnChain { txid: txid.map(|t| t.to_byte_array()) }
155+
},
156+
crate::store::PaymentType::IncomingOnChain { txid } => {
157+
PaymentType::IncomingOnChain { txid: txid.map(|t| t.to_byte_array()) }
158+
},
159+
crate::store::PaymentType::IncomingLightning {} => PaymentType::IncomingLightning {},
160+
crate::store::PaymentType::TrustedInternal {} => PaymentType::TrustedInternal {},
161+
}
162+
}
163+
}

orange-sdk/src/ffi/orange/wallet.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::Balances as OrangeBalances;
1+
use crate::{Balances as OrangeBalances, store};
22

33
use crate::Wallet as OrangeWallet;
44
use crate::WalletConfig as OrangeWalletConfig;
@@ -75,4 +75,25 @@ impl Wallet {
7575
let balance = self.inner.get_balance().await?;
7676
Ok(balance.into())
7777
}
78+
79+
pub async fn is_connected_to_lsp(&self) -> bool {
80+
self.inner.is_connected_to_lsp()
81+
}
82+
83+
/// Sets whether the wallet should automatically rebalance from trusted/onchain to lightning.
84+
pub fn set_rebalance_enabled(&self, value: bool) {
85+
self.inner.set_rebalance_enabled(value)
86+
}
87+
88+
/// Whether the wallet should automatically rebalance from trusted/onchain to lightning.
89+
pub fn get_rebalance_enabled(&self) -> bool {
90+
self.inner.get_rebalance_enabled()
91+
}
92+
93+
pub async fn list_transactions(
94+
&self,
95+
) -> Result<Vec<crate::ffi::orange::Transaction>, WalletError> {
96+
let transactions = self.inner.list_transactions().await?;
97+
Ok(transactions.into_iter().map(|tx| tx.into()).collect())
98+
}
7899
}

orange-sdk/src/store.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,9 @@ impl From<Transaction> for StoreTransaction {
125125
}
126126
}
127127

128+
/// A PaymentId is a unique identifier for a payment. It can be either a Lightning payment or a
129+
/// Trusted payment. It is used to track the state of a payment and to provide information about
130+
/// the payment to the user.
128131
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq)]
129132
pub enum PaymentId {
130133
Lightning([u8; 32]),

0 commit comments

Comments
 (0)