async wrap

This commit is contained in:
Ben Arc
2021-11-10 21:14:47 +00:00
parent e0bfb15e42
commit 860709f48c

View File

@@ -8,6 +8,7 @@ import random
from functools import partial, wraps from functools import partial, wraps
from os import getenv from os import getenv
from typing import AsyncGenerator, Optional from typing import AsyncGenerator, Optional
import time
from .base import ( from .base import (
InvoiceResponse, InvoiceResponse,
@@ -30,6 +31,10 @@ def async_wrap(func):
return run return run
def _pay_invoice(ln, bolt11):
return ln.pay(bolt11)
def _paid_invoices_stream(ln, last_pay_index): def _paid_invoices_stream(ln, last_pay_index):
return ln.waitanyinvoice(last_pay_index) return ln.waitanyinvoice(last_pay_index)
@@ -99,7 +104,8 @@ class CLightningWallet(Wallet):
async def pay_invoice(self, bolt11: str) -> PaymentResponse: async def pay_invoice(self, bolt11: str) -> PaymentResponse:
try: try:
r = self.ln.pay(bolt11) wrapped = async_wrap(_pay_invoice)
r = await wrapped(self.ln, bolt11)
except RpcError as exc: except RpcError as exc:
return PaymentResponse(False, None, 0, None, str(exc)) return PaymentResponse(False, None, 0, None, str(exc))
@@ -133,4 +139,4 @@ class CLightningWallet(Wallet):
wrapped = async_wrap(_paid_invoices_stream) wrapped = async_wrap(_paid_invoices_stream)
paid = await wrapped(self.ln, self.last_pay_index) paid = await wrapped(self.ln, self.last_pay_index)
self.last_pay_index = paid["pay_index"] self.last_pay_index = paid["pay_index"]
yield paid["label"] yield paid["label"]