Skip to content

Commit

Permalink
Merge pull request #548 from danielgranhao/23-20-2023-implement-prepa…
Browse files Browse the repository at this point in the history
…re-refund

Implement `prepare_refund()`
  • Loading branch information
roeierez authored Oct 25, 2023
2 parents 7a1d40c + 88207b5 commit 6daf4b5
Show file tree
Hide file tree
Showing 18 changed files with 603 additions and 48 deletions.
14 changes: 14 additions & 0 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,17 @@ dictionary SendOnchainResponse {
ReverseSwapInfo reverse_swap_info;
};

dictionary PrepareRefundRequest {
string swap_address;
string to_address;
u32 sat_per_vbyte;
};

dictionary PrepareRefundResponse {
u32 refund_tx_weight;
u64 refund_tx_fee_sat;
};

dictionary RefundRequest {
string swap_address;
string to_address;
Expand Down Expand Up @@ -673,6 +684,9 @@ interface BlockingBreezServices {
[Throws=SdkError]
sequence<SwapInfo> list_refundables();

[Throws=SdkError]
PrepareRefundResponse prepare_refund(PrepareRefundRequest req);

[Throws=SdkError]
RefundResponse refund(RefundRequest req);

Expand Down
23 changes: 15 additions & 8 deletions libs/sdk-bindings/src/uniffi_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use breez_sdk_core::{
LocaleOverrides, LocalizedName, LogEntry, LogStream, LspInformation, MessageSuccessActionData,
MetadataItem, Network, NodeConfig, NodeState, OpenChannelFeeRequest, OpenChannelFeeResponse,
OpeningFeeParams, OpeningFeeParamsMenu, Payment, PaymentDetails, PaymentFailedData,
PaymentStatus, PaymentType, PaymentTypeFilter, PrepareSweepRequest, PrepareSweepResponse, Rate,
ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse, RecommendedFees,
RefundRequest, RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
ReverseSwapStatus, RouteHint, RouteHintHop, SendOnchainRequest, SendOnchainResponse,
SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest, SignMessageRequest,
SignMessageResponse, StaticBackupRequest, StaticBackupResponse, SuccessActionProcessed,
SwapInfo, SwapStatus, SweepRequest, SweepResponse, Symbol, UnspentTransactionOutput,
UrlSuccessActionData,
PaymentStatus, PaymentType, PaymentTypeFilter, PrepareRefundRequest, PrepareRefundResponse,
PrepareSweepRequest, PrepareSweepResponse, Rate, ReceiveOnchainRequest, ReceivePaymentRequest,
ReceivePaymentResponse, RecommendedFees, RefundRequest, RefundResponse, ReverseSwapFeesRequest,
ReverseSwapInfo, ReverseSwapPairInfo, ReverseSwapStatus, RouteHint, RouteHintHop,
SendOnchainRequest, SendOnchainResponse, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SuccessActionProcessed, SwapInfo, SwapStatus, SweepRequest,
SweepResponse, Symbol, UnspentTransactionOutput, UrlSuccessActionData,
};
use log::{Level, LevelFilter, Metadata, Record};
use once_cell::sync::{Lazy, OnceCell};
Expand Down Expand Up @@ -245,6 +245,13 @@ impl BlockingBreezServices {
.map_err(|e| e.into())
}

// prepare a refund transaction for a failed/expired swap
// optionally used to know fees before calling `refund()`
pub fn prepare_refund(&self, req: PrepareRefundRequest) -> SdkResult<PrepareRefundResponse> {
rt().block_on(self.breez_services.prepare_refund(req))
.map_err(|e| e.into())
}

// construct and broadcast a refund transaction for a faile/expired swap
pub fn refund(&self, req: RefundRequest) -> SdkResult<RefundResponse> {
rt().block_on(self.breez_services.refund(req))
Expand Down
17 changes: 11 additions & 6 deletions libs/sdk-core/src/binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ use crate::{
BackupStatus, BuyBitcoinRequest, BuyBitcoinResponse, CheckMessageRequest, CheckMessageResponse,
EnvironmentType, ListPaymentsRequest, LnUrlCallbackStatus, LnUrlPayRequest,
LnUrlWithdrawRequest, LnUrlWithdrawResult, NodeConfig, OpenChannelFeeRequest,
OpenChannelFeeResponse, PrepareSweepRequest, PrepareSweepResponse, ReceiveOnchainRequest,
ReceivePaymentRequest, ReceivePaymentResponse, RefundRequest, RefundResponse,
ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo, SendOnchainRequest,
SendOnchainResponse, SendPaymentRequest, SendPaymentResponse, SendSpontaneousPaymentRequest,
SignMessageRequest, SignMessageResponse, StaticBackupRequest, StaticBackupResponse,
SweepRequest, SweepResponse,
OpenChannelFeeResponse, PrepareRefundRequest, PrepareRefundResponse, PrepareSweepRequest,
PrepareSweepResponse, ReceiveOnchainRequest, ReceivePaymentRequest, ReceivePaymentResponse,
RefundRequest, RefundResponse, ReverseSwapFeesRequest, ReverseSwapInfo, ReverseSwapPairInfo,
SendOnchainRequest, SendOnchainResponse, SendPaymentRequest, SendPaymentResponse,
SendSpontaneousPaymentRequest, SignMessageRequest, SignMessageResponse, StaticBackupRequest,
StaticBackupResponse, SweepRequest, SweepResponse,
};

/*
Expand Down Expand Up @@ -314,6 +314,11 @@ pub fn list_refundables() -> Result<Vec<SwapInfo>> {
block_on(async { get_breez_services().await?.list_refundables().await })
}

/// See [BreezServices::prepare_refund]
pub fn prepare_refund(req: PrepareRefundRequest) -> Result<PrepareRefundResponse> {
block_on(async { get_breez_services().await?.prepare_refund(req).await })
}

/// See [BreezServices::refund]
pub fn refund(req: RefundRequest) -> Result<RefundResponse> {
block_on(async { get_breez_services().await?.refund(req).await })
Expand Down
8 changes: 8 additions & 0 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,14 @@ impl BreezServices {
self.btc_receive_swapper.list_refundables()
}

/// Prepares a refund transaction for a failed/expired swap.
///
/// Can optionally be used before [BreezServices::refund] to know how much fees will be paid
/// to perform the refund.
pub async fn prepare_refund(&self, req: PrepareRefundRequest) -> Result<PrepareRefundResponse> {
self.btc_receive_swapper.prepare_refund_swap(req).await
}

/// Construct and broadcast a refund transaction for a failed/expired swap
///
/// Returns the txid of the refund transaction.
Expand Down
49 changes: 49 additions & 0 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ pub extern "C" fn wire_list_refundables(port_: i64) {
wire_list_refundables_impl(port_)
}

#[no_mangle]
pub extern "C" fn wire_prepare_refund(port_: i64, req: *mut wire_PrepareRefundRequest) {
wire_prepare_refund_impl(port_, req)
}

#[no_mangle]
pub extern "C" fn wire_refund(port_: i64, req: *mut wire_RefundRequest) {
wire_refund_impl(port_, req)
Expand Down Expand Up @@ -306,6 +311,11 @@ pub extern "C" fn new_box_autoadd_opening_fee_params_0() -> *mut wire_OpeningFee
support::new_leak_box_ptr(wire_OpeningFeeParams::new_with_null_ptr())
}

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

#[no_mangle]
pub extern "C" fn new_box_autoadd_prepare_sweep_request_0() -> *mut wire_PrepareSweepRequest {
support::new_leak_box_ptr(wire_PrepareSweepRequest::new_with_null_ptr())
Expand Down Expand Up @@ -484,6 +494,12 @@ impl Wire2Api<OpeningFeeParams> for *mut wire_OpeningFeeParams {
Wire2Api::<OpeningFeeParams>::wire2api(*wrap).into()
}
}
impl Wire2Api<PrepareRefundRequest> for *mut wire_PrepareRefundRequest {
fn wire2api(self) -> PrepareRefundRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Wire2Api::<PrepareRefundRequest>::wire2api(*wrap).into()
}
}
impl Wire2Api<PrepareSweepRequest> for *mut wire_PrepareSweepRequest {
fn wire2api(self) -> PrepareSweepRequest {
let wrap = unsafe { support::box_from_leak_ptr(self) };
Expand Down Expand Up @@ -721,6 +737,15 @@ impl Wire2Api<OpeningFeeParams> for wire_OpeningFeeParams {
}
}

impl Wire2Api<PrepareRefundRequest> for wire_PrepareRefundRequest {
fn wire2api(self) -> PrepareRefundRequest {
PrepareRefundRequest {
swap_address: self.swap_address.wire2api(),
to_address: self.to_address.wire2api(),
sat_per_vbyte: self.sat_per_vbyte.wire2api(),
}
}
}
impl Wire2Api<PrepareSweepRequest> for wire_PrepareSweepRequest {
fn wire2api(self) -> PrepareSweepRequest {
PrepareSweepRequest {
Expand Down Expand Up @@ -951,6 +976,14 @@ pub struct wire_OpeningFeeParams {
promise: *mut wire_uint_8_list,
}

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

#[repr(C)]
#[derive(Clone)]
pub struct wire_PrepareSweepRequest {
Expand Down Expand Up @@ -1317,6 +1350,22 @@ impl Default for wire_OpeningFeeParams {
}
}

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

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

impl NewWithNullPtr for wire_PrepareSweepRequest {
fn new_with_null_ptr() -> Self {
Self {
Expand Down
29 changes: 29 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ use crate::models::PaymentDetails;
use crate::models::PaymentStatus;
use crate::models::PaymentType;
use crate::models::PaymentTypeFilter;
use crate::models::PrepareRefundRequest;
use crate::models::PrepareRefundResponse;
use crate::models::PrepareSweepRequest;
use crate::models::PrepareSweepResponse;
use crate::models::ReceiveOnchainRequest;
Expand Down Expand Up @@ -593,6 +595,22 @@ fn wire_list_refundables_impl(port_: MessagePort) {
move || move |task_callback| list_refundables(),
)
}
fn wire_prepare_refund_impl(
port_: MessagePort,
req: impl Wire2Api<PrepareRefundRequest> + UnwindSafe,
) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
debug_name: "prepare_refund",
port: Some(port_),
mode: FfiCallMode::Normal,
},
move || {
let api_req = req.wire2api();
move |task_callback| prepare_refund(api_req)
},
)
}
fn wire_refund_impl(port_: MessagePort, req: impl Wire2Api<RefundRequest> + UnwindSafe) {
FLUTTER_RUST_BRIDGE_HANDLER.wrap(
WrapInfo {
Expand Down Expand Up @@ -1279,6 +1297,17 @@ impl support::IntoDart for PaymentType {
}
}
impl support::IntoDartExceptPrimitive for PaymentType {}
impl support::IntoDart for PrepareRefundResponse {
fn into_dart(self) -> support::DartAbi {
vec![
self.refund_tx_weight.into_dart(),
self.refund_tx_fee_sat.into_dart(),
]
.into_dart()
}
}
impl support::IntoDartExceptPrimitive for PrepareRefundResponse {}

impl support::IntoDart for PrepareSweepResponse {
fn into_dart(self) -> support::DartAbi {
vec![
Expand Down
11 changes: 11 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -842,12 +842,23 @@ pub struct SendOnchainResponse {
pub reverse_swap_info: ReverseSwapInfo,
}

pub struct PrepareRefundRequest {
pub swap_address: String,
pub to_address: String,
pub sat_per_vbyte: u32,
}

pub struct RefundRequest {
pub swap_address: String,
pub to_address: String,
pub sat_per_vbyte: u32,
}

pub struct PrepareRefundResponse {
pub refund_tx_weight: u32,
pub refund_tx_fee_sat: u64,
}

pub struct RefundResponse {
pub refund_tx_id: String,
}
Expand Down
Loading

0 comments on commit 6daf4b5

Please sign in to comment.