black formatting
This commit is contained in:
@@ -5,7 +5,8 @@ db = Database("ext_satspay")
|
||||
|
||||
|
||||
satspay_ext: Blueprint = Blueprint(
|
||||
"satspay", __name__, static_folder="static", template_folder="templates")
|
||||
"satspay", __name__, static_folder="static", template_folder="templates"
|
||||
)
|
||||
|
||||
|
||||
from .views_api import * # noqa
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from typing import List, Optional, Union
|
||||
|
||||
#from lnbits.db import open_ext_db
|
||||
# from lnbits.db import open_ext_db
|
||||
from . import db
|
||||
from .models import Charges
|
||||
|
||||
@@ -15,7 +15,17 @@ from ..watchonly.crud import get_watch_wallet, get_fresh_address, get_mempool
|
||||
###############CHARGES##########################
|
||||
|
||||
|
||||
async def create_charge(user: str, description: str = None, onchainwallet: Optional[str] = None, lnbitswallet: Optional[str] = None, webhook: Optional[str] = None, completelink: Optional[str] = None, completelinktext: Optional[str] = None, time: Optional[int] = None, amount: Optional[int] = None) -> Charges:
|
||||
async def create_charge(
|
||||
user: str,
|
||||
description: str = None,
|
||||
onchainwallet: Optional[str] = None,
|
||||
lnbitswallet: Optional[str] = None,
|
||||
webhook: Optional[str] = None,
|
||||
completelink: Optional[str] = None,
|
||||
completelinktext: Optional[str] = None,
|
||||
time: Optional[int] = None,
|
||||
amount: Optional[int] = None,
|
||||
) -> Charges:
|
||||
charge_id = urlsafe_short_hash()
|
||||
if onchainwallet:
|
||||
wallet = await get_watch_wallet(onchainwallet)
|
||||
@@ -25,9 +35,8 @@ async def create_charge(user: str, description: str = None, onchainwallet: Optio
|
||||
onchainaddress = None
|
||||
if lnbitswallet:
|
||||
payment_hash, payment_request = await create_invoice(
|
||||
wallet_id=lnbitswallet,
|
||||
amount=amount,
|
||||
memo=charge_id)
|
||||
wallet_id=lnbitswallet, amount=amount, memo=charge_id
|
||||
)
|
||||
else:
|
||||
payment_hash = None
|
||||
payment_request = None
|
||||
@@ -51,15 +60,31 @@ async def create_charge(user: str, description: str = None, onchainwallet: Optio
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(charge_id, user, description, onchainwallet, onchainaddress, lnbitswallet,
|
||||
payment_request, payment_hash, webhook, completelink, completelinktext, time, amount, 0),
|
||||
(
|
||||
charge_id,
|
||||
user,
|
||||
description,
|
||||
onchainwallet,
|
||||
onchainaddress,
|
||||
lnbitswallet,
|
||||
payment_request,
|
||||
payment_hash,
|
||||
webhook,
|
||||
completelink,
|
||||
completelinktext,
|
||||
time,
|
||||
amount,
|
||||
0,
|
||||
),
|
||||
)
|
||||
return await get_charge(charge_id)
|
||||
|
||||
|
||||
async def update_charge(charge_id: str, **kwargs) -> Optional[Charges]:
|
||||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
|
||||
await db.execute(f"UPDATE charges SET {q} WHERE id = ?", (*kwargs.values(), charge_id))
|
||||
await db.execute(
|
||||
f"UPDATE charges SET {q} WHERE id = ?", (*kwargs.values(), charge_id)
|
||||
)
|
||||
row = await db.fetchone("SELECT * FROM charges WHERE id = ?", (charge_id,))
|
||||
return Charges.from_row(row) if row else None
|
||||
|
||||
@@ -85,14 +110,18 @@ async def check_address_balance(charge_id: str) -> List[Charges]:
|
||||
mempool = await get_mempool(charge.user)
|
||||
try:
|
||||
async with httpx.AsyncClient() as client:
|
||||
r = await client.get(mempool.endpoint + "/api/address/" + charge.onchainaddress)
|
||||
respAmount = r.json()['chain_stats']['funded_txo_sum']
|
||||
r = await client.get(
|
||||
mempool.endpoint + "/api/address/" + charge.onchainaddress
|
||||
)
|
||||
respAmount = r.json()["chain_stats"]["funded_txo_sum"]
|
||||
if respAmount >= charge.balance:
|
||||
await update_charge(charge_id=charge_id, balance=respAmount)
|
||||
except Exception:
|
||||
pass
|
||||
if charge.lnbitswallet:
|
||||
invoice_status = await check_invoice_status(charge.lnbitswallet, charge.payment_hash)
|
||||
invoice_status = await check_invoice_status(
|
||||
charge.lnbitswallet, charge.payment_hash
|
||||
)
|
||||
if invoice_status.paid:
|
||||
return await update_charge(charge_id=charge_id, balance=charge.amount)
|
||||
row = await db.fetchone("SELECT * FROM charges WHERE id = ?", (charge_id,))
|
||||
|
||||
@@ -17,5 +17,6 @@ async def index():
|
||||
@satspay_ext.route("/<charge_id>")
|
||||
async def display(charge_id):
|
||||
charge = await get_charge(charge_id) or abort(
|
||||
HTTPStatus.NOT_FOUND, "Charge link does not exist.")
|
||||
HTTPStatus.NOT_FOUND, "Charge link does not exist."
|
||||
)
|
||||
return await render_template("satspay/display.html", charge=charge)
|
||||
|
||||
@@ -48,7 +48,19 @@ async def api_charge_create_or_update(charge_id=None):
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_charges_retrieve():
|
||||
try:
|
||||
return jsonify([{**charge._asdict(), **{"time_elapsed": charge.time_elapsed}, **{"paid": charge.paid}}for charge in await get_charges(g.wallet.user)]), HTTPStatus.OK
|
||||
return (
|
||||
jsonify(
|
||||
[
|
||||
{
|
||||
**charge._asdict(),
|
||||
**{"time_elapsed": charge.time_elapsed},
|
||||
**{"paid": charge.paid},
|
||||
}
|
||||
for charge in await get_charges(g.wallet.user)
|
||||
]
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
except:
|
||||
return ""
|
||||
|
||||
@@ -61,7 +73,16 @@ async def api_charge_retrieve(charge_id):
|
||||
if not charge:
|
||||
return jsonify({"message": "charge does not exist"}), HTTPStatus.NOT_FOUND
|
||||
|
||||
return jsonify({**charge._asdict(), **{"time_elapsed": charge.time_elapsed}, **{"paid": charge.paid}}), HTTPStatus.OK
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
**charge._asdict(),
|
||||
**{"time_elapsed": charge.time_elapsed},
|
||||
**{"paid": charge.paid},
|
||||
}
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
|
||||
|
||||
@satspay_ext.route("/api/v1/charge/<charge_id>", methods=["DELETE"])
|
||||
@@ -79,6 +100,7 @@ async def api_charge_delete(charge_id):
|
||||
|
||||
#############################BALANCE##########################
|
||||
|
||||
|
||||
@satspay_ext.route("/api/v1/charges/balance/<charge_id>", methods=["GET"])
|
||||
async def api_charges_balance(charge_id):
|
||||
|
||||
@@ -110,12 +132,13 @@ async def api_charges_balance(charge_id):
|
||||
charge.webhook = None
|
||||
return jsonify(charge._asdict()), HTTPStatus.OK
|
||||
|
||||
|
||||
#############################MEMPOOL##########################
|
||||
|
||||
|
||||
@ satspay_ext.route("/api/v1/mempool", methods=["PUT"])
|
||||
@ api_check_wallet_key("invoice")
|
||||
@ api_validate_post_request(
|
||||
@satspay_ext.route("/api/v1/mempool", methods=["PUT"])
|
||||
@api_check_wallet_key("invoice")
|
||||
@api_validate_post_request(
|
||||
schema={
|
||||
"endpoint": {"type": "string", "empty": False, "required": True},
|
||||
}
|
||||
@@ -125,8 +148,8 @@ async def api_update_mempool():
|
||||
return jsonify(mempool._asdict()), HTTPStatus.OK
|
||||
|
||||
|
||||
@ satspay_ext.route("/api/v1/mempool", methods=["GET"])
|
||||
@ api_check_wallet_key("invoice")
|
||||
@satspay_ext.route("/api/v1/mempool", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_get_mempool():
|
||||
mempool = await get_mempool(g.wallet.user)
|
||||
if not mempool:
|
||||
|
||||
Reference in New Issue
Block a user