Skip to content

Commit

Permalink
Merge pull request #737 from breez/ok300-fees
Browse files Browse the repository at this point in the history
Expand result of open_channel_fee SDK method
  • Loading branch information
ok300 authored Jan 22, 2024
2 parents 55e08fa + e38582a commit f2280e0
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 74 deletions.
6 changes: 3 additions & 3 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -468,13 +468,13 @@ dictionary LspInformation {
};

dictionary OpenChannelFeeRequest {
u64 amount_msat;
u64? amount_msat;
u32? expiry = null;
};

dictionary OpenChannelFeeResponse {
u64 fee_msat;
OpeningFeeParams? used_fee_params;
u64? fee_msat;
OpeningFeeParams fee_params;
};

enum SwapStatus {
Expand Down
36 changes: 15 additions & 21 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -645,35 +645,29 @@ impl BreezServices {
}

/// Gets the fees required to open a channel for a given amount.
/// If there is no channel needed, returns 0.
/// If there is a channel needed, returns the required open channel fees, with a fee params object
/// to pass to methods that require a channel open, like receive_payment, or receive_onchain.
/// If no channel is needed, returns 0. If a channel is needed, returns the required opening fees.
pub async fn open_channel_fee(
&self,
req: OpenChannelFeeRequest,
) -> SdkResult<OpenChannelFeeResponse> {
// get the node state to fetch the current inbound liquidity.
let node_state = self.persister.get_node_state()?.ok_or(SdkError::Generic {
err: "Node info not found".into(),
})?;

// In case we have enough inbound liquidity we return zero fee.
if node_state.inbound_liquidity_msats >= req.amount_msat {
return Ok(OpenChannelFeeResponse {
fee_msat: 0,
used_fee_params: None,
});
}

// Otherwise we need to calculate the fee for opening a new channel.
let lsp_info = self.lsp_info().await?;
let used_fee_params = lsp_info
.cheapest_open_channel_fee(req.expiry.unwrap_or(INVOICE_PAYMENT_FEE_EXPIRY_SECONDS))?;
let fee_msat = used_fee_params.get_channel_fees_msat_for(req.amount_msat);
let fee_params = lsp_info
.cheapest_open_channel_fee(req.expiry.unwrap_or(INVOICE_PAYMENT_FEE_EXPIRY_SECONDS))?
.clone();

let node_state = self.node_info()?;
let fee_msat = req.amount_msat.map(|req_amount_msat| {
match node_state.inbound_liquidity_msats >= req_amount_msat {
// In case we have enough inbound liquidity we return zero fee.
true => 0,
// Otherwise we need to calculate the fee for opening a new channel.
false => fee_params.get_channel_fees_msat_for(req_amount_msat),
}
});

Ok(OpenChannelFeeResponse {
fee_msat,
used_fee_params: Some(used_fee_params.clone()),
fee_params,
})
}

Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-core/src/bridge_generated.io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1112,7 +1112,7 @@ pub struct wire_MetadataFilter {
#[repr(C)]
#[derive(Clone)]
pub struct wire_OpenChannelFeeRequest {
amount_msat: u64,
amount_msat: *mut u64,
expiry: *mut u32,
}

Expand Down Expand Up @@ -1519,7 +1519,7 @@ pub extern "C" fn inflate_NodeConfig_Greenlight() -> *mut NodeConfigKind {
impl NewWithNullPtr for wire_OpenChannelFeeRequest {
fn new_with_null_ptr() -> Self {
Self {
amount_msat: Default::default(),
amount_msat: core::ptr::null_mut(),
expiry: core::ptr::null_mut(),
}
}
Expand Down
4 changes: 2 additions & 2 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1609,8 +1609,8 @@ impl rust2dart::IntoIntoDart<NodeState> for NodeState {
impl support::IntoDart for OpenChannelFeeResponse {
fn into_dart(self) -> support::DartAbi {
vec![
self.fee_msat.into_into_dart().into_dart(),
self.used_fee_params.into_dart(),
self.fee_msat.into_dart(),
self.fee_params.into_into_dart().into_dart(),
]
.into_dart()
}
Expand Down
9 changes: 6 additions & 3 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,14 +879,17 @@ pub struct StaticBackupResponse {
}

pub struct OpenChannelFeeRequest {
pub amount_msat: u64,
pub amount_msat: Option<u64>,
pub expiry: Option<u32>,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct OpenChannelFeeResponse {
pub fee_msat: u64,
pub used_fee_params: Option<OpeningFeeParams>,
/// Opening fee for receiving the amount set in the [OpenChannelFeeRequest], in case it was set.
/// It may be zero if no new channel needs to be opened.
pub fee_msat: Option<u64>,
/// The fee params for receiving more than the current inbound liquidity.
pub fee_params: OpeningFeeParams,
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
Expand Down
2 changes: 1 addition & 1 deletion libs/sdk-flutter/ios/Classes/bridge_generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ typedef struct wire_RefundRequest {
} wire_RefundRequest;

typedef struct wire_OpenChannelFeeRequest {
uint64_t amount_msat;
uint64_t *amount_msat;
uint32_t *expiry;
} wire_OpenChannelFeeRequest;

Expand Down
25 changes: 14 additions & 11 deletions libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1105,22 +1105,26 @@ class NodeState {
}

class OpenChannelFeeRequest {
final int amountMsat;
final int? amountMsat;
final int? expiry;

const OpenChannelFeeRequest({
required this.amountMsat,
this.amountMsat,
this.expiry,
});
}

class OpenChannelFeeResponse {
final int feeMsat;
final OpeningFeeParams? usedFeeParams;
/// Opening fee for receiving the amount set in the [OpenChannelFeeRequest], in case it was set.
/// It may be zero if no new channel needs to be opened.
final int? feeMsat;

/// The fee params for receiving more than the current inbound liquidity.
final OpeningFeeParams feeParams;

const OpenChannelFeeResponse({
required this.feeMsat,
this.usedFeeParams,
this.feeMsat,
required this.feeParams,
});
}

Expand Down Expand Up @@ -3421,8 +3425,8 @@ class BreezSdkCoreImpl implements BreezSdkCore {
final arr = raw as List<dynamic>;
if (arr.length != 2) throw Exception('unexpected arr length: expect 2 but see ${arr.length}');
return OpenChannelFeeResponse(
feeMsat: _wire2api_u64(arr[0]),
usedFeeParams: _wire2api_opt_box_autoadd_opening_fee_params(arr[1]),
feeMsat: _wire2api_opt_box_autoadd_u64(arr[0]),
feeParams: _wire2api_opening_fee_params(arr[1]),
);
}

Expand Down Expand Up @@ -4442,7 +4446,7 @@ class BreezSdkCorePlatform extends FlutterRustBridgeBase<BreezSdkCoreWire> {

void _api_fill_to_wire_open_channel_fee_request(
OpenChannelFeeRequest apiObj, wire_OpenChannelFeeRequest wireObj) {
wireObj.amount_msat = api2wire_u64(apiObj.amountMsat);
wireObj.amount_msat = api2wire_opt_box_autoadd_u64(apiObj.amountMsat);
wireObj.expiry = api2wire_opt_box_autoadd_u32(apiObj.expiry);
}

Expand Down Expand Up @@ -6088,8 +6092,7 @@ final class wire_RefundRequest extends ffi.Struct {
}

final class wire_OpenChannelFeeRequest extends ffi.Struct {
@ffi.Uint64()
external int amount_msat;
external ffi.Pointer<ffi.Uint64> amount_msat;

external ffi.Pointer<ffi.Uint32> expiry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1705,14 +1705,21 @@ fun asNodeStateList(arr: ReadableArray): List<NodeState> {
fun asOpenChannelFeeRequest(openChannelFeeRequest: ReadableMap): OpenChannelFeeRequest? {
if (!validateMandatoryFields(
openChannelFeeRequest,
arrayOf(
"amountMsat",
),
arrayOf(),
)
) {
return null
}
val amountMsat = openChannelFeeRequest.getDouble("amountMsat").toULong()
val amountMsat =
if (hasNonNullKey(
openChannelFeeRequest,
"amountMsat",
)
) {
openChannelFeeRequest.getDouble("amountMsat").toULong()
} else {
null
}
val expiry = if (hasNonNullKey(openChannelFeeRequest, "expiry")) openChannelFeeRequest.getInt("expiry").toUInt() else null
return OpenChannelFeeRequest(
amountMsat,
Expand Down Expand Up @@ -1742,31 +1749,24 @@ fun asOpenChannelFeeResponse(openChannelFeeResponse: ReadableMap): OpenChannelFe
if (!validateMandatoryFields(
openChannelFeeResponse,
arrayOf(
"feeMsat",
"feeParams",
),
)
) {
return null
}
val feeMsat = openChannelFeeResponse.getDouble("feeMsat").toULong()
val usedFeeParams =
if (hasNonNullKey(openChannelFeeResponse, "usedFeeParams")) {
openChannelFeeResponse.getMap("usedFeeParams")?.let {
asOpeningFeeParams(it)
}
} else {
null
}
val feeMsat = if (hasNonNullKey(openChannelFeeResponse, "feeMsat")) openChannelFeeResponse.getDouble("feeMsat").toULong() else null
val feeParams = openChannelFeeResponse.getMap("feeParams")?.let { asOpeningFeeParams(it) }!!
return OpenChannelFeeResponse(
feeMsat,
usedFeeParams,
feeParams,
)
}

fun readableMapOf(openChannelFeeResponse: OpenChannelFeeResponse): ReadableMap {
return readableMapOf(
"feeMsat" to openChannelFeeResponse.feeMsat,
"usedFeeParams" to openChannelFeeResponse.usedFeeParams?.let { readableMapOf(it) },
"feeParams" to readableMapOf(openChannelFeeResponse.feeParams),
)
}

Expand Down
30 changes: 19 additions & 11 deletions libs/sdk-react-native/ios/BreezSDKMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1878,8 +1878,12 @@ enum BreezSDKMapper {
}

static func asOpenChannelFeeRequest(openChannelFeeRequest: [String: Any?]) throws -> OpenChannelFeeRequest {
guard let amountMsat = openChannelFeeRequest["amountMsat"] as? UInt64 else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "amountMsat", typeName: "OpenChannelFeeRequest"))
var amountMsat: UInt64?
if hasNonNilKey(data: openChannelFeeRequest, key: "amountMsat") {
guard let amountMsatTmp = openChannelFeeRequest["amountMsat"] as? UInt64 else {
throw SdkError.Generic(message: errUnexpectedValue(fieldName: "amountMsat"))
}
amountMsat = amountMsatTmp
}
var expiry: UInt32?
if hasNonNilKey(data: openChannelFeeRequest, key: "expiry") {
Expand All @@ -1897,7 +1901,7 @@ enum BreezSDKMapper {

static func dictionaryOf(openChannelFeeRequest: OpenChannelFeeRequest) -> [String: Any?] {
return [
"amountMsat": openChannelFeeRequest.amountMsat,
"amountMsat": openChannelFeeRequest.amountMsat == nil ? nil : openChannelFeeRequest.amountMsat,
"expiry": openChannelFeeRequest.expiry == nil ? nil : openChannelFeeRequest.expiry,
]
}
Expand All @@ -1920,24 +1924,28 @@ enum BreezSDKMapper {
}

static func asOpenChannelFeeResponse(openChannelFeeResponse: [String: Any?]) throws -> OpenChannelFeeResponse {
guard let feeMsat = openChannelFeeResponse["feeMsat"] as? UInt64 else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "feeMsat", typeName: "OpenChannelFeeResponse"))
var feeMsat: UInt64?
if hasNonNilKey(data: openChannelFeeResponse, key: "feeMsat") {
guard let feeMsatTmp = openChannelFeeResponse["feeMsat"] as? UInt64 else {
throw SdkError.Generic(message: errUnexpectedValue(fieldName: "feeMsat"))
}
feeMsat = feeMsatTmp
}
var usedFeeParams: OpeningFeeParams?
if let usedFeeParamsTmp = openChannelFeeResponse["usedFeeParams"] as? [String: Any?] {
usedFeeParams = try asOpeningFeeParams(openingFeeParams: usedFeeParamsTmp)
guard let feeParamsTmp = openChannelFeeResponse["feeParams"] as? [String: Any?] else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "feeParams", typeName: "OpenChannelFeeResponse"))
}
let feeParams = try asOpeningFeeParams(openingFeeParams: feeParamsTmp)

return OpenChannelFeeResponse(
feeMsat: feeMsat,
usedFeeParams: usedFeeParams
feeParams: feeParams
)
}

static func dictionaryOf(openChannelFeeResponse: OpenChannelFeeResponse) -> [String: Any?] {
return [
"feeMsat": openChannelFeeResponse.feeMsat,
"usedFeeParams": openChannelFeeResponse.usedFeeParams == nil ? nil : dictionaryOf(openingFeeParams: openChannelFeeResponse.usedFeeParams!),
"feeMsat": openChannelFeeResponse.feeMsat == nil ? nil : openChannelFeeResponse.feeMsat,
"feeParams": dictionaryOf(openingFeeParams: openChannelFeeResponse.feeParams),
]
}

Expand Down
6 changes: 3 additions & 3 deletions libs/sdk-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,13 +272,13 @@ export type NodeState = {
}

export type OpenChannelFeeRequest = {
amountMsat: number
amountMsat?: number
expiry?: number
}

export type OpenChannelFeeResponse = {
feeMsat: number
usedFeeParams?: OpeningFeeParams
feeMsat?: number
feeParams: OpeningFeeParams
}

export type OpeningFeeParams = {
Expand Down
2 changes: 1 addition & 1 deletion tools/sdk-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ pub(crate) enum Commands {

OpenChannelFee {
/// The received amount
amount_msat: u64,
amount_msat: Option<u64>,

/// The expiration of the fee returned
expiry: Option<u32>,
Expand Down

0 comments on commit f2280e0

Please sign in to comment.