Skip to content

Commit

Permalink
Merge pull request #118 from elnosh/wallet-melt-fix
Browse files Browse the repository at this point in the history
wallet: identify lightning payment failed in melt
  • Loading branch information
elnosh authored Feb 14, 2025
2 parents 3667bdb + e190da0 commit fed5460
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
3 changes: 2 additions & 1 deletion cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,10 +475,10 @@ const (
MintingDisabledErrCode CashuErrCode = 20003
MintQuoteInvalidSigErrCode CashuErrCode = 20008

LightningPaymentErrCode CashuErrCode = 20004
MeltQuotePendingErrCode CashuErrCode = 20005
MeltQuoteAlreadyPaidErrCode CashuErrCode = 20006

//LightningPaymentErrCode CashuErrCode = 20008
MeltQuoteErrCode CashuErrCode = 20009
)

Expand All @@ -504,6 +504,7 @@ var (
DuplicateProofs = Error{Detail: "duplicate proofs", Code: InvalidProofErrCode}
QuoteNotExistErr = Error{Detail: "quote does not exist", Code: MeltQuoteErrCode}
QuotePending = Error{Detail: "quote is pending", Code: MeltQuotePendingErrCode}
LightningPaymentFailed = Error{Detail: "Lightning payment failed", Code: LightningPaymentErrCode}
MeltQuoteAlreadyPaid = Error{Detail: "quote already paid", Code: MeltQuoteAlreadyPaidErrCode}
MeltAmountExceededErr = Error{Detail: "max amount for melting exceeded", Code: AmountLimitExceeded}
MeltQuoteForRequestExists = Error{Detail: "melt quote for payment request already exists", Code: MeltQuoteErrCode}
Expand Down
22 changes: 15 additions & 7 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -913,14 +913,22 @@ func (w *Wallet) Melt(quoteId string) (*nut05.PostMeltQuoteBolt11Response, error
}
meltBolt11Response, err := client.PostMeltBolt11(mint.mintURL, meltBolt11Request)
if err != nil {
// if there was error with melt, remove proofs from pending and save them for use
if err := w.db.SaveProofs(proofs); err != nil {
return nil, fmt.Errorf("error storing proofs: %v", err)
}
if err := w.db.DeletePendingProofsByQuoteId(quote.QuoteId); err != nil {
return nil, fmt.Errorf("error removing pending proofs: %v", err)
if cashuErr, ok := err.(cashu.Error); ok {
if cashuErr.Code == cashu.LightningPaymentErrCode {
// only remove proofs from pending and save them for use
// if got specific error that payment failed
if err := w.db.SaveProofs(proofs); err != nil {
return nil, fmt.Errorf("error storing proofs: %v", err)
}
if err := w.db.DeletePendingProofsByQuoteId(quote.QuoteId); err != nil {
return nil, fmt.Errorf("error removing pending proofs: %v", err)
}
return nil, err
}
} else {
// for any other errors leave proofs as pending
return nil, fmt.Errorf("error doing melt request: %v. Proofs are pending", err)
}
return nil, err
}

switch meltBolt11Response.State {
Expand Down

0 comments on commit fed5460

Please sign in to comment.