refactor: a wallet is a wallet is a wallet
This commit is contained in:
+13
-11
@@ -125,15 +125,15 @@ def get_wallet_for_key(key: str, key_type: str = "invoice") -> Optional[Wallet]:
|
||||
# ---------------
|
||||
|
||||
|
||||
def get_wallet_payment(wallet_id: str, payhash: str) -> Optional[Payment]:
|
||||
def get_wallet_payment(wallet_id: str, checking_id: str) -> Optional[Payment]:
|
||||
with open_db() as db:
|
||||
row = db.fetchone(
|
||||
"""
|
||||
SELECT payhash, amount, fee, pending, memo, time
|
||||
SELECT payhash as checking_id, amount, fee, pending, memo, time
|
||||
FROM apipayments
|
||||
WHERE wallet = ? AND payhash = ?
|
||||
""",
|
||||
(wallet_id, payhash),
|
||||
(wallet_id, checking_id),
|
||||
)
|
||||
|
||||
return Payment(**row) if row else None
|
||||
@@ -148,7 +148,7 @@ def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) ->
|
||||
|
||||
rows = db.fetchall(
|
||||
f"""
|
||||
SELECT payhash, amount, fee, pending, memo, time
|
||||
SELECT payhash as checking_id, amount, fee, pending, memo, time
|
||||
FROM apipayments
|
||||
WHERE wallet = ? AND {clause}
|
||||
ORDER BY time DESC
|
||||
@@ -163,24 +163,26 @@ def get_wallet_payments(wallet_id: str, *, include_all_pending: bool = False) ->
|
||||
# --------
|
||||
|
||||
|
||||
def create_payment(*, wallet_id: str, payhash: str, amount: str, memo: str, fee: int = 0) -> Payment:
|
||||
def create_payment(
|
||||
*, wallet_id: str, checking_id: str, amount: str, memo: str, fee: int = 0, pending: bool = True
|
||||
) -> Payment:
|
||||
with open_db() as db:
|
||||
db.execute(
|
||||
"""
|
||||
INSERT INTO apipayments (wallet, payhash, amount, pending, memo, fee)
|
||||
VALUES (?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(wallet_id, payhash, amount, 1, memo, fee),
|
||||
(wallet_id, checking_id, amount, int(pending), memo, fee),
|
||||
)
|
||||
|
||||
return get_wallet_payment(wallet_id, payhash)
|
||||
return get_wallet_payment(wallet_id, checking_id)
|
||||
|
||||
|
||||
def update_payment_status(payhash: str, pending: bool) -> None:
|
||||
def update_payment_status(checking_id: str, pending: bool) -> None:
|
||||
with open_db() as db:
|
||||
db.execute("UPDATE apipayments SET pending = ? WHERE payhash = ?", (int(pending), payhash,))
|
||||
db.execute("UPDATE apipayments SET pending = ? WHERE payhash = ?", (int(pending), checking_id,))
|
||||
|
||||
|
||||
def delete_payment(payhash: str) -> None:
|
||||
def delete_payment(checking_id: str) -> None:
|
||||
with open_db() as db:
|
||||
db.execute("DELETE FROM apipayments WHERE payhash = ?", (payhash,))
|
||||
db.execute("DELETE FROM apipayments WHERE payhash = ?", (checking_id,))
|
||||
|
||||
@@ -29,10 +29,10 @@ class Wallet(NamedTuple):
|
||||
def balance(self) -> int:
|
||||
return int(self.balance / 1000)
|
||||
|
||||
def get_payment(self, payhash: str) -> "Payment":
|
||||
def get_payment(self, checking_id: str) -> "Payment":
|
||||
from .crud import get_wallet_payment
|
||||
|
||||
return get_wallet_payment(self.id, payhash)
|
||||
return get_wallet_payment(self.id, checking_id)
|
||||
|
||||
def get_payments(self, *, include_all_pending: bool = False) -> List["Payment"]:
|
||||
from .crud import get_wallet_payments
|
||||
@@ -41,7 +41,7 @@ class Wallet(NamedTuple):
|
||||
|
||||
|
||||
class Payment(NamedTuple):
|
||||
payhash: str
|
||||
checking_id: str
|
||||
pending: bool
|
||||
amount: int
|
||||
fee: int
|
||||
@@ -67,9 +67,9 @@ class Payment(NamedTuple):
|
||||
def set_pending(self, pending: bool) -> None:
|
||||
from .crud import update_payment_status
|
||||
|
||||
update_payment_status(self.payhash, pending)
|
||||
update_payment_status(self.checking_id, pending)
|
||||
|
||||
def delete(self) -> None:
|
||||
from .crud import delete_payment
|
||||
|
||||
delete_payment(self.payhash)
|
||||
delete_payment(self.checking_id)
|
||||
|
||||
@@ -160,6 +160,7 @@ new Vue({
|
||||
: false;
|
||||
},
|
||||
paymentsFiltered: function () {
|
||||
return this.payments;
|
||||
return this.payments.filter(function (obj) {
|
||||
return obj.isPaid;
|
||||
});
|
||||
@@ -222,7 +223,7 @@ new Vue({
|
||||
self.receive.paymentReq = response.data.payment_request;
|
||||
|
||||
self.receive.paymentChecker = setInterval(function () {
|
||||
LNbits.api.getPayment(self.w.wallet, response.data.payment_hash).then(function (response) {
|
||||
LNbits.api.getPayment(self.w.wallet, response.data.checking_id).then(function (response) {
|
||||
if (response.data.paid) {
|
||||
self.fetchPayments();
|
||||
self.receive.show = false;
|
||||
@@ -284,20 +285,21 @@ new Vue({
|
||||
icon: null
|
||||
});
|
||||
|
||||
LNbits.api.payInvoice(this.w.wallet, this.send.data.bolt11).catch(function (error) {
|
||||
LNbits.api.payInvoice(this.w.wallet, this.send.data.bolt11).then(function (response) {
|
||||
self.send.paymentChecker = setInterval(function () {
|
||||
LNbits.api.getPayment(self.w.wallet, response.data.checking_id).then(function (res) {
|
||||
if (res.data.paid) {
|
||||
self.send.show = false;
|
||||
clearInterval(self.send.paymentChecker);
|
||||
dismissPaymentMsg();
|
||||
self.fetchPayments();
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
}).catch(function (error) {
|
||||
dismissPaymentMsg();
|
||||
LNbits.utils.notifyApiError(error);
|
||||
});
|
||||
|
||||
self.send.paymentChecker = setInterval(function () {
|
||||
LNbits.api.getPayment(self.w.wallet, self.send.invoice.hash).then(function (response) {
|
||||
if (response.data.paid) {
|
||||
self.send.show = false;
|
||||
clearInterval(self.send.paymentChecker);
|
||||
dismissPaymentMsg();
|
||||
self.fetchPayments();
|
||||
}
|
||||
});
|
||||
}, 2000);
|
||||
},
|
||||
deleteWallet: function (walletId, user) {
|
||||
LNbits.href.deleteWallet(walletId, user);
|
||||
@@ -327,8 +329,6 @@ new Vue({
|
||||
},
|
||||
created: function () {
|
||||
this.fetchPayments();
|
||||
setTimeout(function () {
|
||||
this.checkPendingPayments();
|
||||
}, 1100);
|
||||
setTimeout(this.checkPendingPayments(), 1200);
|
||||
}
|
||||
});
|
||||
|
||||
+27
-28
@@ -15,9 +15,9 @@ def api_payments():
|
||||
if "check_pending" in request.args:
|
||||
for payment in g.wallet.get_payments(include_all_pending=True):
|
||||
if payment.is_out:
|
||||
payment.set_pending(WALLET.get_payment_status(payment.payhash).pending)
|
||||
payment.set_pending(WALLET.get_payment_status(payment.checking_id).pending)
|
||||
elif payment.is_in:
|
||||
payment.set_pending(WALLET.get_invoice_status(payment.payhash).pending)
|
||||
payment.set_pending(WALLET.get_invoice_status(payment.checking_id).pending)
|
||||
|
||||
return jsonify(g.wallet.get_payments()), Status.OK
|
||||
|
||||
@@ -32,18 +32,17 @@ def api_payments_create_invoice():
|
||||
return jsonify({"message": "`memo` needs to be a valid string."}), Status.BAD_REQUEST
|
||||
|
||||
try:
|
||||
r, payhash, payment_request = WALLET.create_invoice(g.data["amount"], g.data["memo"])
|
||||
server_error = not r.ok or "message" in r.json()
|
||||
except Exception:
|
||||
server_error = True
|
||||
ok, checking_id, payment_request, error_message = WALLET.create_invoice(g.data["amount"], g.data["memo"])
|
||||
except Exception as e:
|
||||
ok, error_message = False, str(e)
|
||||
|
||||
if server_error:
|
||||
return jsonify({"message": "Unexpected backend error. Try again later."}), Status.INTERNAL_SERVER_ERROR
|
||||
if not ok:
|
||||
return jsonify({"message": error_message or "Unexpected backend error."}), Status.INTERNAL_SERVER_ERROR
|
||||
|
||||
amount_msat = g.data["amount"] * 1000
|
||||
create_payment(wallet_id=g.wallet.id, payhash=payhash, amount=amount_msat, memo=g.data["memo"])
|
||||
create_payment(wallet_id=g.wallet.id, checking_id=checking_id, amount=amount_msat, memo=g.data["memo"])
|
||||
|
||||
return jsonify({"payment_request": payment_request, "payment_hash": payhash}), Status.CREATED
|
||||
return jsonify({"checking_id": checking_id, "payment_request": payment_request}), Status.CREATED
|
||||
|
||||
|
||||
@api_check_wallet_macaroon(key_type="invoice")
|
||||
@@ -61,24 +60,24 @@ def api_payments_pay_invoice():
|
||||
if invoice.amount_msat > g.wallet.balance_msat:
|
||||
return jsonify({"message": "Insufficient balance."}), Status.FORBIDDEN
|
||||
|
||||
create_payment(
|
||||
wallet_id=g.wallet.id,
|
||||
payhash=invoice.payment_hash,
|
||||
amount=-invoice.amount_msat,
|
||||
memo=invoice.description,
|
||||
fee=-invoice.amount_msat * FEE_RESERVE,
|
||||
)
|
||||
ok, checking_id, fee_msat, error_message = WALLET.pay_invoice(g.data["bolt11"])
|
||||
|
||||
r, server_error, fee_msat, error_message = WALLET.pay_invoice(g.data["bolt11"])
|
||||
if ok:
|
||||
create_payment(
|
||||
wallet_id=g.wallet.id,
|
||||
checking_id=checking_id,
|
||||
amount=-invoice.amount_msat,
|
||||
memo=invoice.description,
|
||||
fee=-invoice.amount_msat * FEE_RESERVE,
|
||||
)
|
||||
|
||||
except Exception as e:
|
||||
server_error = True
|
||||
error_message = str(e)
|
||||
ok, error_message = False, str(e)
|
||||
|
||||
if server_error:
|
||||
return jsonify({"message": error_message}), Status.INTERNAL_SERVER_ERROR
|
||||
if not ok:
|
||||
return jsonify({"message": error_message or "Unexpected backend error."}), Status.INTERNAL_SERVER_ERROR
|
||||
|
||||
return jsonify({"payment_hash": invoice.payment_hash}), Status.CREATED
|
||||
return jsonify({"checking_id": checking_id}), Status.CREATED
|
||||
|
||||
|
||||
@core_app.route("/api/v1/payments", methods=["POST"])
|
||||
@@ -89,10 +88,10 @@ def api_payments_create():
|
||||
return api_payments_create_invoice()
|
||||
|
||||
|
||||
@core_app.route("/api/v1/payments/<payhash>", methods=["GET"])
|
||||
@core_app.route("/api/v1/payments/<checking_id>", methods=["GET"])
|
||||
@api_check_wallet_macaroon(key_type="invoice")
|
||||
def api_payment(payhash):
|
||||
payment = g.wallet.get_payment(payhash)
|
||||
def api_payment(checking_id):
|
||||
payment = g.wallet.get_payment(checking_id)
|
||||
|
||||
if not payment:
|
||||
return jsonify({"message": "Payment does not exist."}), Status.NOT_FOUND
|
||||
@@ -101,9 +100,9 @@ def api_payment(payhash):
|
||||
|
||||
try:
|
||||
if payment.is_out:
|
||||
is_paid = WALLET.get_payment_status(payhash).paid
|
||||
is_paid = WALLET.get_payment_status(checking_id).paid
|
||||
elif payment.is_in:
|
||||
is_paid = WALLET.get_invoice_status(payhash).paid
|
||||
is_paid = WALLET.get_invoice_status(checking_id).paid
|
||||
except Exception:
|
||||
return jsonify({"paid": False}), Status.OK
|
||||
|
||||
|
||||
@@ -14,12 +14,20 @@ from ..crud import create_account, get_user, create_wallet, create_payment
|
||||
|
||||
@core_app.route("/lnurlwallet")
|
||||
def lnurlwallet():
|
||||
memo = "LNbits LNURL funding"
|
||||
|
||||
try:
|
||||
withdraw_res = handle_lnurl(request.args.get("lightning"), response_class=LnurlWithdrawResponse)
|
||||
except LnurlException:
|
||||
abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
|
||||
|
||||
_, payhash, payment_request = WALLET.create_invoice(withdraw_res.max_sats, "LNbits LNURL funding")
|
||||
try:
|
||||
ok, checking_id, payment_request, error_message = WALLET.create_invoice(withdraw_res.max_sats, memo)
|
||||
except Exception as e:
|
||||
ok, error_message = False, str(e)
|
||||
|
||||
if not ok:
|
||||
abort(Status.INTERNAL_SERVER_ERROR, error_message)
|
||||
|
||||
r = requests.get(
|
||||
withdraw_res.callback.base,
|
||||
@@ -30,16 +38,20 @@ def lnurlwallet():
|
||||
abort(Status.INTERNAL_SERVER_ERROR, "Could not process withdraw LNURL.")
|
||||
|
||||
for i in range(10):
|
||||
r = WALLET.get_invoice_status(payhash).raw_response
|
||||
invoice_status = WALLET.get_invoice_status(checking_id)
|
||||
sleep(i)
|
||||
if not r.ok:
|
||||
if not invoice_status.paid:
|
||||
continue
|
||||
break
|
||||
|
||||
user = get_user(create_account().id)
|
||||
wallet = create_wallet(user_id=user.id)
|
||||
create_payment( # TODO: not pending?
|
||||
wallet_id=wallet.id, payhash=payhash, amount=withdraw_res.max_sats * 1000, memo="LNbits lnurl funding"
|
||||
create_payment(
|
||||
wallet_id=wallet.id,
|
||||
checking_id=checking_id,
|
||||
amount=withdraw_res.max_sats * 1000,
|
||||
memo=memo,
|
||||
pending=invoice_status.pending,
|
||||
)
|
||||
|
||||
return redirect(url_for("core.wallet", usr=user.id, wal=wallet.id))
|
||||
|
||||
Reference in New Issue
Block a user