Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix inbound_liquidity_msats #1062

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libs/sdk-bindings/src/breez_sdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ dictionary NodeState {
u64 max_single_payment_amount_msat;
u64 max_chan_reserve_msats;
sequence<string> connected_peers;
u64 max_receivable_single_payment_amount_msat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we add this field the former one (inbound_liquidity_msats) changes its semantic.
My concern is that developers will upgrade and their logic will break without them knowing about it.
Can we rename the inbound_liquidity_msats so developers will get a compilation error on upgrade which will force them to change the code?
We can also leave it with the old semantic (and mark it as deprecated) and add another field (total_inbound_liquidity_msat) with the unified liquidity.
Anything that will not let this change go under the radar.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point @roeierez. I'll rename it to total_inbound_liquidity_msat.

u64 inbound_liquidity_msats;
};

Expand Down
8 changes: 5 additions & 3 deletions libs/sdk-core/src/breez_services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ impl BreezServices {

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 {
match node_state.max_receivable_single_payment_amount_msat >= 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.
Expand Down Expand Up @@ -2469,7 +2469,8 @@ impl Receiver for PaymentReceiver {
let mut channel_fees_msat = None;

// check if we need to open channel
let open_channel_needed = node_state.inbound_liquidity_msats < req.amount_msat;
let open_channel_needed =
node_state.max_receivable_single_payment_amount_msat < req.amount_msat;
if open_channel_needed {
info!("We need to open a channel");

Expand Down Expand Up @@ -3273,7 +3274,8 @@ pub(crate) mod tests {
max_single_payment_amount_msat: 1_000,
max_chan_reserve_msats: 0,
connected_peers: vec!["1111".to_string()],
inbound_liquidity_msats: 2_000,
max_receivable_single_payment_amount_msat: 2_000,
inbound_liquidity_msats: 10_000,
}
}
}
3 changes: 3 additions & 0 deletions libs/sdk-core/src/bridge_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,9 @@ impl support::IntoDart for NodeState {
.into_dart(),
self.max_chan_reserve_msats.into_into_dart().into_dart(),
self.connected_peers.into_into_dart().into_dart(),
self.max_receivable_single_payment_amount_msat
.into_into_dart()
.into_dart(),
self.inbound_liquidity_msats.into_into_dart().into_dart(),
]
.into_dart()
Expand Down
5 changes: 4 additions & 1 deletion libs/sdk-core/src/greenlight/node_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ impl NodeAPI for Greenlight {
// calculate payment limits and inbound liquidity
let mut max_payable: u64 = 0;
let mut max_receivable_single_channel: u64 = 0;
let mut inbound_liquidity_msats: u64 = 0;
opened_channels.iter().try_for_each(|c| -> Result<()> {
max_payable += c
.spendable_msat
Expand All @@ -954,6 +955,7 @@ impl NodeAPI for Greenlight {
.as_ref()
.map(|a| a.msat)
.unwrap_or_default();
inbound_liquidity_msats += receivable_amount;
if receivable_amount > max_receivable_single_channel {
max_receivable_single_channel = receivable_amount;
}
Expand All @@ -976,7 +978,8 @@ impl NodeAPI for Greenlight {
max_single_payment_amount_msat: MAX_PAYMENT_AMOUNT_MSAT,
max_chan_reserve_msats: channels_balance - min(max_payable, channels_balance),
connected_peers,
inbound_liquidity_msats: max_receivable_single_channel,
max_receivable_single_payment_amount_msat: max_receivable_single_channel,
inbound_liquidity_msats,
};
let mut htlc_list: Vec<Htlc> = Vec::new();
for channel in all_channel_models.clone() {
Expand Down
5 changes: 5 additions & 0 deletions libs/sdk-core/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,11 @@ pub struct NodeState {
pub max_single_payment_amount_msat: u64,
pub max_chan_reserve_msats: u64,
pub connected_peers: Vec<String>,

/// Maximum receivable in a single payment without requiring a new channel open.
pub max_receivable_single_payment_amount_msat: u64,
danielgranhao marked this conversation as resolved.
Show resolved Hide resolved

/// Total receivable on all available channels
pub inbound_liquidity_msats: u64,
}

Expand Down
11 changes: 9 additions & 2 deletions libs/sdk-flutter/lib/bridge_generated.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1119,6 +1119,11 @@ class NodeState {
final int maxSinglePaymentAmountMsat;
final int maxChanReserveMsats;
final List<String> connectedPeers;

/// Maximum receivable in a single payment without requiring a new channel open.
final int maxReceivableSinglePaymentAmountMsat;

/// Total receivable on all available channels
final int inboundLiquidityMsats;

const NodeState({
Expand All @@ -1133,6 +1138,7 @@ class NodeState {
required this.maxSinglePaymentAmountMsat,
required this.maxChanReserveMsats,
required this.connectedPeers,
required this.maxReceivableSinglePaymentAmountMsat,
required this.inboundLiquidityMsats,
});
}
Expand Down Expand Up @@ -3749,7 +3755,7 @@ class BreezSdkCoreImpl implements BreezSdkCore {

NodeState _wire2api_node_state(dynamic raw) {
final arr = raw as List<dynamic>;
if (arr.length != 12) throw Exception('unexpected arr length: expect 12 but see ${arr.length}');
if (arr.length != 13) throw Exception('unexpected arr length: expect 13 but see ${arr.length}');
return NodeState(
id: _wire2api_String(arr[0]),
blockHeight: _wire2api_u32(arr[1]),
Expand All @@ -3762,7 +3768,8 @@ class BreezSdkCoreImpl implements BreezSdkCore {
maxSinglePaymentAmountMsat: _wire2api_u64(arr[8]),
maxChanReserveMsats: _wire2api_u64(arr[9]),
connectedPeers: _wire2api_StringList(arr[10]),
inboundLiquidityMsats: _wire2api_u64(arr[11]),
maxReceivableSinglePaymentAmountMsat: _wire2api_u64(arr[11]),
inboundLiquidityMsats: _wire2api_u64(arr[12]),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1755,6 +1755,7 @@ fun asNodeState(nodeState: ReadableMap): NodeState? {
"maxSinglePaymentAmountMsat",
"maxChanReserveMsats",
"connectedPeers",
"maxReceivableSinglePaymentAmountMsat",
"inboundLiquidityMsats",
),
)
Expand All @@ -1772,6 +1773,7 @@ fun asNodeState(nodeState: ReadableMap): NodeState? {
val maxSinglePaymentAmountMsat = nodeState.getDouble("maxSinglePaymentAmountMsat").toULong()
val maxChanReserveMsats = nodeState.getDouble("maxChanReserveMsats").toULong()
val connectedPeers = nodeState.getArray("connectedPeers")?.let { asStringList(it) }!!
val maxReceivableSinglePaymentAmountMsat = nodeState.getDouble("maxReceivableSinglePaymentAmountMsat").toULong()
val inboundLiquidityMsats = nodeState.getDouble("inboundLiquidityMsats").toULong()
return NodeState(
id,
Expand All @@ -1785,6 +1787,7 @@ fun asNodeState(nodeState: ReadableMap): NodeState? {
maxSinglePaymentAmountMsat,
maxChanReserveMsats,
connectedPeers,
maxReceivableSinglePaymentAmountMsat,
inboundLiquidityMsats,
)
}
Expand All @@ -1802,6 +1805,7 @@ fun readableMapOf(nodeState: NodeState): ReadableMap =
"maxSinglePaymentAmountMsat" to nodeState.maxSinglePaymentAmountMsat,
"maxChanReserveMsats" to nodeState.maxChanReserveMsats,
"connectedPeers" to readableArrayOf(nodeState.connectedPeers),
"maxReceivableSinglePaymentAmountMsat" to nodeState.maxReceivableSinglePaymentAmountMsat,
"inboundLiquidityMsats" to nodeState.inboundLiquidityMsats,
)

Expand Down
5 changes: 5 additions & 0 deletions libs/sdk-react-native/ios/BreezSDKMapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2021,6 +2021,9 @@ enum BreezSDKMapper {
guard let connectedPeers = nodeState["connectedPeers"] as? [String] else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "connectedPeers", typeName: "NodeState"))
}
guard let maxReceivableSinglePaymentAmountMsat = nodeState["maxReceivableSinglePaymentAmountMsat"] as? UInt64 else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "maxReceivableSinglePaymentAmountMsat", typeName: "NodeState"))
}
guard let inboundLiquidityMsats = nodeState["inboundLiquidityMsats"] as? UInt64 else {
throw SdkError.Generic(message: errMissingMandatoryField(fieldName: "inboundLiquidityMsats", typeName: "NodeState"))
}
Expand All @@ -2037,6 +2040,7 @@ enum BreezSDKMapper {
maxSinglePaymentAmountMsat: maxSinglePaymentAmountMsat,
maxChanReserveMsats: maxChanReserveMsats,
connectedPeers: connectedPeers,
maxReceivableSinglePaymentAmountMsat: maxReceivableSinglePaymentAmountMsat,
inboundLiquidityMsats: inboundLiquidityMsats
)
}
Expand All @@ -2054,6 +2058,7 @@ enum BreezSDKMapper {
"maxSinglePaymentAmountMsat": nodeState.maxSinglePaymentAmountMsat,
"maxChanReserveMsats": nodeState.maxChanReserveMsats,
"connectedPeers": nodeState.connectedPeers,
"maxReceivableSinglePaymentAmountMsat": nodeState.maxReceivableSinglePaymentAmountMsat,
"inboundLiquidityMsats": nodeState.inboundLiquidityMsats,
]
}
Expand Down
1 change: 1 addition & 0 deletions libs/sdk-react-native/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export interface NodeState {
maxSinglePaymentAmountMsat: number
maxChanReserveMsats: number
connectedPeers: string[]
maxReceivableSinglePaymentAmountMsat: number
inboundLiquidityMsats: number
}

Expand Down
Loading