Skip to content

Commit

Permalink
blink: fix fee esimation
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Feb 18, 2024
1 parent 48158cd commit 05c41b2
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 206 deletions.
43 changes: 10 additions & 33 deletions cashu/lightning/blink.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,7 @@ async def status(self) -> StatusResponse:
try:
r = await self.client.post(
url=self.endpoint,
data=(
'{"query":"query me { me { defaultAccount { wallets { id'
' walletCurrency balance }}}}", "variables":{}}'
),
data=('{"query":"query me { me { defaultAccount { wallets { id' ' walletCurrency balance }}}}", "variables":{}}'),
)
r.raise_for_status()
except Exception as exc:
Expand All @@ -71,9 +68,7 @@ async def status(self) -> StatusResponse:
resp: dict = r.json()
except Exception:
return StatusResponse(
error_message=(
f"Received invalid response from {self.endpoint}: {r.text}"
),
error_message=(f"Received invalid response from {self.endpoint}: {r.text}"),
balance=0,
)

Expand Down Expand Up @@ -137,9 +132,7 @@ async def create_invoice(

resp = r.json()
assert resp, "invalid response"
payment_request = resp["data"]["lnInvoiceCreateOnBehalfOfRecipient"]["invoice"][
"paymentRequest"
]
payment_request = resp["data"]["lnInvoiceCreateOnBehalfOfRecipient"]["invoice"]["paymentRequest"]
checking_id = payment_request

return InvoiceResponse(
Expand All @@ -148,9 +141,7 @@ async def create_invoice(
payment_request=payment_request,
)

async def pay_invoice(
self, quote: MeltQuote, fee_limit_msat: int
) -> PaymentResponse:
async def pay_invoice(self, quote: MeltQuote, fee_limit_msat: int) -> PaymentResponse:
variables = {
"input": {
"paymentRequest": quote.request,
Expand Down Expand Up @@ -185,9 +176,7 @@ async def pay_invoice(
return PaymentResponse(ok=False, error_message=str(e))

resp: dict = r.json()
paid = self.payment_execution_statuses[
resp["data"]["lnInvoicePaymentSend"]["status"]
]
paid = self.payment_execution_statuses[resp["data"]["lnInvoicePaymentSend"]["status"]]
fee = resp["data"]["lnInvoicePaymentSend"]["transaction"]["settlementFee"]
checking_id = quote.request

Expand Down Expand Up @@ -222,9 +211,7 @@ async def get_invoice_status(self, checking_id: str) -> PaymentStatus:
return PaymentStatus(paid=None)
resp: dict = r.json()
if resp["data"]["lnInvoicePaymentStatus"]["errors"]:
logger.error(
"Blink Error", resp["data"]["lnInvoicePaymentStatus"]["errors"]
)
logger.error("Blink Error", resp["data"]["lnInvoicePaymentStatus"]["errors"])
return PaymentStatus(paid=None)
paid = self.invoice_statuses[resp["data"]["lnInvoicePaymentStatus"]["status"]]
return PaymentStatus(paid=paid)
Expand Down Expand Up @@ -267,19 +254,11 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus:

resp: dict = r.json()
# no result found
if not resp["data"]["me"]["defaultAccount"]["walletById"][
"transactionsByPaymentHash"
]:
if not resp["data"]["me"]["defaultAccount"]["walletById"]["transactionsByPaymentHash"]:
return PaymentStatus(paid=None)

paid = self.payment_statuses[
resp["data"]["me"]["defaultAccount"]["walletById"][
"transactionsByPaymentHash"
][0]["status"]
]
fee = resp["data"]["me"]["defaultAccount"]["walletById"][
"transactionsByPaymentHash"
][0]["settlementFee"]
paid = self.payment_statuses[resp["data"]["me"]["defaultAccount"]["walletById"]["transactionsByPaymentHash"][0]["status"]]
fee = resp["data"]["me"]["defaultAccount"]["walletById"]["transactionsByPaymentHash"][0]["settlementFee"]

return PaymentStatus(
paid=paid,
Expand Down Expand Up @@ -326,9 +305,7 @@ async def get_payment_quote(self, bolt11: str) -> PaymentQuoteResponse:

fees_response_msat = int(resp["data"]["lnInvoiceFeeProbe"]["amount"]) * 1000
# we either take fee_msat_response or the BLINK_MAX_FEE_PERCENT, whichever is higher
fees_msat = max(
fees_response_msat, math.ceil(amount_msat * BLINK_MAX_FEE_PERCENT)
)
fees_msat = max(fees_response_msat, math.ceil(amount_msat / 100 * BLINK_MAX_FEE_PERCENT))

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

0 comments on commit 05c41b2

Please sign in to comment.