Mint: Add clnrest.py Lightning backend (#551)

* log cln error

* return a string

* update corelightningrest to work with latest ver using rune

* fix mpp spec and backend support check

* refactor validation in ledger

* remove weird error

* fix mpp melt model

* corelightningrest.py: Added Multi-Mint payout support
lndrest.py: fix `quote.amount` is not always in sats + better checks

* small fix

* Fix quote.unit str2unit conversion + add missing imports

* settings enable mpp corelightning (default false)

* small fix

* fix `paid_invoice_stream`

* make format

* handle runes

* load rune

* rename to MINT_CORELIGHTNING_REST_RUNE

* try without cert

* port

* try except callback dispatcher

* clean up cln-rest streaming parser

* conftest: mint_corelightning_enable_mpp

* enable mpp in regtest.yaml

* fix error handling clnrest, remove lndrest changes

* CLNRest + CoreLightningRest

* clean up corelightningrest and get last index before starting the stream

* clean up

---------

Co-authored-by: callebtc <93376500+callebtc@users.noreply.github.com>
Co-authored-by: Richard Bensberg <r@coinbatsu.com>
This commit is contained in:
lollerfirst
2024-06-30 20:36:19 +02:00
committed by GitHub
parent 2739c3127a
commit ca272bc20f
11 changed files with 409 additions and 14 deletions

View File

@@ -56,7 +56,9 @@ class CoreLightningRestWallet(LightningBackend):
}
self.cert = settings.mint_corelightning_rest_cert or False
self.client = httpx.AsyncClient(verify=self.cert, headers=self.auth)
self.client = httpx.AsyncClient(
base_url=self.url, verify=self.cert, headers=self.auth
)
self.last_pay_index = 0
self.statuses = {
"paid": True,
@@ -72,7 +74,7 @@ class CoreLightningRestWallet(LightningBackend):
logger.warning(f"Error closing wallet connection: {e}")
async def status(self) -> StatusResponse:
r = await self.client.get(f"{self.url}/v1/listFunds", timeout=5)
r = await self.client.get("/v1/listFunds", timeout=5)
r.raise_for_status()
if r.is_error or "error" in r.json():
try:
@@ -124,7 +126,7 @@ class CoreLightningRestWallet(LightningBackend):
data["preimage"] = kwargs["preimage"]
r = await self.client.post(
f"{self.url}/v1/invoice/genInvoice",
"/v1/invoice/genInvoice",
data=data,
)
@@ -177,7 +179,7 @@ class CoreLightningRestWallet(LightningBackend):
)
fee_limit_percent = fee_limit_msat / invoice.amount_msat * 100
r = await self.client.post(
f"{self.url}/v1/pay",
"/v1/pay",
data={
"invoice": quote.request,
"maxfeepercent": f"{fee_limit_percent:.11}",
@@ -226,7 +228,7 @@ class CoreLightningRestWallet(LightningBackend):
async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
r = await self.client.get(
f"{self.url}/v1/invoice/listInvoices",
"/v1/invoice/listInvoices",
params={"payment_hash": checking_id},
)
try:
@@ -242,7 +244,7 @@ class CoreLightningRestWallet(LightningBackend):
async def get_payment_status(self, checking_id: str) -> PaymentStatus:
r = await self.client.get(
f"{self.url}/v1/pay/listPays",
"/v1/pay/listPays",
params={"payment_hash": checking_id},
)
try:
@@ -276,9 +278,17 @@ class CoreLightningRestWallet(LightningBackend):
return PaymentStatus(paid=None)
async def paid_invoices_stream(self) -> AsyncGenerator[str, None]:
# call listinvoices to determine the last pay_index
r = await self.client.get("/v1/invoice/listInvoices")
r.raise_for_status()
data = r.json()
if r.is_error or "error" in data:
raise Exception("error in cln response")
self.last_pay_index = data["invoices"][-1]["pay_index"]
while True:
try:
url = f"{self.url}/v1/invoice/waitAnyInvoice/{self.last_pay_index}"
url = f"/v1/invoice/waitAnyInvoice/{self.last_pay_index}"
async with self.client.stream("GET", url, timeout=None) as r:
async for line in r.aiter_lines():
inv = json.loads(line)
@@ -299,7 +309,7 @@ class CoreLightningRestWallet(LightningBackend):
# yield payment_hash
# hack to return payment_hash if the above shouldn't work
r = await self.client.get(
f"{self.url}/v1/invoice/listInvoices",
"/v1/invoice/listInvoices",
params={"label": inv["label"]},
)
paid_invoce = r.json()