Skip to content

Commit

Permalink
Blink: timeout for LN probes (#444)
Browse files Browse the repository at this point in the history
* blink: timeout for ln probes

* make MINIMUM_FEE_MSAT a const

* format
  • Loading branch information
callebtc authored Feb 18, 2024
1 parent a3145a9 commit 1e26f23
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions cashu/lightning/blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@

# according to https://github.com/GaloyMoney/galoy/blob/7e79cc27304de9b9c2e7d7f4fdd3bac09df23aac/core/api/src/domain/bitcoin/index.ts#L59
BLINK_MAX_FEE_PERCENT = 0.5

DIRECTION_SEND = "SEND"
DIRECTION_RECEIVE = "RECEIVE"
PROBE_FEE_TIMEOUT_SEC = 1
MINIMUM_FEE_MSAT = 2000


class BlinkWallet(LightningBackend):
Expand Down Expand Up @@ -360,34 +363,40 @@ async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:
"variables": variables,
}

fees_response_msat = 0
try:
r = await self.client.post(
url=self.endpoint,
data=json.dumps(data),
timeout=None,
timeout=PROBE_FEE_TIMEOUT_SEC,
)
r.raise_for_status()
resp: dict = r.json()
if resp.get("data", {}).get("lnInvoiceFeeProbe", {}).get("errors"):
raise Exception(
resp["data"]["lnInvoiceFeeProbe"]["errors"][0].get("message")
or "Unknown error"
)

fees_response_msat = (
int(resp.get("data", {}).get("lnInvoiceFeeProbe", {}).get("amount"))
* 1000
)
except httpx.ReadTimeout:
pass
except Exception as e:
logger.error(f"Blink API error: {str(e)}")
raise e
resp: dict = r.json()
if resp.get("data", {}).get("lnInvoiceFeeProbe", {}).get("errors"):
raise Exception(
resp["data"]["lnInvoiceFeeProbe"]["errors"][0].get("message")
or "Unknown error"
)

invoice_obj = decode(bolt11)
assert invoice_obj.amount_msat, "invoice has no amount."

amount_msat = int(invoice_obj.amount_msat)

fees_response_msat = (
int(resp.get("data", {}).get("lnInvoiceFeeProbe", {}).get("amount")) * 1000
)
# we either take fee_msat_response or the BLINK_MAX_FEE_PERCENT, whichever is higher
# we take the highest: fee_msat_response, or BLINK_MAX_FEE_PERCENT, or 2000 msat
fees_msat = max(
fees_response_msat,
max(math.ceil(amount_msat / 100 * BLINK_MAX_FEE_PERCENT), 1000),
max(math.ceil(amount_msat / 100 * BLINK_MAX_FEE_PERCENT), MINIMUM_FEE_MSAT),
)

fees = Amount(unit=Unit.msat, amount=fees_msat)
Expand Down

0 comments on commit 1e26f23

Please sign in to comment.