mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-19 06:44:19 +01:00
Divide fee calculation logic into parts
This commit is contained in:
@@ -290,12 +290,30 @@ result, err := sdkServices.Refund(refundable.BitcoinAddress, destinationAddress,
|
|||||||
<div slot="title">Dart</div>
|
<div slot="title">Dart</div>
|
||||||
<section>
|
<section>
|
||||||
|
|
||||||
|
To calculate fees, you'll need to detect if channel opening fees are needed first and then calculate the fee for the amount you'll be receiving.
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
int calculateChannelOpeningFee(int amountSats) {
|
int calculateChannelOpeningFee(int amountSats) async {
|
||||||
|
bool isChannelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountSats);
|
||||||
|
return isChannelOpeningFeeNeeded ? calculateFeesForAmount(amountSats) : 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
How to detect if open channel fees are needed.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
bool isChannelOpeningFeeNeeded(int amountSats) async {
|
||||||
NodeState? nodeState = await getNodeState();
|
NodeState? nodeState = await getNodeState();
|
||||||
int liquidity = nodeState.inboundLiquidityMsats ~/ 1000;
|
int liquidity = nodeState.inboundLiquidityMsats ~/ 1000;
|
||||||
// Check if we need to open channel
|
// Check if we need to open channel
|
||||||
if(amountSats >= liquidity) {
|
return amountSats >= liquidity;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
How to calculate the fee for a specific amount.
|
||||||
|
|
||||||
|
```dart
|
||||||
|
int calculateFeesForAmount(int amountSats) async {
|
||||||
// We need to open channel so we are calculating the fees for the LSP
|
// We need to open channel so we are calculating the fees for the LSP
|
||||||
String? lspId = await getLspId();
|
String? lspId = await getLspId();
|
||||||
LSPInformation? lspInformation = await fetchLspInfo(lspId!);
|
LSPInformation? lspInformation = await fetchLspInfo(lspId!);
|
||||||
@@ -309,7 +327,6 @@ int calculateChannelOpeningFee(int amountSats) {
|
|||||||
int channelFeesMsat = (amountSats * setupFee ~/ 100);
|
int channelFeesMsat = (amountSats * setupFee ~/ 100);
|
||||||
// If the proportional fee is smaller than minimum fees, minimum fees is selected.
|
// If the proportional fee is smaller than minimum fees, minimum fees is selected.
|
||||||
return max(channelFeesMsat, minFee);
|
return max(channelFeesMsat, minFee);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|||||||
Reference in New Issue
Block a user