migrate to sqlalchemy-aio.
a big refactor that:
- fixes some issues that might have happened (or not) with asynchronous
reactions to payments;
- paves the way to https://github.com/lnbits/lnbits/issues/121;
- uses more async/await notation which just looks nice; and
- makes it simple(r?) for one extension to modify stuff from other extensions.
This commit is contained in:
@@ -1,4 +1,7 @@
|
||||
from quart import Blueprint
|
||||
from lnbits.db import Database
|
||||
|
||||
db = Database("ext_withdraw")
|
||||
|
||||
|
||||
withdraw_ext: Blueprint = Blueprint("withdraw", __name__, static_folder="static", template_folder="templates")
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
from datetime import datetime
|
||||
from typing import List, Optional, Union
|
||||
from lnbits.db import open_ext_db
|
||||
from lnbits.helpers import urlsafe_short_hash
|
||||
|
||||
from . import db
|
||||
from .models import WithdrawLink
|
||||
|
||||
|
||||
def create_withdraw_link(
|
||||
async def create_withdraw_link(
|
||||
*,
|
||||
wallet_id: str,
|
||||
title: str,
|
||||
@@ -17,49 +17,47 @@ def create_withdraw_link(
|
||||
is_unique: bool,
|
||||
usescsv: str,
|
||||
) -> WithdrawLink:
|
||||
|
||||
with open_ext_db("withdraw") as db:
|
||||
|
||||
link_id = urlsafe_short_hash()
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO withdraw_link (
|
||||
id,
|
||||
wallet,
|
||||
title,
|
||||
min_withdrawable,
|
||||
max_withdrawable,
|
||||
uses,
|
||||
wait_time,
|
||||
is_unique,
|
||||
unique_hash,
|
||||
k1,
|
||||
open_time,
|
||||
usescsv
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
link_id,
|
||||
wallet_id,
|
||||
title,
|
||||
min_withdrawable,
|
||||
max_withdrawable,
|
||||
uses,
|
||||
wait_time,
|
||||
int(is_unique),
|
||||
urlsafe_short_hash(),
|
||||
urlsafe_short_hash(),
|
||||
int(datetime.now().timestamp()) + wait_time,
|
||||
usescsv,
|
||||
),
|
||||
link_id = urlsafe_short_hash()
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO withdraw_link (
|
||||
id,
|
||||
wallet,
|
||||
title,
|
||||
min_withdrawable,
|
||||
max_withdrawable,
|
||||
uses,
|
||||
wait_time,
|
||||
is_unique,
|
||||
unique_hash,
|
||||
k1,
|
||||
open_time,
|
||||
usescsv
|
||||
)
|
||||
return get_withdraw_link(link_id, 0)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(
|
||||
link_id,
|
||||
wallet_id,
|
||||
title,
|
||||
min_withdrawable,
|
||||
max_withdrawable,
|
||||
uses,
|
||||
wait_time,
|
||||
int(is_unique),
|
||||
urlsafe_short_hash(),
|
||||
urlsafe_short_hash(),
|
||||
int(datetime.now().timestamp()) + wait_time,
|
||||
usescsv,
|
||||
),
|
||||
)
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
assert link, "Newly created link couldn't be retrieved"
|
||||
return link
|
||||
|
||||
|
||||
def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
|
||||
with open_ext_db("withdraw") as db:
|
||||
row = db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
async def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
|
||||
row = await db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
link = []
|
||||
for item in row:
|
||||
link.append(item)
|
||||
@@ -67,39 +65,34 @@ def get_withdraw_link(link_id: str, num=0) -> Optional[WithdrawLink]:
|
||||
return WithdrawLink._make(link)
|
||||
|
||||
|
||||
def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
|
||||
with open_ext_db("withdraw") as db:
|
||||
row = db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,))
|
||||
link = []
|
||||
for item in row:
|
||||
link.append(item)
|
||||
async def get_withdraw_link_by_hash(unique_hash: str, num=0) -> Optional[WithdrawLink]:
|
||||
row = await db.fetchone("SELECT * FROM withdraw_link WHERE unique_hash = ?", (unique_hash,))
|
||||
link = []
|
||||
for item in row:
|
||||
link.append(item)
|
||||
link.append(num)
|
||||
return WithdrawLink._make(link)
|
||||
|
||||
|
||||
def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
|
||||
async def get_withdraw_links(wallet_ids: Union[str, List[str]]) -> List[WithdrawLink]:
|
||||
if isinstance(wallet_ids, str):
|
||||
wallet_ids = [wallet_ids]
|
||||
|
||||
with open_ext_db("withdraw") as db:
|
||||
q = ",".join(["?"] * len(wallet_ids))
|
||||
rows = db.fetchall(f"SELECT * FROM withdraw_link WHERE wallet IN ({q})", (*wallet_ids,))
|
||||
q = ",".join(["?"] * len(wallet_ids))
|
||||
rows = await db.fetchall(f"SELECT * FROM withdraw_link WHERE wallet IN ({q})", (*wallet_ids,))
|
||||
|
||||
return [WithdrawLink.from_row(row) for row in rows]
|
||||
|
||||
|
||||
def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
|
||||
async def update_withdraw_link(link_id: str, **kwargs) -> Optional[WithdrawLink]:
|
||||
q = ", ".join([f"{field[0]} = ?" for field in kwargs.items()])
|
||||
with open_ext_db("withdraw") as db:
|
||||
db.execute(f"UPDATE withdraw_link SET {q} WHERE id = ?", (*kwargs.values(), link_id))
|
||||
row = db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
|
||||
await db.execute(f"UPDATE withdraw_link SET {q} WHERE id = ?", (*kwargs.values(), link_id))
|
||||
row = await db.fetchone("SELECT * FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
return WithdrawLink.from_row(row) if row else None
|
||||
|
||||
|
||||
def delete_withdraw_link(link_id: str) -> None:
|
||||
with open_ext_db("withdraw") as db:
|
||||
db.execute("DELETE FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
async def delete_withdraw_link(link_id: str) -> None:
|
||||
await db.execute("DELETE FROM withdraw_link WHERE id = ?", (link_id,))
|
||||
|
||||
|
||||
def chunks(lst, n):
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
def m001_initial(db):
|
||||
async def m001_initial(db):
|
||||
"""
|
||||
Creates an improved withdraw table and migrates the existing data.
|
||||
"""
|
||||
db.execute(
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS withdraw_links (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -23,11 +23,11 @@ def m001_initial(db):
|
||||
)
|
||||
|
||||
|
||||
def m002_change_withdraw_table(db):
|
||||
async def m002_change_withdraw_table(db):
|
||||
"""
|
||||
Creates an improved withdraw table and migrates the existing data.
|
||||
"""
|
||||
db.execute(
|
||||
await db.execute(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS withdraw_link (
|
||||
id TEXT PRIMARY KEY,
|
||||
@@ -46,10 +46,10 @@ def m002_change_withdraw_table(db):
|
||||
);
|
||||
"""
|
||||
)
|
||||
db.execute("CREATE INDEX IF NOT EXISTS wallet_idx ON withdraw_link (wallet)")
|
||||
db.execute("CREATE UNIQUE INDEX IF NOT EXISTS unique_hash_idx ON withdraw_link (unique_hash)")
|
||||
await db.execute("CREATE INDEX IF NOT EXISTS wallet_idx ON withdraw_link (wallet)")
|
||||
await db.execute("CREATE UNIQUE INDEX IF NOT EXISTS unique_hash_idx ON withdraw_link (unique_hash)")
|
||||
|
||||
for row in [list(row) for row in db.fetchall("SELECT * FROM withdraw_links")]:
|
||||
for row in [list(row) for row in await db.fetchall("SELECT * FROM withdraw_links")]:
|
||||
usescsv = ""
|
||||
|
||||
for i in range(row[5]):
|
||||
@@ -58,7 +58,7 @@ def m002_change_withdraw_table(db):
|
||||
else:
|
||||
usescsv += "," + str(1)
|
||||
usescsv = usescsv[1:]
|
||||
db.execute(
|
||||
await db.execute(
|
||||
"""
|
||||
INSERT INTO withdraw_link (
|
||||
id,
|
||||
@@ -93,4 +93,4 @@ def m002_change_withdraw_table(db):
|
||||
usescsv,
|
||||
),
|
||||
)
|
||||
db.execute("DROP TABLE withdraw_links")
|
||||
await db.execute("DROP TABLE withdraw_links")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from quart import url_for
|
||||
from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode
|
||||
from lnurl import Lnurl, LnurlWithdrawResponse, encode as lnurl_encode # type: ignore
|
||||
from sqlite3 import Row
|
||||
from typing import NamedTuple
|
||||
import shortuuid # type: ignore
|
||||
|
||||
@@ -3,7 +3,7 @@ from http import HTTPStatus
|
||||
|
||||
from lnbits.decorators import check_user_exists, validate_uuids
|
||||
|
||||
from lnbits.extensions.withdraw import withdraw_ext
|
||||
from . import withdraw_ext
|
||||
from .crud import get_withdraw_link, chunks
|
||||
|
||||
|
||||
@@ -16,19 +16,19 @@ async def index():
|
||||
|
||||
@withdraw_ext.route("/<link_id>")
|
||||
async def display(link_id):
|
||||
link = get_withdraw_link(link_id, 0) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
link = await get_withdraw_link(link_id, 0) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
return await render_template("withdraw/display.html", link=link, unique=True)
|
||||
|
||||
|
||||
@withdraw_ext.route("/print/<link_id>")
|
||||
async def print_qr(link_id):
|
||||
link = get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
link = await get_withdraw_link(link_id) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
if link.uses == 0:
|
||||
return await render_template("withdraw/print_qr.html", link=link, unique=False)
|
||||
links = []
|
||||
count = 0
|
||||
for x in link.usescsv.split(","):
|
||||
linkk = get_withdraw_link(link_id, count) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
linkk = await get_withdraw_link(link_id, count) or abort(HTTPStatus.NOT_FOUND, "Withdraw link does not exist.")
|
||||
links.append(str(linkk.lnurl))
|
||||
count = count + 1
|
||||
page_link = list(chunks(links, 2))
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
from datetime import datetime
|
||||
from quart import g, jsonify, request
|
||||
from http import HTTPStatus
|
||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl
|
||||
from lnurl.exceptions import InvalidUrl as LnurlInvalidUrl # type: ignore
|
||||
import shortuuid # type: ignore
|
||||
|
||||
from lnbits.core.crud import get_user
|
||||
from lnbits.core.services import pay_invoice
|
||||
from lnbits.decorators import api_check_wallet_key, api_validate_post_request
|
||||
|
||||
from lnbits.extensions.withdraw import withdraw_ext
|
||||
from . import withdraw_ext
|
||||
from .crud import (
|
||||
create_withdraw_link,
|
||||
get_withdraw_link,
|
||||
@@ -25,10 +25,18 @@ async def api_links():
|
||||
wallet_ids = [g.wallet.id]
|
||||
|
||||
if "all_wallets" in request.args:
|
||||
wallet_ids = get_user(g.wallet.user).wallet_ids
|
||||
wallet_ids = (await get_user(g.wallet.user)).wallet_ids
|
||||
try:
|
||||
return (
|
||||
jsonify([{**link._asdict(), **{"lnurl": link.lnurl}} for link in get_withdraw_links(wallet_ids)]),
|
||||
jsonify(
|
||||
[
|
||||
{
|
||||
**link._asdict(),
|
||||
**{"lnurl": link.lnurl},
|
||||
}
|
||||
for link in await get_withdraw_links(wallet_ids)
|
||||
]
|
||||
),
|
||||
HTTPStatus.OK,
|
||||
)
|
||||
except LnurlInvalidUrl:
|
||||
@@ -41,7 +49,7 @@ async def api_links():
|
||||
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["GET"])
|
||||
@api_check_wallet_key("invoice")
|
||||
async def api_link_retrieve(link_id):
|
||||
link = get_withdraw_link(link_id, 0)
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
@@ -82,21 +90,22 @@ async def api_link_create_or_update(link_id=None):
|
||||
usescsv = usescsv[1:]
|
||||
|
||||
if link_id:
|
||||
link = get_withdraw_link(link_id, 0)
|
||||
link = await get_withdraw_link(link_id, 0)
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
||||
link = update_withdraw_link(link_id, **g.data, usescsv=usescsv, used=0)
|
||||
link = await update_withdraw_link(link_id, **g.data, usescsv=usescsv, used=0)
|
||||
else:
|
||||
link = create_withdraw_link(wallet_id=g.wallet.id, **g.data, usescsv=usescsv)
|
||||
link = await create_withdraw_link(wallet_id=g.wallet.id, **g.data, usescsv=usescsv)
|
||||
|
||||
return jsonify({**link._asdict(), **{"lnurl": link.lnurl}}), HTTPStatus.OK if link_id else HTTPStatus.CREATED
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/links/<link_id>", methods=["DELETE"])
|
||||
@api_check_wallet_key("admin")
|
||||
async def api_link_delete(link_id):
|
||||
link = get_withdraw_link(link_id)
|
||||
link = await get_withdraw_link(link_id)
|
||||
|
||||
if not link:
|
||||
return jsonify({"message": "Withdraw link does not exist."}), HTTPStatus.NOT_FOUND
|
||||
@@ -104,7 +113,7 @@ async def api_link_delete(link_id):
|
||||
if link.wallet != g.wallet.id:
|
||||
return jsonify({"message": "Not your withdraw link."}), HTTPStatus.FORBIDDEN
|
||||
|
||||
delete_withdraw_link(link_id)
|
||||
await delete_withdraw_link(link_id)
|
||||
|
||||
return "", HTTPStatus.NO_CONTENT
|
||||
|
||||
@@ -114,7 +123,7 @@ async def api_link_delete(link_id):
|
||||
|
||||
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>", methods=["GET"])
|
||||
async def api_lnurl_response(unique_hash):
|
||||
link = get_withdraw_link_by_hash(unique_hash)
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
|
||||
if not link:
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
|
||||
@@ -125,7 +134,7 @@ async def api_lnurl_response(unique_hash):
|
||||
for x in range(1, link.uses - link.used):
|
||||
usescsv += "," + str(1)
|
||||
usescsv = usescsv[1:]
|
||||
link = update_withdraw_link(link.id, used=link.used + 1, usescsv=usescsv)
|
||||
link = await update_withdraw_link(link.id, used=link.used + 1, usescsv=usescsv)
|
||||
|
||||
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK
|
||||
|
||||
@@ -135,7 +144,7 @@ async def api_lnurl_response(unique_hash):
|
||||
|
||||
@withdraw_ext.route("/api/v1/lnurl/<unique_hash>/<id_unique_hash>", methods=["GET"])
|
||||
async def api_lnurl_multi_response(unique_hash, id_unique_hash):
|
||||
link = get_withdraw_link_by_hash(unique_hash)
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
|
||||
if not link:
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
|
||||
@@ -156,13 +165,13 @@ async def api_lnurl_multi_response(unique_hash, id_unique_hash):
|
||||
return jsonify({"status": "ERROR", "reason": "LNURL-withdraw not found."}), HTTPStatus.OK
|
||||
|
||||
usescsv = usescsv[1:]
|
||||
link = update_withdraw_link(link.id, usescsv=usescsv)
|
||||
link = await update_withdraw_link(link.id, usescsv=usescsv)
|
||||
return jsonify(link.lnurl_response.dict()), HTTPStatus.OK
|
||||
|
||||
|
||||
@withdraw_ext.route("/api/v1/lnurl/cb/<unique_hash>", methods=["GET"])
|
||||
async def api_lnurl_callback(unique_hash):
|
||||
link = get_withdraw_link_by_hash(unique_hash)
|
||||
link = await get_withdraw_link_by_hash(unique_hash)
|
||||
k1 = request.args.get("k1", type=str)
|
||||
payment_request = request.args.get("pr", type=str)
|
||||
now = int(datetime.now().timestamp())
|
||||
@@ -180,7 +189,7 @@ async def api_lnurl_callback(unique_hash):
|
||||
return jsonify({"status": "ERROR", "reason": f"Wait {link.open_time - now} seconds."}), HTTPStatus.OK
|
||||
|
||||
try:
|
||||
pay_invoice(
|
||||
await pay_invoice(
|
||||
wallet_id=link.wallet,
|
||||
payment_request=payment_request,
|
||||
max_sat=link.max_withdrawable,
|
||||
@@ -189,12 +198,10 @@ async def api_lnurl_callback(unique_hash):
|
||||
|
||||
changes = {"open_time": link.wait_time + now, "used": link.used + 1}
|
||||
|
||||
update_withdraw_link(link.id, **changes)
|
||||
await update_withdraw_link(link.id, **changes)
|
||||
except ValueError as e:
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
|
||||
except PermissionError:
|
||||
return jsonify({"status": "ERROR", "reason": "Withdraw link is empty."}), HTTPStatus.OK
|
||||
except Exception as e:
|
||||
return jsonify({"status": "ERROR", "reason": str(e)}), HTTPStatus.OK
|
||||
|
||||
return jsonify({"status": "OK"}), HTTPStatus.OK
|
||||
|
||||
Reference in New Issue
Block a user