From ad906df788939ebe7ba1d12428c478cc366a1594 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sun, 18 Feb 2024 12:24:41 +0100 Subject: [PATCH] Mint: blink fix fee estimation (#439) * blink: fix fee esimation * fix line length * fix line length * fix line length * remove noqa --- cashu/lightning/blink.py | 2 +- cashu/mint/ledger.py | 2 ++ tests/test_mint_lightning_blink.py | 12 ++++++++++-- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cashu/lightning/blink.py b/cashu/lightning/blink.py index d4f11e8..24b223d 100644 --- a/cashu/lightning/blink.py +++ b/cashu/lightning/blink.py @@ -327,7 +327,7 @@ class BlinkWallet(LightningBackend): fees_response_msat = int(resp["data"]["lnInvoiceFeeProbe"]["amount"]) * 1000 # we either take fee_msat_response or the BLINK_MAX_FEE_PERCENT, whichever is higher fees_msat = max( - fees_response_msat, math.ceil(amount_msat * BLINK_MAX_FEE_PERCENT) + fees_response_msat, math.ceil(amount_msat / 100 * BLINK_MAX_FEE_PERCENT) ) fees = Amount(unit=Unit.msat, amount=fees_msat) diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 1a97364..3782b0c 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -380,6 +380,7 @@ class Ledger(LedgerVerification, LedgerSpendingConditions): method = Method[quote.method] if not quote.paid: + assert quote.checking_id, "quote has no checking id" logger.trace(f"Lightning: checking invoice {quote.checking_id}") status: PaymentStatus = await self.backends[method][ unit @@ -482,6 +483,7 @@ class Ledger(LedgerVerification, LedgerSpendingConditions): assert mint_quote.method == method.name, "methods do not match" assert not mint_quote.paid, "mint quote already paid" assert not mint_quote.issued, "mint quote already issued" + assert mint_quote.checking_id, "mint quote has no checking id" payment_quote = PaymentQuoteResponse( checking_id=mint_quote.checking_id, amount=Amount(unit, mint_quote.amount), diff --git a/tests/test_mint_lightning_blink.py b/tests/test_mint_lightning_blink.py index 7339fc2..3071c69 100644 --- a/tests/test_mint_lightning_blink.py +++ b/tests/test_mint_lightning_blink.py @@ -49,7 +49,6 @@ async def test_blink_create_invoice(): } } } - respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response)) invoice = await blink.create_invoice(Amount(Unit.sat, 1000)) assert invoice.checking_id == invoice.payment_request @@ -129,9 +128,18 @@ async def test_blink_get_payment_status(): @respx.mock @pytest.mark.asyncio async def test_blink_get_payment_quote(): + # response says 1 sat fees but invoice * 0.5% is 5 sat so we expect 5 sat + mock_response = {"data": {"lnInvoiceFeeProbe": {"amount": 1}}} + respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response)) + quote = await blink.get_payment_quote(payment_request) + assert quote.checking_id == payment_request + assert quote.amount == Amount(Unit.msat, 1000000) # msat + assert quote.fee == Amount(Unit.msat, 5000) # msat + + # response says 10 sat fees but invoice * 0.5% is 5 sat so we expect 10 sat mock_response = {"data": {"lnInvoiceFeeProbe": {"amount": 10}}} respx.post(blink.endpoint).mock(return_value=Response(200, json=mock_response)) quote = await blink.get_payment_quote(payment_request) assert quote.checking_id == payment_request assert quote.amount == Amount(Unit.msat, 1000000) # msat - assert quote.fee == Amount(Unit.msat, 500000) # msat + assert quote.fee == Amount(Unit.msat, 10000) # msat