From 2634c41393c9fb10fedd58f9ae485c9e306e71da Mon Sep 17 00:00:00 2001 From: callebtc <93376500+callebtc@users.noreply.github.com> Date: Sat, 21 Sep 2024 22:38:36 +0200 Subject: [PATCH] fix strike pending return --- cashu/lightning/lndrest.py | 3 +-- cashu/lightning/strike.py | 25 ++++++++----------------- cashu/wallet/cli/cli.py | 24 +++++++++++++++++++----- cashu/wallet/wallet.py | 10 ++++++++-- 4 files changed, 36 insertions(+), 26 deletions(-) diff --git a/cashu/lightning/lndrest.py b/cashu/lightning/lndrest.py index e92c46a2..36c2d605 100644 --- a/cashu/lightning/lndrest.py +++ b/cashu/lightning/lndrest.py @@ -355,8 +355,7 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus: if payment is not None and payment.get("status"): preimage = ( payment.get("payment_preimage") - if payment.get("payment_preimage") - != "0000000000000000000000000000000000000000000000000000000000000000" + if payment.get("payment_preimage") != "0" * 64 else None ) return PaymentStatus( diff --git a/cashu/lightning/strike.py b/cashu/lightning/strike.py index a71172df..d5591bc6 100644 --- a/cashu/lightning/strike.py +++ b/cashu/lightning/strike.py @@ -77,8 +77,8 @@ class StrikePaymentResponse(BaseModel): paymentId: str state: str result: str - completed: str - delivered: str + completed: Optional[str] + delivered: Optional[str] amount: StrikeAmount totalFee: StrikeAmount lightningNetworkFee: StrikeAmount @@ -284,21 +284,12 @@ async def get_payment_status(self, checking_id: str) -> PaymentStatus: try: r = await self.client.get(url=f"{self.endpoint}/v1/payments/{checking_id}") r.raise_for_status() - data = r.json() - if not data.get("state"): - return PaymentStatus( - result=PaymentResult.UNKNOWN, error_message="Unknown" - ) - if data["paid"]: - return PaymentStatus( - result=PaymentResult.SETTLED, - fee=Amount(self.unit, data["details"]["fee"]), - preimage=data["preimage"], - ) - else: - return PaymentStatus( - result=PaymentResult.FAILED, error_message="Failed" - ) + payment = StrikePaymentResponse.parse_obj(r.json()) + fee = self.fee_int(payment) + return PaymentStatus( + result=PAYMENT_RESULT_MAP[payment.state], + fee=Amount(self.unit, fee), + ) except Exception as e: return PaymentStatus(result=PaymentResult.UNKNOWN, error_message=str(e)) diff --git a/cashu/wallet/cli/cli.py b/cashu/wallet/cli/cli.py index d41c45cb..1f994d53 100644 --- a/cashu/wallet/cli/cli.py +++ b/cashu/wallet/cli/cli.py @@ -222,7 +222,7 @@ async def pay( print(" Error: Balance too low.") return send_proofs, fees = await wallet.select_to_send( - wallet.proofs, total_amount, include_fees=True + wallet.proofs, total_amount, include_fees=True, set_reserved=True ) try: melt_response = await wallet.melt( @@ -231,11 +231,25 @@ async def pay( except Exception as e: print(f" Error paying invoice: {str(e)}") return - print(" Invoice paid", end="", flush=True) - if melt_response.payment_preimage and melt_response.payment_preimage != "0" * 64: - print(f" (Preimage: {melt_response.payment_preimage}).") + if ( + melt_response.state + and MintQuoteState(melt_response.state) == MintQuoteState.paid + ): + print(" Invoice paid", end="", flush=True) + if ( + melt_response.payment_preimage + and melt_response.payment_preimage != "0" * 64 + ): + print(f" (Preimage: {melt_response.payment_preimage}).") + else: + print(".") + elif MintQuoteState(melt_response.state) == MintQuoteState.pending: + print(" Invoice pending.") + elif MintQuoteState(melt_response.state) == MintQuoteState.unpaid: + print(" Invoice unpaid.") else: - print(".") + print(" Error paying invoice.") + await print_balance(ctx) diff --git a/cashu/wallet/wallet.py b/cashu/wallet/wallet.py index c5cd9221..382de504 100644 --- a/cashu/wallet/wallet.py +++ b/cashu/wallet/wallet.py @@ -13,6 +13,7 @@ BlindedSignature, DLEQWallet, Invoice, + MeltQuoteState, Proof, Unit, WalletKeyset, @@ -758,13 +759,18 @@ async def melt( status = await super().melt(quote_id, proofs, change_outputs) # if payment fails - if not status.paid: - # remove the melt_id in proofs + if MeltQuoteState(status.state) == MeltQuoteState.unpaid: + # remove the melt_id in proofs and set reserved to False for p in proofs: p.melt_id = None + p.reserved = False await update_proof(p, melt_id="", db=self.db) raise Exception("could not pay invoice.") + elif MeltQuoteState(status.state) == MeltQuoteState.pending: + # payment is still pending + return status + # else: # invoice was paid successfully await self.invalidate(proofs)