Skip to content

Commit

Permalink
fix strike pending return
Browse files Browse the repository at this point in the history
  • Loading branch information
callebtc committed Sep 21, 2024
1 parent 3d75d22 commit 2634c41
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 26 deletions.
3 changes: 1 addition & 2 deletions cashu/lightning/lndrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
25 changes: 8 additions & 17 deletions cashu/lightning/strike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))

Expand Down
24 changes: 19 additions & 5 deletions cashu/wallet/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)


Expand Down
10 changes: 8 additions & 2 deletions cashu/wallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BlindedSignature,
DLEQWallet,
Invoice,
MeltQuoteState,
Proof,
Unit,
WalletKeyset,
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2634c41

Please sign in to comment.