feat: check paid invoices

This commit is contained in:
Vlad Stan 2022-10-07 11:47:37 +03:00 committed by dni ⚡
parent 163f0b6014
commit 337ccf5459
4 changed files with 26 additions and 14 deletions

View file

@ -87,10 +87,11 @@ class Invoice(BaseModel):
@classmethod @classmethod
def from_row(cls, row: Row): def from_row(cls, row: Row):
return cls( return cls(
amount=int(row[0]), cashu_id=str(row[0]),
pr=str(row[1]), amount=int(row[1]),
hash=str(row[2]), pr=str(row[2]),
issued=bool(row[3]), hash=str(row[3]),
issued=bool(row[4]),
) )

View file

@ -157,7 +157,7 @@ async def store_lightning_invoice(cashu_id: str, invoice: Invoice):
async def get_lightning_invoice(cashu_id: str, hash: str): async def get_lightning_invoice(cashu_id: str, hash: str):
row = await db.fetchone( row = await db.fetchone(
""" """
SELECT * from invoices SELECT * from cashu.invoices
WHERE cashu_id =? AND hash = ? WHERE cashu_id =? AND hash = ?
""", """,
( (
@ -170,7 +170,7 @@ async def get_lightning_invoice(cashu_id: str, hash: str):
async def update_lightning_invoice(cashu_id: str, hash: str, issued: bool): async def update_lightning_invoice(cashu_id: str, hash: str, issued: bool):
await db.execute( await db.execute(
"UPDATE invoices SET issued = ? WHERE cashu_id = ? AND hash = ?", "UPDATE cashu.invoices SET issued = ? WHERE cashu_id = ? AND hash = ?",
( (
issued, issued,
cashu_id, cashu_id,

View file

@ -1,5 +1,5 @@
from sqlite3 import Row from sqlite3 import Row
from typing import List, Optional from typing import List, Union
from fastapi import Query from fastapi import Query
from pydantic import BaseModel from pydantic import BaseModel
@ -146,3 +146,8 @@ class MeltPayload(BaseModel):
proofs: List[Proof] proofs: List[Proof]
amount: int amount: int
invoice: str invoice: str
class CreateTokens(BaseModel):
# cashu_id: str = Query(None)
payloads: MintPayloads
payment_hash: Union[str, None] = None

View file

@ -30,6 +30,7 @@ from .mint import get_pubkeys
from .models import ( from .models import (
Cashu, Cashu,
CheckPayload, CheckPayload,
CreateTokens,
Invoice, Invoice,
MeltPayload, MeltPayload,
MintPayloads, MintPayloads,
@ -251,12 +252,11 @@ async def mint_pay_request(
return {"pr": payment_request, "hash": payment_hash} return {"pr": payment_request, "hash": payment_hash}
@cashu_ext.post("/mint") @cashu_ext.post("/api/v1/mint/{cashu_id}")
async def mint_coins( async def mint_coins(
payloads: MintPayloads, data: CreateTokens,
payment_hash: Union[str, None] = None,
cashu_id: str = Query(None), cashu_id: str = Query(None),
wallet: WalletTypeInfo = Depends(get_key_type), wallet: WalletTypeInfo = Depends(require_admin_key),
): ):
""" """
Requests the minting of tokens belonging to a paid payment request. Requests the minting of tokens belonging to a paid payment request.
@ -268,14 +268,20 @@ async def mint_coins(
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Mint does not exist." status_code=HTTPStatus.NOT_FOUND, detail="Mint does not exist."
) )
invoice: Invoice = get_lightning_invoice(cashu_id, payment_hash) invoice: Invoice = (
None
if data.payment_hash == None
else await get_lightning_invoice(cashu_id, data.payment_hash)
)
if invoice is None: if invoice is None:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.NOT_FOUND, detail="Mint does not have this invoice." status_code=HTTPStatus.NOT_FOUND, detail="Mint does not have this invoice."
) )
# if invoice.issued == True:
# todo: give old tokens?
status: PaymentStatus = check_transaction_status(cashu.wallet, payment_hash) status: PaymentStatus = await check_transaction_status(cashu.wallet, data.payment_hash)
if status.paid == False: if status.paid != True:
raise HTTPException( raise HTTPException(
status_code=HTTPStatus.PAYMENT_REQUIRED, detail="Invoice not paid." status_code=HTTPStatus.PAYMENT_REQUIRED, detail="Invoice not paid."
) )