mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-18 06:14:21 +01:00
update fee example
This commit is contained in:
@@ -450,13 +450,7 @@ To calculate the fees for a channel being opened by the LSP:
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```rust,ignore
|
```rust,ignore
|
||||||
async fn calculate_channel_opening_fee(amount_msat: u64) -> Result<u64> {
|
// TODO add example for open_channel_fee
|
||||||
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),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -464,12 +458,12 @@ async fn calculate_channel_opening_fee(amount_msat: u64) -> Result<u64> {
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
|
let amountMsat = <amount msat>
|
||||||
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats: amountMsats)
|
do {
|
||||||
if channelOpeningFeeNeeded {
|
let channelFees = try sdk.openChannelFee(
|
||||||
return calculateFeesForAmount(amountMsats: amountMsats)
|
req: OpenChannelFeeRequest(amountMsat: amountMsat))
|
||||||
}
|
} catch {
|
||||||
return nil
|
// Handle error
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
@@ -477,14 +471,8 @@ func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
|
|||||||
<div slot="title">Android</div>
|
<div slot="title">Android</div>
|
||||||
<section>
|
<section>
|
||||||
|
|
||||||
```kotlin,ignore
|
```kotlin, ignore
|
||||||
fun calculateChannelOpeningFee(amountMsats: Long): Long? {
|
// TODO add example for openChannelFee
|
||||||
val channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats)
|
|
||||||
if (channelOpeningFeeNeeded) {
|
|
||||||
return calculateFeesForAmount(amountMsats)
|
|
||||||
}
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -492,13 +480,7 @@ fun calculateChannelOpeningFee(amountMsats: Long): Long? {
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
const calculateChannelOpeningFee = async (amountMsats: number): number => {
|
// TODO add example for openChannelFee
|
||||||
const channelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsats)
|
|
||||||
if (channelOpeningFeeNeeded) {
|
|
||||||
return calculateFeesForAmount(amountMsats)
|
|
||||||
}
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -506,9 +488,14 @@ const calculateChannelOpeningFee = async (amountMsats: number): number => {
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
int calculateChannelOpeningFee(int amountMsat) async {
|
int amountMsats = <amount msats>
|
||||||
bool isChannelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsat);
|
try {
|
||||||
return isChannelOpeningFeeNeeded ? calculateFeesForAmount(amountMsat) : 0;
|
int channelFees = openChannelFee(OpenChannelFeeRequest(
|
||||||
|
amountMsats: amountMsats,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
} catch {
|
||||||
|
// Handle error
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
@@ -517,13 +504,13 @@ int calculateChannelOpeningFee(int amountMsat) async {
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def calculate_channel_opening_fees(amount_msats):
|
amount_msat = <amount msats>
|
||||||
is_channel_opening_fee_needed = is_channel_opening_fee_needed()
|
try:
|
||||||
|
channel_fees = sdk_services.open_channel_fee(
|
||||||
if is_channel_opening_fee_needed:
|
breez_sdk.OpenChannelFeeRequest(
|
||||||
return calculate_fees_for_amount(amount_msats)
|
amount_msat=amount_msat))
|
||||||
else:
|
except Exception as error:
|
||||||
return None
|
# Handle error
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -531,13 +518,8 @@ def calculate_channel_opening_fees(amount_msats):
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
|
amountMsat := <amount msat>
|
||||||
isChannelOpeningFeeNeeded := isChannelOpeningFeeNeeded(amountMsats)
|
channelFees, err := sdkServices.OpenChannelFee(breez_sdk.OpenChannelFeeRequest(amountMsat))
|
||||||
if !isChannelOpeningFeeNeeded {
|
|
||||||
return 0, fmt.Errorf("Channel not needed")
|
|
||||||
}
|
|
||||||
return calculateFeesForAmount(amountMsats), nil
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -545,324 +527,12 @@ func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
|
|||||||
<section>
|
<section>
|
||||||
|
|
||||||
```cs
|
```cs
|
||||||
ulong calculateChannelOpeningFee(ulong amountMsats)
|
// TODO add example for openChannelFee
|
||||||
{
|
|
||||||
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats);
|
|
||||||
if (channelOpeningFeeNeeded)
|
|
||||||
{
|
|
||||||
return calculateFeesForAmount(amountMsats);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
</custom-tabs>
|
</custom-tabs>
|
||||||
|
|
||||||
How to detect if open channel fees are needed:
|
|
||||||
|
|
||||||
<custom-tabs category="lang">
|
|
||||||
<div slot="title">Rust</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```rust,ignore
|
|
||||||
fn is_channel_opening_fee_needed(amount_msats: u64) -> Result<bool> {
|
|
||||||
let node_info = sdk.node_info()?.ok_or(anyhow!("No node info found"))?;
|
|
||||||
Ok(node_info.inbound_liquidity_msats <= amount_msats)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Swift</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Android</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">React Native</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```typescript
|
|
||||||
const isChannelOpeningFeeNeeded = async (amountMsats: number): boolean => {
|
|
||||||
try {
|
|
||||||
const nodeInfo = await nodeInfo()
|
|
||||||
return nodeInfo.inboundLiquidityMsats <= amountMsats
|
|
||||||
} catch (error) {
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Dart</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```dart
|
|
||||||
// Assumes nodeState isn't empty
|
|
||||||
bool isChannelOpeningFeeNeeded(int amountMsat) async {
|
|
||||||
NodeState? nodeState = await getNodeState();
|
|
||||||
return amountMsat >= nodeState.inboundLiquidityMsats;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Python</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```python
|
|
||||||
def is_channel_opening_fee_needed(amount_msats):
|
|
||||||
return sdk_services.node_info().inbound_liquidity_msats <= amount_msats
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Go</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```go
|
|
||||||
func isChannelOpeningFeeNeeded(amountMsats uint64) bool {
|
|
||||||
nodeInfo, err := sdkServices.NodeInfo()
|
|
||||||
if err != nil {
|
|
||||||
// Handle error
|
|
||||||
}
|
|
||||||
return nodeInfo.InboundLiquidityMsats <= amountMsats
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">C#</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```cs
|
|
||||||
bool isChannelOpeningFeeNeeded(ulong amountMsats)
|
|
||||||
{
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var nodeInfo = sdk.NodeInfo();
|
|
||||||
return nodeInfo.inboundLiquidityMsats <= amountMsats;
|
|
||||||
}
|
|
||||||
catch (Exception)
|
|
||||||
{
|
|
||||||
// handle error
|
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
</custom-tabs>
|
|
||||||
|
|
||||||
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:
|
|
||||||
|
|
||||||
<custom-tabs category="lang">
|
|
||||||
<div slot="title">Rust</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```rust,ignore
|
|
||||||
async fn calculate_fees_for_amount(amount_msat: u64) -> Result<u64> {
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Swift</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Android</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">React Native</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Dart</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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);
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Python</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">Go</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
|
|
||||||
<div slot="title">C#</div>
|
|
||||||
<section>
|
|
||||||
|
|
||||||
```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;
|
|
||||||
}
|
|
||||||
```
|
|
||||||
</section>
|
|
||||||
</custom-tabs>
|
</custom-tabs>
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -66,7 +66,11 @@ try {
|
|||||||
|
|
||||||
```dart
|
```dart
|
||||||
try {
|
try {
|
||||||
ReverseSwapPairInfo currentFees = await fetchReverseSwapFees(ReverseSwapFeesRequest(50000));
|
ReverseSwapPairInfo currentFees = await fetchReverseSwapFees(
|
||||||
|
req: ReverseSwapFeesRequest(
|
||||||
|
sendAmountSat: 50000,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
print("Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}");
|
print("Total estimated fees for reverse swap: ${currentFees.totalEstimatedFees}");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@@ -80,10 +84,9 @@ try {
|
|||||||
|
|
||||||
```python
|
```python
|
||||||
try:
|
try:
|
||||||
current_fees = sdk_services.fetch_reverse_swap_fees(
|
current_fees = sdk_services.fetch_reverse_swap_fees(
|
||||||
breez_sdk.ReverseSwapFeesRequest(
|
breez_sdk.ReverseSwapFeesRequest(send_amount_sat=50000))
|
||||||
send_amount_sat=50000))
|
print("Total estimated fees for reverseswap:", current_fees.total_estimated_fees)
|
||||||
print("Total estimated fees for reverse swap:", current_fees.total_estimated_fees)
|
|
||||||
except Exception as error:
|
except Exception as error:
|
||||||
# Handle error
|
# Handle error
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user