mirror of
https://github.com/aljazceru/breez-sdk-docs.git
synced 2025-12-18 22:34:19 +01:00
Merge remote-tracking branch 'origin/fee-examples'
This commit is contained in:
@@ -346,10 +346,10 @@ When the amount to be received exceeds the inbound liquidity of the node, a new
|
|||||||
To calculate the fees for a channel being opened by the LSP:
|
To calculate the fees for a channel being opened by the LSP:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func calculateChannelOpeningFee(amountStats: Int64) -> Int64? {
|
func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
|
||||||
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountStats: amountStats)
|
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats: amountMsats)
|
||||||
if channelOpeningFeeNeeded {
|
if channelOpeningFeeNeeded {
|
||||||
return calculateFeesForAmount(amountStats: amountStats)
|
return calculateFeesForAmount(amountMsats: amountMsats)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -357,14 +357,12 @@ func calculateChannelOpeningFee(amountStats: Int64) -> Int64? {
|
|||||||
|
|
||||||
How to detect if open channel fees are needed:
|
How to detect if open channel fees are needed:
|
||||||
```swift
|
```swift
|
||||||
func isChannelOpeningFeeNeeded(amountStats: Int64) -> Bool {
|
func isChannelOpeningFeeNeeded(amountMsats: Int64) -> Bool {
|
||||||
do {
|
do {
|
||||||
let nodeInfo = try sdk.nodeInfo()
|
let nodeInfo = try sdk.nodeInfo()
|
||||||
|
|
||||||
if var liqudity = nodeInfo?.inboundLiquidityMsats{
|
if let inboundLiquidityMsats = nodeInfo?.inboundLiquidityMsats {
|
||||||
liqudity /= 1000
|
return inboundLiquidityMsats <= amountMsats
|
||||||
print(liqudity)
|
|
||||||
return amountStats >= liqudity
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch {
|
} catch {
|
||||||
@@ -379,25 +377,22 @@ LSP fees are calculated in two ways, either by a minimum fee set by the LSP or b
|
|||||||
This information can be retrieved for each LSP and then calculated:
|
This information can be retrieved for each LSP and then calculated:
|
||||||
|
|
||||||
```swift
|
```swift
|
||||||
func calculateFeesForAmount(amountStats: Int64) -> Int64? {
|
func calculateFeesForAmount(amountMsats: Int64) -> Int64? {
|
||||||
do {
|
do {
|
||||||
if let lspId = try sdk.lspId() {
|
if let lspId = try sdk.lspId() {
|
||||||
let lspInfo = try sdk.fetchLspInfo(lspId: lspId)
|
let lspInfo = try sdk.fetchLspInfo(lspId: lspId)
|
||||||
|
|
||||||
// setupFee is the proportional fee charged based on the amount.
|
// We calculate the dynamic fees in millisatoshis rounded to satoshis.
|
||||||
let setupFee = lspInfo!.channelFeePermyriad / 100
|
let channelDynamicFeeMsat = amountMsats * lspInfo!.channelFeePermyriad / 10_000 / 1000 * 1000
|
||||||
// minFee is the minimum fee required by the LSP to open a channel.
|
let feeSat = max(lspInfo!.channelMinimumFeeMsat,channelDynamicFeeMsat)
|
||||||
let minFee = lspInfo!.channelMinimumFeeMsat / 1000
|
|
||||||
|
|
||||||
// A setup fee of {setupFee}% with a minimum of {minFee} will be applied for sending more than {liquidity}.
|
return feeSat
|
||||||
let channelFeesMsat = amountStats * setupFee / 1000
|
|
||||||
return max(channelFeesMsat, minFee)
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// Handle error
|
|
||||||
}
|
}
|
||||||
return nil
|
} catch {
|
||||||
|
// Handle error
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
</section>
|
</section>
|
||||||
@@ -452,11 +447,11 @@ When the amount to be received exceeds the inbound liquidity of the node, a new
|
|||||||
To calculate the fees for a channel being opened by the LSP:
|
To calculate the fees for a channel being opened by the LSP:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def calculate_channel_opening_fees(amount_sats):
|
def calculate_channel_opening_fees(amount_msats):
|
||||||
is_channel_opening_fee_needed = is_channel_opening_fee_needed()
|
is_channel_opening_fee_needed = is_channel_opening_fee_needed()
|
||||||
|
|
||||||
if is_channel_opening_fee_needed:
|
if is_channel_opening_fee_needed:
|
||||||
return calculate_fees_for_amount(amount_sats)
|
return calculate_fees_for_amount(amount_msats)
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
```
|
```
|
||||||
@@ -464,9 +459,8 @@ def calculate_channel_opening_fees(amount_sats):
|
|||||||
How to detect if open channel fees are needed:
|
How to detect if open channel fees are needed:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def is_channel_opening_fee_needed(amount_sats):
|
def is_channel_opening_fee_needed(amount_msats):
|
||||||
liqudity = sdk_services.node_info().inbound_liquidity_msats // 1000
|
return sdk_services.node_info().inbound_liquidity_msats <= amount_msats
|
||||||
return amount_sats >= liqudity
|
|
||||||
```
|
```
|
||||||
|
|
||||||
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.
|
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.
|
||||||
@@ -474,21 +468,17 @@ LSP fees are calculated in two ways, either by a minimum fee set by the LSP or b
|
|||||||
This information can be retrieved for each LSP and then calculated:
|
This information can be retrieved for each LSP and then calculated:
|
||||||
|
|
||||||
```python
|
```python
|
||||||
def calculate_fees_for_amount(amount_sats):
|
def calculate_fees_for_amount(amount_msats):
|
||||||
# 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.
|
||||||
lsp_id = sdk_services.lsp_id()
|
lsp_id = sdk_services.lsp_id()
|
||||||
lsp_info = sdk_services.fetch_lsp_info()
|
lsp_info = sdk_services.fetch_lsp_info()
|
||||||
|
|
||||||
# setup_fee is the proportional fee charged based on the amount.
|
# We calculate the dynamic fees in millisatoshis rounded to satoshis.
|
||||||
setup_fee = lsp_info.channel_fee_permyriad / 100
|
channel_dynamic_fee = amount_msats * lsp_info.channel_minimum_fee_msat * lsp_info.channel_fee_permyriad / 10000 // 10000 * 10000
|
||||||
|
|
||||||
# min_fee is the minimum fee required by the LSP to open a channel.
|
fee_sat = max(lsp_info.channel_minimum_fee_msat, channel_dynamic_fee)
|
||||||
min_fee = lsp_info.channel_minimum_fee_msat // 1000
|
|
||||||
|
|
||||||
# A setup fee of {setup_fee} with a minimum of {min_fee} will be applied for sending more than {liquidity}.
|
return fee_sat
|
||||||
channel_fee_msat = amount_sats * setup_fee // 1000
|
|
||||||
|
|
||||||
return max(channel_fee_msat, min_fee)
|
|
||||||
```
|
```
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
@@ -500,24 +490,23 @@ When the amount to be received exceeds the inbound liquidity of the node, a new
|
|||||||
To calculate the fees for a channel being opened by the LSP:
|
To calculate the fees for a channel being opened by the LSP:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func CalculateChannelOpeningFee(amountSats uint64) (uint64, error) {
|
func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
|
||||||
isChannelOpeningFeeNeeded := isChannelOpeningFeeNeeded(amountSats)
|
isChannelOpeningFeeNeeded := isChannelOpeningFeeNeeded(amountMsats)
|
||||||
if !isChannelOpeningFeeNeeded {
|
if !isChannelOpeningFeeNeeded {
|
||||||
return 0, fmt.Errorf("Channel not needed")
|
return 0, fmt.Errorf("Channel not needed")
|
||||||
}
|
}
|
||||||
return calculateFeesForAmount(amountSats), nil
|
return calculateFeesForAmount(amountMsats), nil
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
How to detect if open channel fees are needed:
|
How to detect if open channel fees are needed:
|
||||||
```go
|
```go
|
||||||
func isChannelOpeningFeeNeeded(amountSats uint64) bool {
|
func isChannelOpeningFeeNeeded(amountMsats uint64) bool {
|
||||||
nodeInfo, err := sdkServices.NodeInfo()
|
nodeInfo, err := sdkServices.NodeInfo()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle error
|
// Handle error
|
||||||
}
|
}
|
||||||
liqudity := nodeInfo.InboundLiquidityMsats / 1000
|
return nodeInfo.InboundLiquidityMsats <= amountMsats
|
||||||
return amountSats >= liqudity
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -526,28 +515,25 @@ LSP fees are calculated in two ways, either by a minimum fee set by the LSP or b
|
|||||||
This information can be retrieved for each LSP and then calculated:
|
This information can be retrieved for each LSP and then calculated:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func calculateFeesForAmount(amountSats uint64) uint64 {
|
func calculateFeesForAmount(amountMsats uint64) uint64 {
|
||||||
// We need to open channel so we are calculating the fees for the LSP
|
|
||||||
lspId, err := sdkServices.LspId()
|
lspId, err := sdkServices.LspId()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle error
|
log.Printf("received error %#v", err)
|
||||||
}
|
}
|
||||||
lspInfo, err := sdkServices.FetchLspInfo(*lspId)
|
lspInfo, err := sdkServices.FetchLspInfo(*lspId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Handle error
|
log.Printf("received error %#v", err)
|
||||||
}
|
}
|
||||||
// setupFee is the proportional fee charged based on the amount
|
// We calculate the dynamic fees in millisatoshis rounded to satoshis
|
||||||
setupFee := lspInfo.ChannelMinimumFeeMsat / 1000
|
channelDynamicFeeMSat := amountMsats * uint64(lspInfo.ChannelFeePermyriad) / 10000 / 1000 * 1000
|
||||||
|
|
||||||
// minFee is the minimum fee required by the LSP to open a channel.
|
var feeSat uint64
|
||||||
minFee := lspInfo.ChannelMinimumFeeMsat / 1000
|
if channelDynamicFeeMSat >= uint64(lspInfo.ChannelMinimumFeeMsat) {
|
||||||
|
feeSat = channelDynamicFeeMSat
|
||||||
// A setup fee of {setupFee}% with a minimum of {minFee} will be applied for sending more than {liquidity}.
|
} else {
|
||||||
channelFeeMSat := amountSats * uint64(setupFee) / 1000
|
feeSat = uint64(lspInfo.ChannelMinimumFeeMsat)
|
||||||
if channelFeeMSat > uint64(minFee) {
|
|
||||||
return channelFeeMSat
|
|
||||||
}
|
}
|
||||||
return uint64(minFee)
|
return uint64(feeSat)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user