Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mint: add expiry to quotes, closes #385 #429

Merged
merged 3 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions cashu/core/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ class MeltQuote(BaseModel):
paid_time: Union[int, None] = None
fee_paid: int = 0
proof: str = ""
expiry: Optional[int] = None

@classmethod
def from_row(cls, row: Row):
Expand Down Expand Up @@ -269,7 +270,7 @@ class MintQuote(BaseModel):
issued: bool
created_time: Union[int, None] = None
paid_time: Union[int, None] = None
expiry: int = 0
expiry: Optional[int] = None

@classmethod
def from_row(cls, row: Row):
Expand All @@ -295,7 +296,6 @@ def from_row(cls, row: Row):
issued=row["issued"],
created_time=created_time,
paid_time=paid_time,
expiry=0,
)


Expand Down Expand Up @@ -370,7 +370,7 @@ class PostMintQuoteResponse(BaseModel):
quote: str # quote id
request: str # input payment request
paid: bool # whether the request has been paid
expiry: int # expiry of the quote
expiry: Optional[int] # expiry of the quote


# ------- API: MINT -------
Expand Down Expand Up @@ -417,6 +417,7 @@ class PostMeltQuoteResponse(BaseModel):
amount: int # input amount
fee_reserve: int # input fee reserve
paid: bool # whether the request has been paid
expiry: Optional[int] # expiry of the quote


# ------- API: MELT -------
Expand Down
3 changes: 1 addition & 2 deletions cashu/lightning/fake.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 3 additions & 1 deletion cashu/mint/ledger.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ async def mint_quote(self, quote_request: PostMintQuoteRequest) -> MintQuote:
issued=False,
paid=False,
created_time=int(time.time()),
expiry=invoice_obj.expiry or 0,
expiry=invoice_obj.expiry,
)
await self.crud.store_mint_quote(
quote=quote,
Expand Down Expand Up @@ -507,13 +507,15 @@ async def melt_quote(
paid=False,
fee_reserve=payment_quote.fee.to(unit).amount,
created_time=int(time.time()),
expiry=invoice_obj.expiry,
)
await self.crud.store_melt_quote(quote=quote, db=self.db)
return PostMeltQuoteResponse(
quote=quote.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:
Expand Down
1 change: 1 addition & 0 deletions cashu/mint/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,7 @@ async def melt_quote(self, payment_request: str) -> PostMeltQuoteResponse:
amount=invoice_obj.amount_msat // 1000,
fee_reserve=ret.fee or 0,
paid=False,
expiry=invoice_obj.expiry,
)
# END backwards compatibility < 0.15.0
self.raise_on_error_request(resp)
Expand Down
3 changes: 3 additions & 0 deletions tests/test_mint_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down
Loading