Wallets refactor (#1729)

* feat: cleanup function for wallet

* update eclair implementation

* update lnd implementation

* update lnbits implementation

* update lnpay implementation

* update lnbits implementation

* update opennode implementation

* update spark implementation

* use base_url for clients

* fix lnpay

* fix opennode

* fix lntips

* test real invoice creation

* add small delay to test

* test paid invoice stream

* fix lnbits

* fix lndrest

* fix spark

fix spark

* check node balance in test

* increase balance check delay

* check balance in pay test aswell

* make sure get_payment_status is called

* fix lndrest

* revert unnecessary changes
This commit is contained in:
jackstar12
2023-06-19 12:12:00 +02:00
committed by GitHub
parent 49411e58cc
commit e6499104c0
11 changed files with 259 additions and 262 deletions
+20 -35
View File
@@ -29,17 +29,18 @@ class LNbitsWallet(Wallet):
if not self.endpoint or not key:
raise Exception("cannot initialize lnbits wallet")
self.key = {"X-Api-Key": key}
self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.key)
async def cleanup(self):
await self.client.aclose()
async def status(self) -> StatusResponse:
async with httpx.AsyncClient() as client:
try:
r = await client.get(
url=f"{self.endpoint}/api/v1/wallet", headers=self.key, timeout=15
)
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
r = await self.client.get(url="/api/v1/wallet", timeout=15)
except Exception as exc:
return StatusResponse(
f"Failed to connect to {self.endpoint} due to: {exc}", 0
)
try:
data = r.json()
@@ -69,10 +70,7 @@ class LNbitsWallet(Wallet):
if unhashed_description:
data["unhashed_description"] = unhashed_description.hex()
async with httpx.AsyncClient() as client:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments", headers=self.key, json=data
)
r = await self.client.post(url="/api/v1/payments", json=data)
ok, checking_id, payment_request, error_message = (
not r.is_error,
None,
@@ -89,20 +87,12 @@ class LNbitsWallet(Wallet):
return InvoiceResponse(ok, checking_id, payment_request, error_message)
async def pay_invoice(self, bolt11: str, fee_limit_msat: int) -> PaymentResponse:
async with httpx.AsyncClient() as client:
r = await client.post(
url=f"{self.endpoint}/api/v1/payments",
headers=self.key,
json={"out": True, "bolt11": bolt11},
timeout=None,
)
ok, checking_id, _, _, error_message = (
not r.is_error,
None,
None,
None,
None,
r = await self.client.post(
url="/api/v1/payments",
json={"out": True, "bolt11": bolt11},
timeout=None,
)
ok = not r.is_error
if r.is_error:
error_message = r.json()["detail"]
@@ -118,11 +108,9 @@ class LNbitsWallet(Wallet):
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
try:
async with httpx.AsyncClient() as client:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}",
headers=self.key,
)
r = await self.client.get(
url=f"/api/v1/payments/{checking_id}",
)
if r.is_error:
return PaymentStatus(None)
return PaymentStatus(r.json()["paid"])
@@ -130,10 +118,7 @@ class LNbitsWallet(Wallet):
return PaymentStatus(None)
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
async with httpx.AsyncClient() as client:
r = await client.get(
url=f"{self.endpoint}/api/v1/payments/{checking_id}", headers=self.key
)
r = await self.client.get(url=f"/api/v1/payments/{checking_id}")
if r.is_error:
return PaymentStatus(None)