Browse Source

refactor: code review

fee_issues
Eneko Illarramendi 5 years ago
parent
commit
0d517b884a
  1. 20
      lnbits/__init__.py
  2. 9
      lnbits/wallets/base.py
  3. 27
      lnbits/wallets/lnd.py
  4. 20
      lnbits/wallets/lntxbot.py
  5. 28
      lnbits/wallets/opennode.py

20
lnbits/__init__.py

@ -282,7 +282,7 @@ def api_transactions():
# insert the payment # insert the payment
db.execute( db.execute(
"INSERT INTO apipayments (payhash, amount, fee, wallet, pending, memo) VALUES (?, ?, ?, ?, 1, ?)", "INSERT OR IGNORE INTO apipayments (payhash, amount, fee, wallet, pending, memo) VALUES (?, ?, ?, ?, 1, ?)",
( (
invoice.payment_hash, invoice.payment_hash,
-int(invoice.amount_msat), -int(invoice.amount_msat),
@ -305,26 +305,14 @@ def api_transactions():
else: else:
# actually send the payment # actually send the payment
r = WALLET.pay_invoice(data["payment_request"]) r = WALLET.pay_invoice(data["payment_request"])
print(r)
if not r.raw_response:
return jsonify({"ERROR": "UNEXPECTED PAYMENT ERROR"}), 500
data = r.raw_response.json()
print(data) if not r.raw_response.ok or r.failed:
return jsonify({"ERROR": "UNEXPECTED PAYMENT ERROR"}), 500
if r.raw_response and "error" in data:
# payment didn't went through, delete it here
# (these guarantees specific to lntxbot)
db.execute("DELETE FROM apipayments WHERE payhash = ?", (invoice.payment_hash,))
return jsonify({"PAID": "FALSE"}), 200
# payment went through, not pending anymore, save actual fees # payment went through, not pending anymore, save actual fees
db.execute( db.execute(
"UPDATE apipayments SET pending = 0, fee = ? WHERE payhash = ? AND wallet = ?", "UPDATE apipayments SET pending = 0, fee = ? WHERE payhash = ? AND wallet = ?",
(invoice.amount_msat, invoice.payment_hash, wallet["id"]), (r.fee_msat, invoice.payment_hash, wallet["id"]),
) )
return jsonify({"PAID": "TRUE", "payment_hash": invoice.payment_hash}), 200 return jsonify({"PAID": "TRUE", "payment_hash": invoice.payment_hash}), 200

9
lnbits/wallets/base.py

@ -9,12 +9,15 @@ class InvoiceResponse(NamedTuple):
payment_request: Optional[str] = None payment_request: Optional[str] = None
class PaymentResponse(NamedTuple):
raw_response: Response
failed: bool = False
fee_msat: int = 0
class TxStatus(NamedTuple): class TxStatus(NamedTuple):
raw_response: Response raw_response: Response
settled: Optional[bool] = None settled: Optional[bool] = None
class PaymentResponse(NamedTuple):
raw_response: Response
class Wallet(ABC): class Wallet(ABC):

27
lnbits/wallets/lnd.py

@ -1,7 +1,5 @@
from requests import Response, get, post from requests import get, post
from flask import jsonify from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
from .base import InvoiceResponse, TxStatus, Wallet, PaymentResponse
import json
class LndWallet(Wallet): class LndWallet(Wallet):
@ -26,28 +24,23 @@ class LndWallet(Wallet):
payment_request = data["payment_request"] payment_request = data["payment_request"]
rr = get(url=f"{self.endpoint}/v1/payreq/{payment_request}", headers=self.auth_read) rr = get(url=f"{self.endpoint}/v1/payreq/{payment_request}", headers=self.auth_read)
print(rr.json())
if rr.ok: if rr.ok:
dataa = rr.json() dataa = rr.json()
payment_hash = dataa["payment_hash"] payment_hash = dataa["payment_hash"]
return InvoiceResponse(r, payment_hash, payment_request) return InvoiceResponse(r, payment_hash, payment_request)
def pay_invoice(self, bolt11: str) -> PaymentResponse: def pay_invoice(self, bolt11: str) -> PaymentResponse:
fee_msat = None r = post(
r = post(url=f"{self.endpoint}/v1/channels/transactions", headers=self.auth_admin, json={"payment_request": bolt11}) url=f"{self.endpoint}/v1/channels/transactions", headers=self.auth_admin, json={"payment_request": bolt11}
return PaymentResponse(r) )
return PaymentResponse(r, not r.ok)
def get_invoice_status(self, payment_hash: str, wait: bool = True) -> TxStatus: def get_invoice_status(self, payment_hash: str, wait: bool = True) -> TxStatus:
r = get(url=f"{self.endpoint}/v1/invoice/{payment_hash}", headers=self.auth_read) r = get(url=f"{self.endpoint}/v1/invoice/{payment_hash}", headers=self.auth_read)
# print(payment_hash)
print(r.json()) if not r.ok or "settled" not in r.json():
if not r.ok:
return TxStatus(r, None) return TxStatus(r, None)
return TxStatus(r, r.json()["settled"]) return TxStatus(r, r.json()["settled"])
@ -64,5 +57,3 @@ class LndWallet(Wallet):
# check payment.status: https://api.lightning.community/rest/index.html?python#peersynctype # check payment.status: https://api.lightning.community/rest/index.html?python#peersynctype
statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False} statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False}
return TxStatus(r, statuses[payment["status"]] if payment else None) return TxStatus(r, statuses[payment["status"]] if payment else None)

20
lnbits/wallets/lntxbot.py

@ -1,7 +1,6 @@
from requests import Response, get, post from requests import post
from flask import jsonify
from .base import InvoiceResponse, TxStatus, Wallet, PaymentResponse from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
import json
class LntxbotWallet(Wallet): class LntxbotWallet(Wallet):
@ -23,11 +22,17 @@ class LntxbotWallet(Wallet):
return InvoiceResponse(r, payment_hash, payment_request) return InvoiceResponse(r, payment_hash, payment_request)
def pay_invoice(self, bolt11: str) -> PaymentResponse: def pay_invoice(self, bolt11: str) -> PaymentResponse:
fee_msat = None
r = post(url=f"{self.endpoint}/payinvoice", headers=self.auth_admin, json={"invoice": bolt11}) r = post(url=f"{self.endpoint}/payinvoice", headers=self.auth_admin, json={"invoice": bolt11})
return PaymentResponse(r) failed, fee_msat = not r.ok, 0
if r.ok:
data = r.json()
if "error" in data and data["error"]:
failed = True
elif "fee_msat" in data:
fee_msat = data["fee_msat"]
return PaymentResponse(r, failed, fee_msat)
def get_invoice_status(self, payment_hash: str, wait: bool = True) -> TxStatus: def get_invoice_status(self, payment_hash: str, wait: bool = True) -> TxStatus:
wait = "true" if wait else "false" wait = "true" if wait else "false"
@ -42,7 +47,6 @@ class LntxbotWallet(Wallet):
return TxStatus(r, True) return TxStatus(r, True)
def get_payment_status(self, payment_hash: str) -> TxStatus: def get_payment_status(self, payment_hash: str) -> TxStatus:
r = post(url=f"{self.endpoint}/paymentstatus/{payment_hash}", headers=self.auth_invoice) r = post(url=f"{self.endpoint}/paymentstatus/{payment_hash}", headers=self.auth_invoice)
data = r.json() data = r.json()
@ -51,5 +55,3 @@ class LntxbotWallet(Wallet):
return TxStatus(r, None) return TxStatus(r, None)
return TxStatus(r, {"complete": True, "failed": False, "unknown": None}[data.get("status", "unknown")]) return TxStatus(r, {"complete": True, "failed": False, "unknown": None}[data.get("status", "unknown")])

28
lnbits/wallets/opennode.py

@ -1,7 +1,7 @@
from requests import Response, get, post from requests import get, post
from flask import jsonify
from .base import InvoiceResponse, TxStatus, Wallet, PaymentResponse from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
import json
class OpenNodeWallet(Wallet): class OpenNodeWallet(Wallet):
"""https://api.lightning.community/rest/index.html#lnd-rest-api-reference""" """https://api.lightning.community/rest/index.html#lnd-rest-api-reference"""
@ -20,33 +20,24 @@ class OpenNodeWallet(Wallet):
) )
if r.ok: if r.ok:
data = r.json() data = r.json()
payment_hash, payment_request = data['data']['id'], data["data"]["lightning_invoice"]["payreq"] payment_hash, payment_request = data["data"]["id"], data["data"]["lightning_invoice"]["payreq"]
return InvoiceResponse(r, payment_hash, payment_request) return InvoiceResponse(r, payment_hash, payment_request)
def pay_invoice(self, bolt11: str) -> PaymentResponse: def pay_invoice(self, bolt11: str) -> PaymentResponse:
r = post(url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln", "address": bolt11})
r = post(url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln","address": bolt11}) return PaymentResponse(r, not r.ok)
return PaymentResponse(r)
def get_invoice_status(self, payment_hash: str) -> TxStatus: def get_invoice_status(self, payment_hash: str) -> TxStatus:
r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice) r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice)
data = r.json()
print(data)
print(f"{self.endpoint}/v1/charge/{payment_hash} {self.auth_invoice}")
if not r.ok: if not r.ok:
return TxStatus(r, None) return TxStatus(r, None)
statuses = {"processing": None, "paid": True, "unpaid": False} statuses = {"processing": None, "paid": True, "unpaid": False}
return TxStatus(r, statuses[r.json()["data"]["status"]]) return TxStatus(r, statuses[r.json()["data"]["status"]])
def get_payment_status(self, payment_hash: str) -> TxStatus: def get_payment_status(self, payment_hash: str) -> TxStatus:
r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin) r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin)
if not r.ok: if not r.ok:
@ -54,6 +45,3 @@ class OpenNodeWallet(Wallet):
statuses = {"pending": None, "confirmed": True, "error": False, "failed": False} statuses = {"pending": None, "confirmed": True, "error": False, "failed": False}
return TxStatus(r, statuses[r.json()["data"]["status"]]) return TxStatus(r, statuses[r.json()["data"]["status"]])

Loading…
Cancel
Save