This commit is contained in:
callebtc
2022-09-17 01:54:05 +03:00
parent 3591a6fe71
commit 5338c642cb
5 changed files with 23 additions and 8 deletions

View File

@@ -13,7 +13,10 @@ MINT_SERVER_HOST=127.0.0.1
MINT_SERVER_PORT=3338 MINT_SERVER_PORT=3338
LIGHTNING=TRUE LIGHTNING=TRUE
LIGHTNING_FEE=1.01 # fee to reserve in percent of the amount
LIGHTNING_FEE_PERCENT=1.0
# minimum fee to reserve
LIGHTNING_RESERVE_FEE_MIN=4000
LNBITS_ENDPOINT=https://legend.lnbits.com LNBITS_ENDPOINT=https://legend.lnbits.com
LNBITS_KEY=yourkeyasdasdasd LNBITS_KEY=yourkeyasdasdasd

View File

@@ -1,5 +1,6 @@
import asyncio import asyncio
from functools import partial, wraps from functools import partial, wraps
from core.settings import LIGHTNING_FEE_PERCENT, LIGHTNING_RESERVE_FEE_MIN
def async_wrap(func): def async_wrap(func):
@@ -24,3 +25,10 @@ def async_unwrap(to_await):
coroutine = run_and_capture_result() coroutine = run_and_capture_result()
loop.run_until_complete(coroutine) loop.run_until_complete(coroutine)
return async_response[0] return async_response[0]
def fee_reserve(amount_msat: int) -> int:
"""Function for calculating the Lightning fee reserve"""
return max(
int(LIGHTNING_RESERVE_FEE_MIN), int(amount_msat * LIGHTNING_FEE_PERCENT / 100.0)
)

View File

@@ -5,8 +5,9 @@ env.read_env()
DEBUG = env.bool("DEBUG", default=False) DEBUG = env.bool("DEBUG", default=False)
LIGHTNING = env.bool("LIGHTNING", default=True) LIGHTNING = env.bool("LIGHTNING", default=True)
LIGHTNING_FEE = env.float("LIGHTNING_FEE", default=1.01) LIGHTNING_FEE_PERCENT = env.float("LIGHTNING_FEE_PERCENT", default=1.0)
assert LIGHTNING_FEE >= 1.0, "LIGHTNING_FEE must be at least 1.0" assert LIGHTNING_FEE_PERCENT >= 0, "LIGHTNING_FEE_PERCENT must be at least 0"
LIGHTNING_RESERVE_FEE_MIN = env.float("LIGHTNING_RESERVE_FEE_MIN", default=4000)
MINT_PRIVATE_KEY = env.str("MINT_PRIVATE_KEY") MINT_PRIVATE_KEY = env.str("MINT_PRIVATE_KEY")

View File

@@ -13,7 +13,9 @@ from core.base import Proof, BlindedMessage, BlindedSignature, BasePoint
import core.b_dhke as b_dhke import core.b_dhke as b_dhke
from core.base import Invoice from core.base import Invoice
from core.db import Database from core.db import Database
from core.settings import MAX_ORDER, LIGHTNING, LIGHTNING_FEE from core.settings import MAX_ORDER, LIGHTNING
from core.helpers import fee_reserve
from core.split import amount_split from core.split import amount_split
from lightning import WALLET from lightning import WALLET
from mint.crud import ( from mint.crud import (
@@ -161,7 +163,7 @@ class Ledger:
if error: if error:
raise Exception(f"Lightning wallet not responding: {error}") raise Exception(f"Lightning wallet not responding: {error}")
ok, checking_id, fee_msat, preimage, error_message = await WALLET.pay_invoice( ok, checking_id, fee_msat, preimage, error_message = await WALLET.pay_invoice(
invoice, fee_limit_msat=amount * (1 - LIGHTNING_FEE) invoice, fee_limit_msat=fee_reserve(amount * 1000)
) )
return ok, preimage return ok, preimage
@@ -213,7 +215,7 @@ class Ledger:
# if not LIGHTNING: # if not LIGHTNING:
total = sum([p["amount"] for p in proofs]) total = sum([p["amount"] for p in proofs])
# check that lightning fees are included # check that lightning fees are included
assert math.ceil(total * LIGHTNING_FEE) >= amount, Exception( assert total + fee_reserve(amount * 1000) >= amount, Exception(
"provided proofs not enough for Lightning payment." "provided proofs not enough for Lightning payment."
) )

View File

@@ -9,9 +9,10 @@ from functools import wraps
import click import click
from bech32 import bech32_decode, bech32_encode, convertbits from bech32 import bech32_decode, bech32_encode, convertbits
from core.settings import MINT_URL, LIGHTNING, LIGHTNING_FEE from core.settings import MINT_URL, LIGHTNING
from core.migrations import migrate_databases from core.migrations import migrate_databases
from core.base import Proof from core.base import Proof
from core.helpers import fee_reserve
import core.bolt11 as bolt11 import core.bolt11 as bolt11
from core.bolt11 import Invoice from core.bolt11 import Invoice
from wallet.wallet import Wallet as Wallet from wallet.wallet import Wallet as Wallet
@@ -142,7 +143,7 @@ async def pay(ctx, invoice: str):
wallet.status() wallet.status()
decoded_invoice: Invoice = bolt11.decode(invoice) decoded_invoice: Invoice = bolt11.decode(invoice)
amount = math.ceil( amount = math.ceil(
decoded_invoice.amount_msat / 1000 * LIGHTNING_FEE (decoded_invoice.amount_msat + fee_reserve(decoded_invoice.amount_msat)) / 1000
) # 1% fee for Lightning ) # 1% fee for Lightning
print( print(
f"Paying Lightning invoice of {decoded_invoice.amount_msat // 1000} sat ({amount} sat incl. fees)" f"Paying Lightning invoice of {decoded_invoice.amount_msat // 1000} sat ({amount} sat incl. fees)"