Skip to content

Commit

Permalink
feat: cache wallet info (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
dangeross authored Dec 12, 2024
1 parent 35b0a8b commit 00caa6e
Show file tree
Hide file tree
Showing 24 changed files with 431 additions and 108 deletions.
6 changes: 6 additions & 0 deletions cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub(crate) enum Command {
#[clap(name = "filter", short = 'r', long = "filter")]
filters: Option<Vec<PaymentType>>,

/// The optional payment state. Either "pending", "complete", "failed", "pendingrefund" or "refundable"
#[clap(name = "state", short = 's', long = "state")]
states: Option<Vec<PaymentState>>,

/// The optional from unix timestamp
#[clap(name = "from_timestamp", short = 'f', long = "from")]
from_timestamp: Option<i64>,
Expand Down Expand Up @@ -477,6 +481,7 @@ pub(crate) async fn handle_command(
}
Command::ListPayments {
filters,
states,
from_timestamp,
to_timestamp,
limit,
Expand All @@ -493,6 +498,7 @@ pub(crate) async fn handle_command(
let payments = sdk
.list_payments(&ListPaymentsRequest {
filters,
states,
from_timestamp,
to_timestamp,
limit,
Expand Down
17 changes: 8 additions & 9 deletions lib/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ typedef struct wire_cst_list_payment_type {
int32_t len;
} wire_cst_list_payment_type;

typedef struct wire_cst_list_payment_state {
int32_t *ptr;
int32_t len;
} wire_cst_list_payment_state;

typedef struct wire_cst_ListPaymentDetails_Liquid {
struct wire_cst_list_prim_u_8_strict *destination;
} wire_cst_ListPaymentDetails_Liquid;
Expand All @@ -101,6 +106,7 @@ typedef struct wire_cst_list_payment_details {

typedef struct wire_cst_list_payments_request {
struct wire_cst_list_payment_type *filters;
struct wire_cst_list_payment_state *states;
int64_t *from_timestamp;
int64_t *to_timestamp;
uint32_t *offset;
Expand Down Expand Up @@ -1288,6 +1294,8 @@ struct wire_cst_list_localized_name *frbgen_breez_liquid_cst_new_list_localized_

struct wire_cst_list_payment *frbgen_breez_liquid_cst_new_list_payment(int32_t len);

struct wire_cst_list_payment_state *frbgen_breez_liquid_cst_new_list_payment_state(int32_t len);

struct wire_cst_list_payment_type *frbgen_breez_liquid_cst_new_list_payment_type(int32_t len);

struct wire_cst_list_prim_u_8_strict *frbgen_breez_liquid_cst_new_list_prim_u_8_strict(int32_t len);
Expand Down Expand Up @@ -1357,6 +1365,7 @@ static int64_t dummy_method_to_enforce_bundling(void) {
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_locale_overrides);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_localized_name);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_payment);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_payment_state);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_payment_type);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_prim_u_8_strict);
dummy_var ^= ((int64_t) (void*) frbgen_breez_liquid_cst_new_list_rate);
Expand Down
1 change: 1 addition & 0 deletions lib/bindings/src/breez_sdk_liquid.udl
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,7 @@ dictionary RestoreRequest {

dictionary ListPaymentsRequest {
sequence<PaymentType>? filters = null;
sequence<PaymentState>? states = null;
i64? from_timestamp = null;
i64? to_timestamp = null;
u32? offset = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/core/src/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct BindingLiquidSdk {

impl BindingLiquidSdk {
pub async fn get_info(&self) -> Result<GetInfoResponse, SdkError> {
self.sdk.get_info().await.map_err(Into::into)
self.sdk.get_info().await
}

#[frb(sync)]
Expand Down
3 changes: 2 additions & 1 deletion lib/core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;
use log::info;
use log::{debug, info};
use tokio::sync::{broadcast, RwLock};

use crate::model::{EventListener, SdkEvent};
Expand Down Expand Up @@ -42,6 +42,7 @@ impl EventManager {
match self.is_paused.load(Ordering::SeqCst) {
true => info!("Event notifications are paused, not emitting event {e:?}"),
false => {
debug!("Emitting event: {e:?}");
let _ = self.notifier.send(e.clone());

for listener in (*self.listeners.read().await).values() {
Expand Down
77 changes: 77 additions & 0 deletions lib/core/src/frb_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2686,6 +2686,18 @@ impl SseDecode for crate::model::ListPaymentDetails {
}
}

impl SseDecode for Vec<crate::model::PaymentState> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut len_ = <i32>::sse_decode(deserializer);
let mut ans_ = vec![];
for idx_ in 0..len_ {
ans_.push(<crate::model::PaymentState>::sse_decode(deserializer));
}
return ans_;
}
}

impl SseDecode for Vec<crate::model::PaymentType> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
Expand All @@ -2702,13 +2714,15 @@ impl SseDecode for crate::model::ListPaymentsRequest {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
let mut var_filters = <Option<Vec<crate::model::PaymentType>>>::sse_decode(deserializer);
let mut var_states = <Option<Vec<crate::model::PaymentState>>>::sse_decode(deserializer);
let mut var_fromTimestamp = <Option<i64>>::sse_decode(deserializer);
let mut var_toTimestamp = <Option<i64>>::sse_decode(deserializer);
let mut var_offset = <Option<u32>>::sse_decode(deserializer);
let mut var_limit = <Option<u32>>::sse_decode(deserializer);
let mut var_details = <Option<crate::model::ListPaymentDetails>>::sse_decode(deserializer);
return crate::model::ListPaymentsRequest {
filters: var_filters,
states: var_states,
from_timestamp: var_fromTimestamp,
to_timestamp: var_toTimestamp,
offset: var_offset,
Expand Down Expand Up @@ -3405,6 +3419,17 @@ impl SseDecode for Option<u64> {
}
}

impl SseDecode for Option<Vec<crate::model::PaymentState>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
if (<bool>::sse_decode(deserializer)) {
return Some(<Vec<crate::model::PaymentState>>::sse_decode(deserializer));
} else {
return None;
}
}
}

impl SseDecode for Option<Vec<crate::model::PaymentType>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_decode(deserializer: &mut flutter_rust_bridge::for_generated::SseDeserializer) -> Self {
Expand Down Expand Up @@ -4814,6 +4839,7 @@ impl flutter_rust_bridge::IntoDart for crate::model::ListPaymentsRequest {
fn into_dart(self) -> flutter_rust_bridge::for_generated::DartAbi {
[
self.filters.into_into_dart().into_dart(),
self.states.into_into_dart().into_dart(),
self.from_timestamp.into_into_dart().into_dart(),
self.to_timestamp.into_into_dart().into_dart(),
self.offset.into_into_dart().into_dart(),
Expand Down Expand Up @@ -6838,6 +6864,16 @@ impl SseEncode for crate::model::ListPaymentDetails {
}
}

impl SseEncode for Vec<crate::model::PaymentState> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<i32>::sse_encode(self.len() as _, serializer);
for item in self {
<crate::model::PaymentState>::sse_encode(item, serializer);
}
}
}

impl SseEncode for Vec<crate::model::PaymentType> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
Expand All @@ -6852,6 +6888,7 @@ impl SseEncode for crate::model::ListPaymentsRequest {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<Option<Vec<crate::model::PaymentType>>>::sse_encode(self.filters, serializer);
<Option<Vec<crate::model::PaymentState>>>::sse_encode(self.states, serializer);
<Option<i64>>::sse_encode(self.from_timestamp, serializer);
<Option<i64>>::sse_encode(self.to_timestamp, serializer);
<Option<u32>>::sse_encode(self.offset, serializer);
Expand Down Expand Up @@ -7406,6 +7443,16 @@ impl SseEncode for Option<u64> {
}
}

impl SseEncode for Option<Vec<crate::model::PaymentState>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
<bool>::sse_encode(self.is_some(), serializer);
if let Some(value) = self {
<Vec<crate::model::PaymentState>>::sse_encode(value, serializer);
}
}
}

impl SseEncode for Option<Vec<crate::model::PaymentType>> {
// Codec=Sse (Serialization based), see doc to use other codecs
fn sse_encode(self, serializer: &mut flutter_rust_bridge::for_generated::SseSerializer) {
Expand Down Expand Up @@ -8894,6 +8941,16 @@ mod io {
}
}
}
impl CstDecode<Vec<crate::model::PaymentState>> for *mut wire_cst_list_payment_state {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> Vec<crate::model::PaymentState> {
let vec = unsafe {
let wrap = flutter_rust_bridge::for_generated::box_from_leak_ptr(self);
flutter_rust_bridge::for_generated::vec_from_leak_ptr(wrap.ptr, wrap.len)
};
vec.into_iter().map(CstDecode::cst_decode).collect()
}
}
impl CstDecode<Vec<crate::model::PaymentType>> for *mut wire_cst_list_payment_type {
// Codec=Cst (C-struct based), see doc to use other codecs
fn cst_decode(self) -> Vec<crate::model::PaymentType> {
Expand All @@ -8909,6 +8966,7 @@ mod io {
fn cst_decode(self) -> crate::model::ListPaymentsRequest {
crate::model::ListPaymentsRequest {
filters: self.filters.cst_decode(),
states: self.states.cst_decode(),
from_timestamp: self.from_timestamp.cst_decode(),
to_timestamp: self.to_timestamp.cst_decode(),
offset: self.offset.cst_decode(),
Expand Down Expand Up @@ -10239,6 +10297,7 @@ mod io {
fn new_with_null_ptr() -> Self {
Self {
filters: core::ptr::null_mut(),
states: core::ptr::null_mut(),
from_timestamp: core::ptr::null_mut(),
to_timestamp: core::ptr::null_mut(),
offset: core::ptr::null_mut(),
Expand Down Expand Up @@ -11905,6 +11964,17 @@ mod io {
flutter_rust_bridge::for_generated::new_leak_box_ptr(wrap)
}

#[no_mangle]
pub extern "C" fn frbgen_breez_liquid_cst_new_list_payment_state(
len: i32,
) -> *mut wire_cst_list_payment_state {
let wrap = wire_cst_list_payment_state {
ptr: flutter_rust_bridge::for_generated::new_leak_vec_ptr(Default::default(), len),
len,
};
flutter_rust_bridge::for_generated::new_leak_box_ptr(wrap)
}

#[no_mangle]
pub extern "C" fn frbgen_breez_liquid_cst_new_list_payment_type(
len: i32,
Expand Down Expand Up @@ -12297,6 +12367,12 @@ mod io {
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_list_payment_state {
ptr: *mut i32,
len: i32,
}
#[repr(C)]
#[derive(Clone, Copy)]
pub struct wire_cst_list_payment_type {
ptr: *mut i32,
len: i32,
Expand All @@ -12305,6 +12381,7 @@ mod io {
#[derive(Clone, Copy)]
pub struct wire_cst_list_payments_request {
filters: *mut wire_cst_list_payment_type,
states: *mut wire_cst_list_payment_state,
from_timestamp: *mut i64,
to_timestamp: *mut i64,
offset: *mut u32,
Expand Down
6 changes: 4 additions & 2 deletions lib/core/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ pub struct RefundResponse {
}

/// Returned when calling [crate::sdk::LiquidSdk::get_info].
#[derive(Debug, Serialize)]
#[derive(Debug, Serialize, Deserialize)]
pub struct GetInfoResponse {
/// Usable balance. This is the confirmed onchain balance minus `pending_send_sat`.
pub balance_sat: u64,
Expand Down Expand Up @@ -564,6 +564,7 @@ pub struct RestoreRequest {
#[derive(Default)]
pub struct ListPaymentsRequest {
pub filters: Option<Vec<PaymentType>>,
pub states: Option<Vec<PaymentState>>,
/// Epoch time, in seconds
pub from_timestamp: Option<i64>,
/// Epoch time, in seconds
Expand Down Expand Up @@ -1022,7 +1023,8 @@ pub struct RefundableSwap {
}

/// The payment state of an individual payment.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Hash)]
#[derive(Clone, Copy, Debug, Default, EnumString, Eq, PartialEq, Serialize, Hash)]
#[strum(serialize_all = "lowercase")]
pub enum PaymentState {
#[default]
Created = 0,
Expand Down
15 changes: 15 additions & 0 deletions lib/core/src/persist/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ use anyhow::Result;
use rusqlite::{Transaction, TransactionBehavior};
use std::str::FromStr;

use crate::model::GetInfoResponse;
use crate::sync::model::{data::LAST_DERIVATION_INDEX_DATA_ID, RecordType};

use super::Persister;

const KEY_WALLET_INFO: &str = "wallet_info";
const KEY_SWAPPER_PROXY_URL: &str = "swapper_proxy_url";
const KEY_IS_FIRST_SYNC_COMPLETE: &str = "is_first_sync_complete";
const KEY_WEBHOOK_URL: &str = "webhook_url";
Expand Down Expand Up @@ -62,6 +64,19 @@ impl Persister {
res
}

pub fn set_wallet_info(&self, info: &GetInfoResponse) -> Result<()> {
let serialized_info = serde_json::to_string(info)?;
self.update_cached_item(KEY_WALLET_INFO, serialized_info)
}

pub fn get_wallet_info(&self) -> Result<Option<GetInfoResponse>> {
let info_str = self.get_cached_item(KEY_WALLET_INFO)?;
Ok(match info_str {
Some(str) => serde_json::from_str(str.as_str())?,
None => None,
})
}

pub fn set_swapper_proxy_url(&self, swapper_proxy_url: String) -> Result<()> {
self.update_cached_item(KEY_SWAPPER_PROXY_URL, swapper_proxy_url)
}
Expand Down
Loading

0 comments on commit 00caa6e

Please sign in to comment.