From 846e998b89d3c1bba7e9b40fdc38f4e8aca7b2f2 Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Fri, 16 Feb 2024 11:02:16 +0100 Subject: [PATCH] add expiry to quotes, closes #385 --- cashu/core/base.py | 2 ++ cashu/lightning/fake.py | 3 +-- cashu/mint/ledger.py | 2 ++ cashu/mint/router.py | 1 + tests/test_mint_api.py | 3 +++ 5 files changed, 9 insertions(+), 2 deletions(-) diff --git a/cashu/core/base.py b/cashu/core/base.py index 7a9c29e0..4469346b 100644 --- a/cashu/core/base.py +++ b/cashu/core/base.py @@ -230,6 +230,7 @@ class MeltQuote(BaseModel): paid_time: Union[int, None] = None fee_paid: int = 0 proof: str = "" + expiry: int = 0 @classmethod def from_row(cls, row: Row): @@ -417,6 +418,7 @@ class PostMeltQuoteResponse(BaseModel): amount: int # input amount fee_reserve: int # input fee reserve paid: bool # whether the request has been paid + expiry: int # expiry of the quote # ------- API: MELT ------- diff --git a/cashu/lightning/fake.py b/cashu/lightning/fake.py index dc6a6d91..8da840ee 100644 --- a/cashu/lightning/fake.py +++ b/cashu/lightning/fake.py @@ -66,8 +66,7 @@ async def create_invoice( else: tags.add(TagChar.description, memo or "") - if expiry: - tags.add(TagChar.expire_time, expiry) + tags.add(TagChar.expire_time, expiry or 3600) if payment_secret: secret = payment_secret.hex() diff --git a/cashu/mint/ledger.py b/cashu/mint/ledger.py index 8d0be64b..2f5017cb 100644 --- a/cashu/mint/ledger.py +++ b/cashu/mint/ledger.py @@ -507,6 +507,7 @@ async def melt_quote( paid=False, fee_reserve=payment_quote.fee.to(unit).amount, created_time=int(time.time()), + expiry=invoice_obj.expiry or 0, ) await self.crud.store_melt_quote(quote=quote, db=self.db) return PostMeltQuoteResponse( @@ -514,6 +515,7 @@ async def melt_quote( amount=quote.amount, fee_reserve=quote.fee_reserve, paid=quote.paid, + expiry=quote.expiry, ) async def get_melt_quote(self, quote_id: str) -> MeltQuote: diff --git a/cashu/mint/router.py b/cashu/mint/router.py index 02289bcc..d4deb948 100644 --- a/cashu/mint/router.py +++ b/cashu/mint/router.py @@ -262,6 +262,7 @@ async def melt_quote(quote: str) -> PostMeltQuoteResponse: amount=melt_quote.amount, fee_reserve=melt_quote.fee_reserve, paid=melt_quote.paid, + expiry=melt_quote.expiry, ) logger.trace(f"< GET /v1/melt/quote/bolt11/{quote}") return resp diff --git a/tests/test_mint_api.py b/tests/test_mint_api.py index d46dd3e7..22d8e30c 100644 --- a/tests/test_mint_api.py +++ b/tests/test_mint_api.py @@ -182,6 +182,7 @@ async def test_mint_quote(ledger: Ledger): assert result["request"] invoice = bolt11.decode(result["request"]) assert invoice.amount_msat == 100 * 1000 + assert result["expiry"] == invoice.expiry # get mint quote again from api response = httpx.get( @@ -243,6 +244,8 @@ async def test_melt_quote_internal(ledger: Ledger, wallet: Wallet): assert result["amount"] == 64 # TODO: internal invoice, fee should be 0 assert result["fee_reserve"] == 0 + invoice_obj = bolt11.decode(request) + assert result["expiry"] == invoice_obj.expiry # get melt quote again from api response = httpx.get(