update fee example

This commit is contained in:
ruben beck
2023-09-04 13:15:55 +02:00
parent 48fe5f4ec3
commit 06046dbecd
2 changed files with 41 additions and 368 deletions

View File

@@ -450,13 +450,7 @@ To calculate the fees for a channel being opened by the LSP:
<section>
```rust,ignore
async fn calculate_channel_opening_fee(amount_msat: u64) -> Result<u64> {
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),
}
}
// TODO add example for open_channel_fee
```
</section>
@@ -464,12 +458,12 @@ async fn calculate_channel_opening_fee(amount_msat: u64) -> Result<u64> {
<section>
```swift
func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats: amountMsats)
if channelOpeningFeeNeeded {
return calculateFeesForAmount(amountMsats: amountMsats)
}
return nil
let amountMsat = <amount msat>
do {
let channelFees = try sdk.openChannelFee(
req: OpenChannelFeeRequest(amountMsat: amountMsat))
} catch {
// Handle error
}
```
</section>
@@ -477,14 +471,8 @@ func calculateChannelOpeningFee(amountMsats: Int64) -> Int64? {
<div slot="title">Android</div>
<section>
```kotlin,ignore
fun calculateChannelOpeningFee(amountMsats: Long): Long? {
val channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats)
if (channelOpeningFeeNeeded) {
return calculateFeesForAmount(amountMsats)
}
return null
}
```kotlin, ignore
// TODO add example for openChannelFee
```
</section>
@@ -492,13 +480,7 @@ fun calculateChannelOpeningFee(amountMsats: Long): Long? {
<section>
```typescript
const calculateChannelOpeningFee = async (amountMsats: number): number => {
const channelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsats)
if (channelOpeningFeeNeeded) {
return calculateFeesForAmount(amountMsats)
}
return 0
}
// TODO add example for openChannelFee
```
</section>
@@ -506,10 +488,15 @@ const calculateChannelOpeningFee = async (amountMsats: number): number => {
<section>
```dart
int calculateChannelOpeningFee(int amountMsat) async {
bool isChannelOpeningFeeNeeded = await isChannelOpeningFeeNeeded(amountMsat);
return isChannelOpeningFeeNeeded ? calculateFeesForAmount(amountMsat) : 0;
}
int amountMsats = <amount msats>
try {
int channelFees = openChannelFee(OpenChannelFeeRequest(
amountMsats: amountMsats,
),
);
} catch {
// Handle error
}
```
</section>
@@ -517,13 +504,13 @@ int calculateChannelOpeningFee(int amountMsat) async {
<section>
```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 = <amount msats>
try:
channel_fees = sdk_services.open_channel_fee(
breez_sdk.OpenChannelFeeRequest(
amount_msat=amount_msat))
except Exception as error:
# Handle error
```
</section>
@@ -531,13 +518,8 @@ def calculate_channel_opening_fees(amount_msats):
<section>
```go
func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
isChannelOpeningFeeNeeded := isChannelOpeningFeeNeeded(amountMsats)
if !isChannelOpeningFeeNeeded {
return 0, fmt.Errorf("Channel not needed")
}
return calculateFeesForAmount(amountMsats), nil
}
amountMsat := <amount msat>
channelFees, err := sdkServices.OpenChannelFee(breez_sdk.OpenChannelFeeRequest(amountMsat))
```
</section>
@@ -545,325 +527,13 @@ func CalculateChannelOpeningFee(amountMsats uint64) (uint64, error) {
<section>
```cs
ulong calculateChannelOpeningFee(ulong amountMsats)
{
var channelOpeningFeeNeeded = isChannelOpeningFeeNeeded(amountMsats);
if (channelOpeningFeeNeeded)
{
return calculateFeesForAmount(amountMsats);
}
return 0;
}
```
</section>
</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;
}
// TODO add example for openChannelFee
```
</section>
</custom-tabs>
[^1]: For more details on these fees, see [Channel Opening Fees](connecting_lsp.md#channel-opening-fees)
</custom-tabs>
[^1]: For more details on these fees, see [Channel Opening Fees](connecting_lsp.md#channel-opening-fees)

View File

@@ -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
```