mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-18 22:34:19 +01:00
add fee calc example for swift
This commit is contained in:
@@ -287,6 +287,70 @@ result, err := sdkServices.Refund(refundable.BitcoinAddress, destinationAddress,
|
||||
# Calculating fees
|
||||
|
||||
<custom-tabs category="lang">
|
||||
<div slot="title">Swift</div>
|
||||
<section>
|
||||
|
||||
When the amount to be received exceeds the inbound liquidity of the node, a new channel will be opened by the LSP in order for the node to receive it. This can checked by retrieving the NodeState from the SDK and comparing the inbound liquidity to the amount to be received. If the amount is greater or equal to the inbound liquidity, a new channel opening is required.
|
||||
|
||||
To calculate the fees for a channel being opened by the LSP:
|
||||
|
||||
```swift
|
||||
func calculateChannelOpeningFee(amountStats: Int64) -> Int64? {
|
||||
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountStats: amountStats)
|
||||
if channelOpeningFeeNeeded {
|
||||
return calculateFeesForAmount(amountStats: amountStats)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
How to detect if open channel fees are needed.
|
||||
```swift
|
||||
func isChannelOpeningFeeNeeded(amountStats: Int64) -> Bool {
|
||||
do {
|
||||
let nodeInfo = try sdk.nodeInfo()
|
||||
|
||||
if var liqudity = nodeInfo?.inboundLiquidityMsats{
|
||||
liqudity /= 1000
|
||||
print(liqudity)
|
||||
return amountStats >= liqudity
|
||||
}
|
||||
|
||||
} catch {
|
||||
// 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:
|
||||
|
||||
```swift
|
||||
func calculateFeesForAmount(amountStats: Int64) -> Int64? {
|
||||
do {
|
||||
if let lspId = try sdk.lspId() {
|
||||
let lspInfo = try sdk.fetchLspInfo(lspId: lspId)
|
||||
|
||||
// setupFee is the proportional fee charged based on the amount
|
||||
let setupFee = lspInfo!.channelFeePermyriad / 100
|
||||
// minFee is the minimum fee required by the LSP to open a channels
|
||||
let minFee = lspInfo!.channelMinimumFeeMsat / 1000
|
||||
|
||||
// A setup fee of {setupFee}% with a minimum of {minFee} will be applied for sending more than {liquidity}.
|
||||
let channelFeesMsat = amountStats * setupFee / 1000
|
||||
return max(channelFeesMsat, minFee)
|
||||
}
|
||||
} catch {
|
||||
// Handle error
|
||||
}
|
||||
return nil
|
||||
}
|
||||
```
|
||||
|
||||
</section>
|
||||
|
||||
<div slot="title">Dart</div>
|
||||
<section>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user