Skip to content

Commit

Permalink
tests for nut05 quote state
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Jul 4, 2024
1 parent 0b26be9 commit 6d09435
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 19 deletions.
8 changes: 4 additions & 4 deletions cashu/nuts/nut05/nut05.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package nut05

import (
"encoding/json"
"errors"
//"errors"

"github.com/elnosh/gonuts/cashu"
)
Expand Down Expand Up @@ -98,9 +98,9 @@ func (quoteResponse *PostMeltQuoteBolt11Response) UnmarshalJSON(data []byte) err
quoteResponse.Amount = tempQuote.Amount
quoteResponse.FeeReserve = tempQuote.FeeReserve
state := StringToState(tempQuote.State)
if state == Unknown {
return errors.New("invalid state")
}
// if state == Unknown {
// return errors.New("invalid state")
// }
quoteResponse.State = state
quoteResponse.Paid = tempQuote.Paid
quoteResponse.Expiry = tempQuote.Expiry
Expand Down
2 changes: 2 additions & 0 deletions cmd/nutw/nutw.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,8 @@ func requestMint(amountStr string) error {
return err
}

fmt.Printf("quote id: %v\n\n", mintResponse.Quote)

fmt.Printf("invoice: %v\n\n", mintResponse.Request)
fmt.Println("after paying the invoice you can redeem the ecash using the --invoice flag")
return nil
Expand Down
14 changes: 0 additions & 14 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,20 +315,6 @@ func (m *Mint) GetMeltQuoteState(method, quoteId string) (MeltQuote, error) {
return MeltQuote{}, cashu.QuoteNotExistErr
}

// if quote not paid, check status of payment with backend
if meltQuote.State == nut05.Unpaid {
invoice, err := m.LightningClient.InvoiceStatus(meltQuote.PaymentHash)
if err != nil {
return MeltQuote{}, cashu.BuildCashuError(err.Error(), cashu.StandardErr.Code)
}
if invoice.Settled {
meltQuote.Paid = true
meltQuote.State = nut05.Paid
meltQuote.Preimage = invoice.Preimage
m.db.SaveMeltQuote(*meltQuote)
}
}

return *meltQuote, nil
}

Expand Down
70 changes: 70 additions & 0 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package mint_test

import (
"context"
"encoding/hex"
"errors"
"flag"
"log"
Expand All @@ -13,6 +14,7 @@ import (

btcdocker "github.com/elnosh/btc-docker-test"
"github.com/elnosh/gonuts/cashu"
"github.com/elnosh/gonuts/cashu/nuts/nut05"
"github.com/elnosh/gonuts/crypto"
"github.com/elnosh/gonuts/mint"
"github.com/elnosh/gonuts/testutils"
Expand Down Expand Up @@ -236,6 +238,74 @@ func TestMeltRequest(t *testing.T) {
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}
}

func TestMeltQuoteState(t *testing.T) {
invoice := lnrpc.Invoice{Value: 2000}
addInvoiceResponse, err := lnd2.Client.AddInvoice(ctx, &invoice)
if err != nil {
t.Fatalf("error creating invoice: %v", err)
}

lookupInvoice, err := lnd2.Client.LookupInvoice(ctx, &lnrpc.PaymentHash{RHash: addInvoiceResponse.RHash})
if err != nil {
t.Fatalf("error finding invoice: %v", err)
}

meltRequest, err := testMint.MeltRequest(testutils.BOLT11_METHOD, addInvoiceResponse.PaymentRequest, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("got unexpected error in melt request: %v", err)
}

// test invalid method
_, err = testMint.GetMeltQuoteState("strike", meltRequest.Id)
if !errors.Is(err, cashu.PaymentMethodNotSupportedErr) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.PaymentMethodNotSupportedErr, err)
}

// test invalid quote id
_, err = testMint.GetMeltQuoteState(testutils.BOLT11_METHOD, "quote1234")
if !errors.Is(err, cashu.QuoteNotExistErr) {
t.Fatalf("expected error '%v' but got '%v' instead", cashu.PaymentMethodNotSupportedErr, err)
}

// test before paying melt
meltQuote, err := testMint.GetMeltQuoteState(testutils.BOLT11_METHOD, meltRequest.Id)
if err != nil {
t.Fatalf("unexpected error getting melt quote state: %v", err)
}
if meltQuote.Paid {
t.Fatalf("expected quote.Paid '%v' but got '%v' instead", false, meltQuote.Paid)
}
if meltQuote.State != nut05.Unpaid {
t.Fatalf("expected quote state '%v' but got '%v' instead", nut05.Unpaid.String(), meltQuote.State.String())
}

// test state after melting
validProofs, err := testutils.GetValidProofsForAmount(6500, testMint, lnd2)
if err != nil {
t.Fatalf("error generating valid proofs: %v", err)
}

melt, err := testMint.MeltTokens(testutils.BOLT11_METHOD, meltQuote.Id, validProofs)
if err != nil {
t.Fatalf("got unexpected error in melt: %v", err)
}

meltQuote, err = testMint.GetMeltQuoteState(testutils.BOLT11_METHOD, meltRequest.Id)
if err != nil {
t.Fatalf("unexpected error getting melt quote state: %v", err)
}
if !melt.Paid {
t.Fatal("got unexpected unpaid melt quote")
}
if meltQuote.State != nut05.Paid {
t.Fatalf("expected quote state '%v' but got '%v' instead", nut05.Paid.String(), meltQuote.State.String())
}
preimageString := hex.EncodeToString(lookupInvoice.RPreimage)
if meltQuote.Preimage != preimageString {
t.Fatalf("expected quote preimage '%v' but got '%v' instead", preimageString, meltQuote.Preimage)
}

}

Expand Down
6 changes: 5 additions & 1 deletion wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,11 @@ func (w *Wallet) Melt(invoice string, mint string) (*nut05.PostMeltQuoteBolt11Re

// TODO: deprecate paid field and only use State
// TODO: check for PENDING as well
paid := meltBolt11Response.Paid && meltBolt11Response.State == nut05.Paid
paid := meltBolt11Response.Paid
// if state field is present, use that instead of paid
if meltBolt11Response.State != nut05.Unknown {
paid = meltBolt11Response.State == nut05.Paid
}
if !paid {
// save proofs if invoice was not paid
w.saveProofs(proofs)
Expand Down

0 comments on commit 6d09435

Please sign in to comment.