feat: check paid invoices

This commit is contained in:
Vlad Stan 2022-10-07 11:47:37 +03:00
parent b27d31b381
commit 95ebd179ad
4 changed files with 26 additions and 14 deletions

View File

@ -87,10 +87,11 @@ class Invoice(BaseModel):
@classmethod
def from_row(cls, row: Row):
return cls(
amount=int(row[0]),
pr=str(row[1]),
hash=str(row[2]),
issued=bool(row[3]),
cashu_id=str(row[0]),
amount=int(row[1]),
pr=str(row[2]),
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):
row = await db.fetchone(
"""
SELECT * from invoices
SELECT * from cashu.invoices
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):
await db.execute(
"UPDATE invoices SET issued = ? WHERE cashu_id = ? AND hash = ?",
"UPDATE cashu.invoices SET issued = ? WHERE cashu_id = ? AND hash = ?",
(
issued,
cashu_id,

View File

@ -1,5 +1,5 @@
from sqlite3 import Row
from typing import List, Optional
from typing import List, Union
from fastapi import Query
from pydantic import BaseModel
@ -146,3 +146,8 @@ class MeltPayload(BaseModel):
proofs: List[Proof]
amount: int
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 (
Cashu,
CheckPayload,
CreateTokens,
Invoice,
MeltPayload,
MintPayloads,
@ -251,12 +252,11 @@ async def mint_pay_request(
return {"pr": payment_request, "hash": payment_hash}
@cashu_ext.post("/mint")
@cashu_ext.post("/api/v1/mint/{cashu_id}")
async def mint_coins(
payloads: MintPayloads,
payment_hash: Union[str, None] = None,
data: CreateTokens,
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.
@ -268,14 +268,20 @@ async def mint_coins(
raise HTTPException(
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:
raise HTTPException(
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)
if status.paid == False:
status: PaymentStatus = await check_transaction_status(cashu.wallet, data.payment_hash)
if status.paid != True:
raise HTTPException(
status_code=HTTPStatus.PAYMENT_REQUIRED, detail="Invoice not paid."
)