Skip to content

Commit

Permalink
tests for nut04 state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Jul 2, 2024
1 parent 3db134a commit c197e5e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
81 changes: 81 additions & 0 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

btcdocker "github.com/elnosh/btc-docker-test"
"github.com/elnosh/gonuts/cashu"
"github.com/elnosh/gonuts/cashu/nuts/nut04"
"github.com/elnosh/gonuts/crypto"
"github.com/elnosh/gonuts/mint"
"github.com/elnosh/gonuts/testutils"
Expand Down Expand Up @@ -110,6 +111,86 @@ func TestRequestMintQuote(t *testing.T) {
}
}

func TestMintQuoteState(t *testing.T) {
var mintAmount uint64 = 42000
mintQuoteResponse, err := testMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT)
if err != nil {
t.Fatalf("error requesting mint quote: %v", err)
}

var keyset crypto.Keyset
for _, k := range testMint.ActiveKeysets {
keyset = k
break
}

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

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

// test quote state before paying invoice
quoteStateResponse, err := testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote)
if err != nil {
t.Fatalf("unexpected error getting quote state: %v", err)
}
if quoteStateResponse.Paid {
t.Fatalf("expected quote.Paid '%v' but got '%v' instead", false, quoteStateResponse.Paid)
}
if quoteStateResponse.State != nut04.Unpaid {
t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Unpaid.String(), quoteStateResponse.State.String())
}

//pay invoice
sendPaymentRequest := lnrpc.SendRequest{
PaymentRequest: mintQuoteResponse.Request,
}
response, _ := lnd2.Client.SendPaymentSync(ctx, &sendPaymentRequest)
if len(response.PaymentError) > 0 {
t.Fatalf("error paying invoice: %v", response.PaymentError)
}

// test quote state after paying invoice
quoteStateResponse, err = testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote)
if err != nil {
t.Fatalf("unexpected error getting quote state: %v", err)
}
if !quoteStateResponse.Paid {
t.Fatalf("expected quote.Paid '%v' but got '%v' instead", true, quoteStateResponse.Paid)
}
if quoteStateResponse.State != nut04.Paid {
t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Paid.String(), quoteStateResponse.State.String())
}

blindedMessages, _, _, err := testutils.CreateBlindedMessages(mintAmount, keyset)

// mint tokens
_, err = testMint.MintTokens(testutils.BOLT11_METHOD, mintQuoteResponse.Quote, blindedMessages)
if err != nil {
t.Fatalf("got unexpected error minting tokens: %v", err)
}

// test quote state after minting tokens
quoteStateResponse, err = testMint.GetMintQuoteState(testutils.BOLT11_METHOD, mintQuoteResponse.Quote)
if err != nil {
t.Fatalf("unexpected error getting quote state: %v", err)
}
if !quoteStateResponse.Paid {
t.Fatalf("expected quote.Paid '%v' but got '%v' instead", true, quoteStateResponse.Paid)
}
if quoteStateResponse.State != nut04.Issued {
t.Fatalf("expected quote state '%v' but got '%v' instead", nut04.Issued.String(), quoteStateResponse.State.String())
}

}

func TestMintTokens(t *testing.T) {
var mintAmount uint64 = 42000
mintQuoteResponse, err := testMint.RequestMintQuote(testutils.BOLT11_METHOD, mintAmount, testutils.SAT_UNIT)
Expand Down
2 changes: 2 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,8 @@ func (w *Wallet) MintTokens(quoteId string) (cashu.Proofs, error) {
return nil, errors.New("invoice not paid")
}

// TODO: do not try mint if state == ISSUED

invoice, err := w.GetInvoiceByPaymentRequest(mintQuote.Request)
if err != nil {
return nil, err
Expand Down

0 comments on commit c197e5e

Please sign in to comment.