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 Sep 14, 2023
1 parent 0022fad commit 6b218f2
Show file tree
Hide file tree
Showing 17 changed files with 633 additions and 150 deletions.
20 changes: 16 additions & 4 deletions libs/Cargo.lock

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

24 changes: 21 additions & 3 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,22 @@ enum BuyBitcoinProvider {
"Moonpay",
};

interface BlockingBreezServices {
dictionary SweepRequest {
string to_address;
u64 sat_per_kw;
};

dictionary PrepareWithdrawRequest {
string to_address;
u64 fee_rate_sats_per_vbyte;
};

dictionary PrepareWithdrawResponse {
u64 weight;
u64 fee_sat;
};

interface BlockingBreezServices {

[Throws=SdkError]
void disconnect();
Expand Down Expand Up @@ -532,7 +547,7 @@ interface BlockingBreezServices {
sequence<Payment> list_payments(PaymentTypeFilter filter, i64? from_timestamp, i64? to_timestamp);

[Throws=SdkError]
void sweep(string to_address, u64 fee_rate_sats_per_vbyte);
void sweep(SweepRequest req);

[Throws=SdkError]
sequence<Rate> fetch_fiat_rates();
Expand Down Expand Up @@ -593,6 +608,9 @@ interface BlockingBreezServices {

[Throws=SdkError]
BuyBitcoinResponse buy_bitcoin(BuyBitcoinRequest req);

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

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

Config default_config(EnvironmentType env_type, string api_key, NodeConfig node_config);
};
};
30 changes: 19 additions & 11 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ use breez_sdk_core::{
LocalizedName, LogEntry, LogStream, LspInformation, MessageSuccessActionData, MetadataItem,
Network, NodeConfig, NodeState, OpenChannelFeeRequest, OpenChannelFeeResponse,
OpeningFeeParams, OpeningFeeParamsMenu, Payment, PaymentDetails, PaymentFailedData,
PaymentType, PaymentTypeFilter, Rate, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, RecommendedFees, ReverseSwapFeesRequest, ReverseSwapInfo,
ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop, SignMessageRequest,
SignMessageResponse, SuccessActionProcessed, SwapInfo, SwapStatus, Symbol,
UnspentTransactionOutput, UrlSuccessActionData,
PaymentType, PaymentTypeFilter, PrepareWithdrawRequest, PrepareWithdrawResponse, Rate,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint,
RouteHintHop, SignMessageRequest, SignMessageResponse, SuccessActionProcessed, SwapInfo,
SwapStatus, SweepRequest, Symbol, UnspentTransactionOutput, UrlSuccessActionData,
};

static RT: Lazy<tokio::runtime::Runtime> = Lazy::new(|| tokio::runtime::Runtime::new().unwrap());
Expand Down Expand Up @@ -199,12 +199,9 @@ impl BlockingBreezServices {
.map_err(|e| e.into())
}

pub fn sweep(&self, to_address: String, fee_rate_sats_per_vbyte: u64) -> SdkResult<()> {
rt().block_on(
self.breez_services
.sweep(to_address, fee_rate_sats_per_vbyte),
)
.map_err(|e| e.into())
pub fn sweep(&self, req: SweepRequest) -> SdkResult<()> {
rt().block_on(self.breez_services.sweep(req))
.map_err(|e| e.into())
}

pub fn fetch_fiat_rates(&self) -> SdkResult<Vec<Rate>> {
Expand Down Expand Up @@ -332,6 +329,17 @@ impl BlockingBreezServices {
pub fn buy_bitcoin(&self, req: BuyBitcoinRequest) -> SdkResult<BuyBitcoinResponse> {
rt().block_on(self.breez_services.buy_bitcoin(req))
}

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
2 changes: 1 addition & 1 deletion libs/sdk-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ base64 = "0.13.0"
chrono = "*"
ecies = { version = "0.2", default-features = false, features = ["pure"] }
env_logger = "*"
futures = "0.3.28"
ripemd = "*"
rand = "*"
tiny-bip39 = "*"
Expand Down Expand Up @@ -61,7 +62,6 @@ const_format = "*"
miniz_oxide = "0.7.1"

[dev-dependencies]
futures = "0.3.28"
mockito = "0.31.1"
regex = "1.8.1"
tower = "0.4.13"
Expand Down
20 changes: 11 additions & 9 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ use crate::lsp::LspInformation;
use crate::models::{Config, LogEntry, NodeState, Payment, PaymentTypeFilter, SwapInfo};
use crate::{
BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse,
EnvironmentType, LnUrlCallbackStatus, NodeConfig, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
SignMessageRequest, SignMessageResponse,
EnvironmentType, LnUrlCallbackStatus, NodeConfig, PrepareWithdrawRequest,
PrepareWithdrawResponse, ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, SignMessageRequest,
SignMessageResponse, SweepRequest,
};

static BREEZ_SERVICES_INSTANCE: OnceCell<Arc<BreezServices>> = OnceCell::new();
Expand Down Expand Up @@ -320,12 +321,13 @@ pub fn buy_bitcoin(req_data: BuyBitcoinRequest) -> Result<BuyBitcoinResponse> {
}

/// See [BreezServices::sweep]
pub fn sweep(to_address: String, fee_rate_sats_per_vbyte: u64) -> Result<()> {
block_on(async {
get_breez_services()?
.sweep(to_address, fee_rate_sats_per_vbyte)
.await
})
pub fn sweep(req: SweepRequest) -> Result<()> {
block_on(async { get_breez_services()?.sweep(req).await })
}

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

/* Refundables API's */
Expand Down
28 changes: 18 additions & 10 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,15 +449,23 @@ impl BreezServices {
}

/// Sweep on-chain funds to the specified on-chain address, with the given feerate
pub async fn sweep(&self, to_address: String, fee_rate_sats_per_vbyte: u64) -> Result<()> {
pub async fn sweep(&self, req: SweepRequest) -> Result<()> {
self.start_node().await?;
self.node_api
.sweep(to_address, fee_rate_sats_per_vbyte)
.await?;
self.node_api.sweep(req).await?;
self.sync().await?;
Ok(())
}

pub async fn prepare_withdraw(
&self,
req: PrepareWithdrawRequest,
) -> Result<PrepareWithdrawResponse> {
self.start_node().await?;
let response = self.node_api.prepare_withdraw(req).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 @@ -1182,7 +1190,7 @@ impl log::Log for GlobalSdkLogger {
fn flush(&self) {}
}

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 Expand Up @@ -1312,6 +1320,11 @@ impl BreezServicesBuilder {
.unwrap_or_else(|| Arc::new(SqliteStorage::new(self.config.working_dir.clone())));
persister.init()?;

// mempool space is used to monitor the chain
let chain_service = Arc::new(MempoolSpace::from_base_url(
self.config.mempoolspace_url.clone(),
));

let mut node_api = self.node_api.clone();
let mut backup_transport = self.backup_transport.clone();
if node_api.is_none() {
Expand Down Expand Up @@ -1382,11 +1395,6 @@ impl BreezServicesBuilder {
self.config.api_key.clone(),
));

// mempool space is used to monitor the chain
let chain_service = Arc::new(MempoolSpace::from_base_url(
self.config.mempoolspace_url.clone(),
));

let current_lsp_id = persister.get_lsp_id()?;
if current_lsp_id.is_none() && self.config.default_lsp_id.is_some() {
persister.set_lsp_id(self.config.default_lsp_id.clone().unwrap())?;
Expand Down
Loading

0 comments on commit 6b218f2

Please sign in to comment.