diff --git a/src/guide/receive_onchain.md b/src/guide/receive_onchain.md index 7fd0f12..37edab8 100644 --- a/src/guide/receive_onchain.md +++ b/src/guide/receive_onchain.md @@ -450,13 +450,10 @@ To calculate the fees for a channel being opened by the LSP:
```rust,ignore -async fn calculate_channel_opening_fee(amount_msat: u64) -> Result { - let channel_opening_fee_needed = is_channel_opening_fee_needed(amount_msat, sdk.clone())?; - match channel_opening_fee_needed { - true => calculate_fees_for_amount(amount_msat).await, - false => Ok(0), - } -} +let amount_msat = ; +let channel_fees = sdk.open_channel_fee( + OpenChannelFeeRequest { amount_msat, expiry: None }) + .await?; ```
@@ -464,12 +461,12 @@ async fn calculate_channel_opening_fee(amount_msat: u64) -> Result {
```swift -func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? { - var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats: amountMsats) - if channelOpeningFeeNeeded { - return calculateFeesForAmount(amountMsats: amountMsats) - } - return nil +let amountMsat = +do { + let channelFees = try sdk.openChannelFee( + req: OpenChannelFeeRequest(amountMsat: amountMsat)) +} catch { + // Handle error } ```
@@ -478,13 +475,7 @@ func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
```kotlin,ignore -fun calculateChannelOpeningFee(amountMsats: Long): Long? { - val channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats) - if (channelOpeningFeeNeeded) { - return calculateFeesForAmount(amountMsats) - } - return null -} +// TODO add example for openChannelFee ```
@@ -492,12 +483,11 @@ fun calculateChannelOpeningFee(amountMsats: Long): Long? {
```typescript -const calculateChannelOpeningFee = async (amountMsats: number): number => { - const channelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsats) - if (channelOpeningFeeNeeded) { - return calculateFeesForAmount(amountMsats) - } - return 0 +const amountMsat = +try { + const channelFees = await openChannelFee({amountMsat: amountMsat}) +} catch (error) { + // handle error } ```
@@ -506,10 +496,15 @@ const calculateChannelOpeningFee = async (amountMsats: number): number => {
```dart -int calculateChannelOpeningFee(int amountMsat) async { - bool isChannelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsat); - return isChannelOpeningFeeNeeded ? calculateFeesForAmount(amountMsat) : 0; -} +int amountMsats = +try { + int channelFees = openChannelFee(OpenChannelFeeRequest( + amountMsats: amountMsats, + ), + ); +} catch { + // Handle error +} ```
@@ -517,13 +512,13 @@ int calculateChannelOpeningFee(int amountMsat) async {
```python -def calculate_channel_opening_fees(amount_msats): - is_channel_opening_fee_needed = is_channel_opening_fee_needed() - - if is_channel_opening_fee_needed: - return calculate_fees_for_amount(amount_msats) - else: - return None +amount_msat = +try: + channel_fees = sdk_services.open_channel_fee( + breez_sdk.OpenChannelFeeRequest( + amount_msat=amount_msat)) +except Exception as error: + # Handle error ```
@@ -531,13 +526,8 @@ def calculate_channel_opening_fees(amount_msats):
```go -func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) { - isChannelOpeningFeeNeeded := isChannelOpeningFeeNeeded(amountMsats) - if !isChannelOpeningFeeNeeded { - return 0, fmt.Errorf("Channel not needed") - } - return calculateFeesForAmount(amountMsats), nil -} +amountMsat := +channelFees, err := sdkServices.OpenChannelFee(breez_sdk.OpenChannelFeeRequest{AmountMsat: amountMsat}) ```
@@ -545,325 +535,13 @@ func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
```cs -ulong calculateChannelOpeningFee(ulong amountMsats) -{ - var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats); - if (channelOpeningFeeNeeded) - { - return calculateFeesForAmount(amountMsats); - } - return 0; -} -``` -
- - -How to detect if open channel fees are needed: - - -
Rust
-
- -```rust,ignore -fn is_channel_opening_fee_needed(amount_msats: u64) -> Result { - let node_info = sdk.node_info()?.ok_or(anyhow!("No node info found"))?; - Ok(node_info.inbound_liquidity_msats <= amount_msats) -} -``` -
- -
Swift
-
- -```swift -func isChannelOpeningFeeNeeded(amountMsats: Int64) -> Bool { - do { - let nodeInfo = try sdk.nodeInfo() - - if let inboundLiquidityMsats = nodeInfo?.inboundLiquidityMsats { - return inboundLiquidityMsats <= amountMsats - } - } catch { - // Handle error - } - return false -} -``` -
- -
Android
-
- -```kotlin,ignore -fun isChannelOpeningFeeNeeded(amountMsats: Long): Boolean { - try { - val nodeInfo = sdk.nodeInfo() - val inboundLiquidityMsats = nodeInfo?.inboundLiquidityMsats?.toLong() - if (inboundLiquidityMsats != null) { - return inboundLiquidityMsats <= amountMsats - } - } catch (e: Exception) { - // Handle error - } - return false -} -``` -
- -
React Native
-
- -```typescript -const isChannelOpeningFeeNeeded = async (amountMsats: number): boolean => { - try { - const nodeInfo = await nodeInfo() - return nodeInfo.inboundLiquidityMsats <= amountMsats - } catch (error) { - // handle error - } - return false -} -``` -
- -
Dart
-
- -```dart -// Assumes nodeState isn't empty -bool isChannelOpeningFeeNeeded(int amountMsat) async { - NodeState? nodeState = await getNodeState(); - return amountMsat >= nodeState.inboundLiquidityMsats; -} -``` -
- -
Python
-
- -```python -def is_channel_opening_fee_needed(amount_msats): - return sdk_services.node_info().inbound_liquidity_msats <= amount_msats -``` -
- -
Go
-
- -```go -func isChannelOpeningFeeNeeded(amountMsats uint64) bool { - nodeInfo, err := sdkServices.NodeInfo() - if err != nil { - // Handle error - } - return nodeInfo.InboundLiquidityMsats <= amountMsats -} -``` -
- -
C#
-
- -```cs -bool isChannelOpeningFeeNeeded(ulong amountMsats) -{ - try - { - var nodeInfo = sdk.NodeInfo(); - return nodeInfo.inboundLiquidityMsats <= amountMsats; - } - catch (Exception) - { - // handle error - } - - return false; -} -``` -
-
- -LSP fees are calculated in two ways, either by a minimum fee set by the LSP or by a fee per myriad calculated based on the amount being received. If the fee calculated from the fee per myriad is less than the minimum fee, the minimum fee is used. - -This information can be retrieved for each LSP and then calculated: - - -
Rust
-
- -```rust,ignore -async fn calculate_fees_for_amount(amount_msat: u64) -> Result { - let lsp_id = sdk.lsp_id().await?.ok_or(anyhow!("No lsp id found"))?; - let lsp_info = sdk - .fetch_lsp_info(lsp_id) - .await? - .ok_or(anyhow!("No lsp id found"))?; - - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - let channel_dynamic_fee_msat = - amount_msat * lsp_info.channel_fee_permyriad as u64 / 10_000 / 1000 * 1000; - let fee_msat = max( - lsp_info.channel_minimum_fee_msat as u64, - channel_dynamic_fee_msat, - ) - Ok(fee_msat) -} -``` -
- -
Swift
-
- -```swift -func calculateFeesForAmount(amountMsats: Int64) -> Int64? { - do { - if let lspId = try sdk.lspId() { - let lspInfo = try sdk.fetchLspInfo(lspId: lspId) - - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - let channelDynamicFeeMsat = amountMsats * lspInfo!.channelFeePermyriad / 10_000 / 1000 * 1000 - let feeMsat = max(lspInfo!.channelMinimumFeeMsat, channelDynamicFeeMsat) - - return feeMsat - } - } catch { - // Handle error - } - return nil -} -``` -
- -
Android
-
- -```kotlin,ignore -fun calculateFeesForAmount(amountMsats: Long): Long? { - try { - val lspId = sdk.lspId() ?: return null - val lspInfo = sdk.fetchLspInfo(lspId) ?: return null - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - val channelDynamicFeeMsat = amountMsats * lspInfo.channelFeePermyriad / 1000 - return lspInfo.channelMinimumFeeMsat.coerceAtLeast(channelDynamicFeeMsat) - } catch (e: Exception) { - // Handle error - } - return null -} -``` -
- -
React Native
-
- -```typescript -const calculateFeesForAmount = async (amountMsats: number): number => { - try { - const id = await lspId() - const lspInfo = await fetchLspInfo(id) - - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - const channelDynamicFeeMsat = amountMsats * lspInfo.channelFeePermyriad / 10000 / 1000 * 1000 - const feeMsat = Math.max(lspInfo.channelMinimumFeeMsat, channelDynamicFeeMsat) - - return feeMsat - } catch (error) { - // handle error - } - return 0 -} -``` -
- -
Dart
-
- -```dart -// Assumes lspId & lspInformation isn't empty -int calculateFeesForAmount(int amountMsat) async { - String? lspId = await getLspId(); - LSPInformation? lspInformation = await fetchLspInfo(lspId); - - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - int channelFeesMsat = (amountMsat * lspInformation.channelFeePermyriad / 10000 / 1000 * 1000); - return max(channelFeesMsat, lspInformation.channelMinimumFeeMsat); -} -``` -
- -
Python
-
- -```python -def calculate_fees_for_amount(amount_msats): - # We need to open channel so we are calculating the fees for the LSP. - lsp_id = sdk_services.lsp_id() - lsp_info = sdk_services.fetch_lsp_info() - - # We calculate the dynamic fees in millisatoshis rounded to satoshis. - channel_dynamic_fee = amount_msats * lsp_info.channel_minimum_fee_msat * lsp_info.channel_fee_permyriad / 10000 // 10000 * 10000 - - fee_msat = max(lsp_info.channel_minimum_fee_msat, channel_dynamic_fee) - - return fee_msat -``` -
- -
Go
-
- -```go -func calculateFeesForAmount(amountMsats uint64) uint64 { - lspId, err := sdkServices.LspId() - if err != nil { - // Handle error - } - - lspInfo, err := sdkServices.FetchLspInfo(*lspId) - if err != nil { - // Handle error - } - - // We calculate the dynamic fees in millisatoshis rounded to satoshis - channelDynamicFeeMsats := amountMsats * uint64(lspInfo.ChannelFeePermyriad) / 10000 / 1000 * 1000 - feeMsats := uint64(lspInfo.ChannelMinimumFeeMsat) - - if channelDynamicFeeMsats >= feeMsats { - feeMsats = channelDynamicFeeMsats - } - - return feeMsats -} -``` -
- -
C#
-
- -```cs -ulong calculateFeesForAmount(ulong amountMsats) -{ - try - { - var id = sdk.LspId(); - var lspInfo = sdk.FetchLspInfo(id); - - // We calculate the dynamic fees in millisatoshis rounded to satoshis. - var channelDynamicFeeMsat = amountMsats * (ulong)lspInfo.channelFeePermyriad / 10000 / 1000 * 1000; - var feeMsat = Math.Max((ulong)lspInfo.channelMinimumFeeMsat, channelDynamicFeeMsat); - - return feeMsat; - } - catch (Exception) - { - // Handle error - } - - return 0; -} +// TODO add example for openChannelFee ```
-[^1]: For more details on these fees, see [Channel Opening Fees](connecting_lsp.md#channel-opening-fees) \ No newline at end of file + + + +[^1]: For more details on these fees, see [Channel Opening Fees](connecting_lsp.md#channel-opening-fees) diff --git a/src/guide/send_onchain.md b/src/guide/send_onchain.md index 5170c5c..563f7e5 100644 --- a/src/guide/send_onchain.md +++ b/src/guide/send_onchain.md @@ -66,7 +66,11 @@ try { ```dart try { - ReverseSwapPairInfo currentFees = await fetchReverseSwapFees(ReverseSwapFeesRequest(50000)); + ReverseSwapPairInfo currentFees = await fetchReverseSwapFees( + req: ReverseSwapFeesRequest( + sendAmountSat: 50000, + ), + ); print("Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}"); } catch (error) { @@ -80,10 +84,9 @@ try { ```python try: - current_fees = sdk_services.fetch_reverse_swap_fees( - breez_sdk.ReverseSwapFeesRequest( - send_amount_sat=50000)) - print("Total estimated fees for reverse swap:", current_fees.total_estimated_fees) + current_fees = sdk_services.fetch_reverse_swap_fees( + breez_sdk.ReverseSwapFeesRequest(send_amount_sat=50000)) + print("Total estimated fees for reverseswap:", current_fees.total_estimated_fees) except Exception as error: # Handle error ```