diff --git a/.env.example b/.env.example index 16ecad4..b10432b 100644 --- a/.env.example +++ b/.env.example @@ -13,7 +13,10 @@ MINT_SERVER_HOST=127.0.0.1 MINT_SERVER_PORT=3338 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_KEY=yourkeyasdasdasd \ No newline at end of file diff --git a/core/helpers.py b/core/helpers.py index 5969d7b..f6afeb5 100644 --- a/core/helpers.py +++ b/core/helpers.py @@ -1,5 +1,6 @@ import asyncio from functools import partial, wraps +from core.settings import LIGHTNING_FEE_PERCENT, LIGHTNING_RESERVE_FEE_MIN def async_wrap(func): @@ -24,3 +25,10 @@ def async_unwrap(to_await): coroutine = run_and_capture_result() loop.run_until_complete(coroutine) 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) + ) diff --git a/core/settings.py b/core/settings.py index f56f702..2e86b09 100644 --- a/core/settings.py +++ b/core/settings.py @@ -5,8 +5,9 @@ env.read_env() DEBUG = env.bool("DEBUG", default=False) LIGHTNING = env.bool("LIGHTNING", default=True) -LIGHTNING_FEE = env.float("LIGHTNING_FEE", default=1.01) -assert LIGHTNING_FEE >= 1.0, "LIGHTNING_FEE must be at least 1.0" +LIGHTNING_FEE_PERCENT = env.float("LIGHTNING_FEE_PERCENT", default=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") diff --git a/mint/ledger.py b/mint/ledger.py index c9aca62..abcd9f3 100644 --- a/mint/ledger.py +++ b/mint/ledger.py @@ -13,7 +13,9 @@ from core.base import Proof, BlindedMessage, BlindedSignature, BasePoint import core.b_dhke as b_dhke from core.base import Invoice 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 lightning import WALLET from mint.crud import ( @@ -161,7 +163,7 @@ class Ledger: if error: raise Exception(f"Lightning wallet not responding: {error}") 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 @@ -213,7 +215,7 @@ class Ledger: # if not LIGHTNING: total = sum([p["amount"] for p in proofs]) # 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." ) diff --git a/wallet/cashu.py b/wallet/cashu.py index e8f2667..af861dc 100755 --- a/wallet/cashu.py +++ b/wallet/cashu.py @@ -9,9 +9,10 @@ from functools import wraps import click 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.base import Proof +from core.helpers import fee_reserve import core.bolt11 as bolt11 from core.bolt11 import Invoice from wallet.wallet import Wallet as Wallet @@ -142,7 +143,7 @@ async def pay(ctx, invoice: str): wallet.status() decoded_invoice: Invoice = bolt11.decode(invoice) 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 print( f"Paying Lightning invoice of {decoded_invoice.amount_msat // 1000} sat ({amount} sat incl. fees)"