Skip to content

Commit

Permalink
Add prepare withdraw method
Browse files Browse the repository at this point in the history
  • Loading branch information
ademar111190 committed Jul 31, 2023
1 parent d5f0128 commit a652203
Show file tree
Hide file tree
Showing 13 changed files with 440 additions and 56 deletions.
18 changes: 16 additions & 2 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,18 @@ enum BuyBitcoinProvider {
"Moonpay",
};

interface BlockingBreezServices {
dictionary PrepareWithdrawRequest {
string to_address;
u32 fee_rate_sats_per_vbyte;
};

dictionary PrepareWithdrawResponse {
string raw_tx_hex;
u32 sat_per_vbyte;
u64 fee_sat;
};

interface BlockingBreezServices {

[Throws=SdkError]
void disconnect();
Expand Down Expand Up @@ -502,6 +513,9 @@ interface BlockingBreezServices {

[Throws=SdkError]
string buy_bitcoin(BuyBitcoinProvider provider);

[Throws=SdkError]
PrepareWithdrawResponse prepare_withdraw(PrepareWithdrawRequest prepare_withdraw_request);
};

namespace breez_sdk {
Expand All @@ -522,4 +536,4 @@ namespace breez_sdk {
sequence<u8> mnemonic_to_seed(string phrase);

Config default_config(EnvironmentType env_type, string api_key, NodeConfig node_config);
};
};
18 changes: 15 additions & 3 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ use breez_sdk_core::{
LnUrlErrorData, LnUrlPayRequestData, LnUrlPayResult, LnUrlWithdrawRequestData, LocaleOverrides,
LocalizedName, LogEntry, LspInformation, MessageSuccessActionData, MetadataItem, Network,
NodeConfig, NodeState, Payment, PaymentDetails, PaymentFailedData, PaymentType,
PaymentTypeFilter, Rate, RecommendedFees, ReverseSwapInfo, ReverseSwapPairInfo,
ReverseSwapStatus, RouteHint, RouteHintHop, SuccessActionProcessed, SwapInfo, SwapStatus,
Symbol, UnspentTransactionOutput, UrlSuccessActionData,
PaymentTypeFilter, PrepareWithdrawRequest, PrepareWithdrawResponse, Rate, RecommendedFees,
ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop,
SuccessActionProcessed, SwapInfo, SwapStatus, Symbol, UnspentTransactionOutput,
UrlSuccessActionData,
};

static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
Expand Down Expand Up @@ -302,6 +303,17 @@ impl BlockingBreezServices {
rt().block_on(self.breez_services.buy_bitcoin(provider))
.map_err(|e| e.into())
}

pub fn prepare_withdraw(
&self,
prepare_withdraw_request: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse, SdkError> {
rt().block_on(
self.breez_services
.prepare_withdraw(prepare_withdraw_request),
)
.map_err(|e| e.into())
}
}

pub fn parse_invoice(invoice: String) -> SdkResult<LNInvoice> {
Expand Down
13 changes: 12 additions & 1 deletion libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, PaymentTypeFilter, SwapInfo};
use crate::{
BackupStatus, BuyBitcoinProvider, EnvironmentType, LnUrlCallbackStatus, NodeConfig,
ReverseSwapInfo, ReverseSwapPairInfo,
PrepareWithdrawRequest, PrepareWithdrawResponse, ReverseSwapInfo, ReverseSwapPairInfo,
};

static BREEZ_SERVICES_INSTANCE: OnceCell<Arc<BreezServices>> = OnceCell::new();
Expand Down Expand Up @@ -311,6 +311,17 @@ pub fn sweep(to_address: String, fee_rate_sats_per_vbyte: u64) -> Result<()> {
})
}

/// See [BreezServices::prepare_withdraw]
pub fn prepare_withdraw(
prepare_withdraw_request: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse> {
block_on(async {
get_breez_services()?
.prepare_withdraw(prepare_withdraw_request)
.await
})
}

/* Refundables API's */

/// See [BreezServices::list_refundables]
Expand Down
15 changes: 14 additions & 1 deletion libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,19 @@ impl BreezServices {
Ok(())
}

pub async fn prepare_withdraw(
&self,
prepare_withdraw_request: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse> {
self.start_node().await?;
let response = self
.node_api
.prepare_withdraw(prepare_withdraw_request)
.await?;
self.sync().await?;
Ok(response)
}

/// Fetch live rates of fiat currencies
pub async fn fetch_fiat_rates(&self) -> Result<Vec<Rate>> {
self.fiat_api.fetch_fiat_rates().await
Expand Down Expand Up @@ -931,7 +944,7 @@ impl BreezServices {
}
}

fn closed_channel_to_transaction(channel: crate::models::Channel) -> Result<Payment> {
fn closed_channel_to_transaction(channel: models::Channel) -> Result<Payment> {
let now = SystemTime::now();
Ok(Payment {
id: channel.funding_txid.clone(),
Expand Down
50 changes: 50 additions & 0 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ pub extern "C" fn wire_sweep(
wire_sweep_impl(port_, to_address, fee_rate_sats_per_vbyte)
}

#[no_mangle]
pub extern "C" fn wire_prepare_withdraw(
port_: i64,
prepare_withdraw_request: *mut wire_PrepareWithdrawRequest,
) {
wire_prepare_withdraw_impl(port_, prepare_withdraw_request)
}

#[no_mangle]
pub extern "C" fn wire_list_refundables(port_: i64) {
wire_list_refundables_impl(port_)
Expand Down Expand Up @@ -292,6 +300,11 @@ pub extern "C" fn new_box_autoadd_node_config_0() -> *mut wire_NodeConfig {
support::new_leak_box_ptr(wire_NodeConfig::new_with_null_ptr())
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_prepare_withdraw_request_0() -> *mut wire_PrepareWithdrawRequest {
support::new_leak_box_ptr(wire_PrepareWithdrawRequest::new_with_null_ptr())
}

#[no_mangle]
pub extern "C" fn new_box_autoadd_u64_0(value: u64) -> *mut u64 {
support::new_leak_box_ptr(value)
Expand Down Expand Up @@ -363,6 +376,12 @@ impl Wire2Api<NodeConfig> for *mut wire_NodeConfig {
Wire2Api::<NodeConfig>::wire2api(*wrap).into()
}
}
impl Wire2Api<PrepareWithdrawRequest> for *mut wire_PrepareWithdrawRequest {
fn wire2api(self) -> PrepareWithdrawRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Wire2Api::<PrepareWithdrawRequest>::wire2api(*wrap).into()
}
}
impl Wire2Api<u64> for *mut u64 {
fn wire2api(self) -> u64 {
unsafe { *support::box_from_leak_ptr(self) }
Expand Down Expand Up @@ -452,6 +471,15 @@ impl Wire2Api<NodeConfig> for wire_NodeConfig {
}
}

impl Wire2Api<PrepareWithdrawRequest> for wire_PrepareWithdrawRequest {
fn wire2api(self) -> PrepareWithdrawRequest {
PrepareWithdrawRequest {
to_address: self.to_address.wire2api(),
fee_rate_sats_per_vbyte: self.fee_rate_sats_per_vbyte.wire2api(),
}
}
}

impl Wire2Api<Vec<u8>> for *mut wire_uint_8_list {
fn wire2api(self) -> Vec<u8> {
unsafe {
Expand Down Expand Up @@ -521,6 +549,13 @@ pub struct wire_LnUrlWithdrawRequestData {
max_withdrawable: u64,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_PrepareWithdrawRequest {
to_address: *mut wire_uint_8_list,
fee_rate_sats_per_vbyte: u32,
}

#[repr(C)]
#[derive(Clone)]
pub struct wire_uint_8_list {
Expand Down Expand Up @@ -689,6 +724,21 @@ pub extern "C" fn inflate_NodeConfig_Greenlight() -> *mut NodeConfigKind {
})
}

impl NewWithNullPtr for wire_PrepareWithdrawRequest {
fn new_with_null_ptr() -> Self {
Self {
to_address: core::ptr::null_mut(),
fee_rate_sats_per_vbyte: Default::default(),
}
}
}

impl Default for wire_PrepareWithdrawRequest {
fn default() -> Self {
Self::new_with_null_ptr()
}
}

// Section: sync execution mode utility

#[no_mangle]
Expand Down
31 changes: 31 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ use crate::models::Payment;
use crate::models::PaymentDetails;
use crate::models::PaymentType;
use crate::models::PaymentTypeFilter;
use crate::models::PrepareWithdrawRequest;
use crate::models::PrepareWithdrawResponse;
use crate::models::ReverseSwapInfo;
use crate::models::ReverseSwapPairInfo;
use crate::models::ReverseSwapStatus;
Expand Down Expand Up @@ -521,6 +523,22 @@ fn wire_sweep_impl(
},
)
}
fn wire_prepare_withdraw_impl(
port_: MessagePort,
prepare_withdraw_request: impl Wire2Api<PrepareWithdrawRequest> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
debug_name: "prepare_withdraw",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_prepare_withdraw_request = prepare_withdraw_request.wire2api();
move |task_callback| prepare_withdraw(api_prepare_withdraw_request)
},
)
}
fn wire_list_refundables_impl(port_: MessagePort) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
Expand Down Expand Up @@ -684,6 +702,7 @@ impl Wire2Api<PaymentTypeFilter> for i32 {
}
}
}

impl Wire2Api<u16> for u16 {
fn wire2api(self) -> u16 {
self
Expand Down Expand Up @@ -1123,6 +1142,18 @@ impl support::IntoDart for PaymentType {
}
}
impl support::IntoDartExceptPrimitive for PaymentType {}
impl support::IntoDart for PrepareWithdrawResponse {
fn into_dart(self) -> support::DartAbi {
vec![
self.raw_tx_hex.into_dart(),
self.sat_per_vbyte.into_dart(),
self.fee_sat.into_dart(),
]
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for PrepareWithdrawResponse {}

impl support::IntoDart for Rate {
fn into_dart(self) -> support::DartAbi {
vec![self.coin.into_dart(), self.value.into_dart()].into_dart()
Expand Down
Loading

0 comments on commit a652203

Please sign in to comment.