refactor: "payments" is the name, and API updates
This commit is contained in:
+10
-5
@@ -13,11 +13,16 @@ class PaymentResponse(NamedTuple):
|
||||
raw_response: Response
|
||||
failed: bool = False
|
||||
fee_msat: int = 0
|
||||
error_message: Optional[str] = None
|
||||
|
||||
|
||||
class TxStatus(NamedTuple):
|
||||
class PaymentStatus(NamedTuple):
|
||||
raw_response: Response
|
||||
settled: Optional[bool] = None
|
||||
paid: Optional[bool] = None
|
||||
|
||||
@property
|
||||
def pending(self) -> bool:
|
||||
return self.paid is not True
|
||||
|
||||
|
||||
class Wallet(ABC):
|
||||
@@ -26,13 +31,13 @@ class Wallet(ABC):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def pay_invoice(self, bolt11: str) -> Response:
|
||||
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
|
||||
pass
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
from requests import get, post
|
||||
from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
|
||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||
|
||||
|
||||
class LndWallet(Wallet):
|
||||
@@ -41,15 +41,15 @@ class LndWallet(Wallet):
|
||||
)
|
||||
return PaymentResponse(r, not r.ok)
|
||||
|
||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = get(url=f"{self.endpoint}/v1/invoice/{payment_hash}", headers=self.auth_read, verify=False)
|
||||
|
||||
if not r.ok or "settled" not in r.json():
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
return TxStatus(r, r.json()["settled"])
|
||||
return PaymentStatus(r, r.json()["settled"])
|
||||
|
||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = get(
|
||||
url=f"{self.endpoint}/v1/payments",
|
||||
headers=self.auth_admin,
|
||||
@@ -58,11 +58,11 @@ class LndWallet(Wallet):
|
||||
)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
payments = [p for p in r.json()["payments"] if p["payment_hash"] == payment_hash]
|
||||
payment = payments[0] if payments else None
|
||||
|
||||
# check payment.status: https://api.lightning.community/rest/index.html?python#peersynctype
|
||||
statuses = {"UNKNOWN": None, "IN_FLIGHT": None, "SUCCEEDED": True, "FAILED": False}
|
||||
return TxStatus(r, statuses[payment["status"]] if payment else None)
|
||||
return PaymentStatus(r, statuses[payment["status"]] if payment else None)
|
||||
|
||||
+7
-13
@@ -1,6 +1,6 @@
|
||||
from requests import get, post
|
||||
|
||||
from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
|
||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||
|
||||
|
||||
class LNPayWallet(Wallet):
|
||||
@@ -37,20 +37,14 @@ class LNPayWallet(Wallet):
|
||||
|
||||
return PaymentResponse(r, not r.ok)
|
||||
|
||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
|
||||
return self.get_payment_status(payment_hash)
|
||||
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = get(url=f"{self.endpoint}/user/lntx/{payment_hash}", headers=self.auth_api)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
statuses = {0: None, 1: True, -1: False}
|
||||
return TxStatus(r, statuses[r.json()["settled"]])
|
||||
|
||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||
r = get(url=f"{self.endpoint}/user/lntx/{payment_hash}", headers=self.auth_api)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
|
||||
statuses = {0: None, 1: True, -1: False}
|
||||
return TxStatus(r, statuses[r.json()["settled"]])
|
||||
return PaymentStatus(r, statuses[r.json()["settled"]])
|
||||
|
||||
+13
-12
@@ -1,6 +1,6 @@
|
||||
from requests import post
|
||||
|
||||
from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
|
||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||
|
||||
|
||||
class LntxbotWallet(Wallet):
|
||||
@@ -23,38 +23,39 @@ class LntxbotWallet(Wallet):
|
||||
|
||||
def pay_invoice(self, bolt11: str) -> PaymentResponse:
|
||||
r = post(url=f"{self.endpoint}/payinvoice", headers=self.auth_admin, json={"invoice": bolt11})
|
||||
failed, fee_msat = not r.ok, 0
|
||||
failed, fee_msat, error_message = not r.ok, 0, None
|
||||
|
||||
if r.ok:
|
||||
data = r.json()
|
||||
if "error" in data and data["error"]:
|
||||
failed = True
|
||||
error_message = data["message"]
|
||||
elif "fee_msat" in data:
|
||||
fee_msat = data["fee_msat"]
|
||||
|
||||
return PaymentResponse(r, failed, fee_msat)
|
||||
return PaymentResponse(r, failed, fee_msat, error_message)
|
||||
|
||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = post(url=f"{self.endpoint}/invoicestatus/{payment_hash}?wait=false", headers=self.auth_invoice)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
data = r.json()
|
||||
|
||||
if "error" in data:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
if "preimage" not in data or not data["preimage"]:
|
||||
return TxStatus(r, False)
|
||||
return PaymentStatus(r, False)
|
||||
|
||||
return TxStatus(r, True)
|
||||
return PaymentStatus(r, True)
|
||||
|
||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = post(url=f"{self.endpoint}/paymentstatus/{payment_hash}", headers=self.auth_invoice)
|
||||
|
||||
if not r.ok or "error" in r.json():
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
statuses = {"complete": True, "failed": False, "unknown": None}
|
||||
return TxStatus(r, statuses[r.json().get("status", "unknown")])
|
||||
statuses = {"complete": True, "failed": False, "pending": None, "unknown": None}
|
||||
return PaymentStatus(r, statuses[r.json().get("status", "unknown")])
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from requests import get, post
|
||||
|
||||
from .base import InvoiceResponse, PaymentResponse, TxStatus, Wallet
|
||||
from .base import InvoiceResponse, PaymentResponse, PaymentStatus, Wallet
|
||||
|
||||
|
||||
class OpenNodeWallet(Wallet):
|
||||
@@ -28,20 +28,20 @@ class OpenNodeWallet(Wallet):
|
||||
r = post(url=f"{self.endpoint}/v2/withdrawals", headers=self.auth_admin, json={"type": "ln", "address": bolt11})
|
||||
return PaymentResponse(r, not r.ok)
|
||||
|
||||
def get_invoice_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_invoice_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = get(url=f"{self.endpoint}/v1/charge/{payment_hash}", headers=self.auth_invoice)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
statuses = {"processing": None, "paid": True, "unpaid": False}
|
||||
return TxStatus(r, statuses[r.json()["data"]["status"]])
|
||||
return PaymentStatus(r, statuses[r.json()["data"]["status"]])
|
||||
|
||||
def get_payment_status(self, payment_hash: str) -> TxStatus:
|
||||
def get_payment_status(self, payment_hash: str) -> PaymentStatus:
|
||||
r = get(url=f"{self.endpoint}/v1/withdrawal/{payment_hash}", headers=self.auth_admin)
|
||||
|
||||
if not r.ok:
|
||||
return TxStatus(r, None)
|
||||
return PaymentStatus(r, None)
|
||||
|
||||
statuses = {"pending": None, "confirmed": True, "error": False, "failed": False}
|
||||
return TxStatus(r, statuses[r.json()["data"]["status"]])
|
||||
return PaymentStatus(r, statuses[r.json()["data"]["status"]])
|
||||
|
||||
Reference in New Issue
Block a user