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 8, 2024
1 parent cfe4e91 commit 05e6a82
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 5 deletions.
5 changes: 0 additions & 5 deletions cashu/nuts/nut04/nut04.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ type State int
const (
Unpaid State = iota
Paid
Pending
Issued
Unknown
)
Expand All @@ -25,8 +24,6 @@ func (state State) String() string {
return "UNPAID"
case Paid:
return "PAID"
case Pending:
return "PENDING"
case Issued:
return "ISSUED"
default:
Expand All @@ -40,8 +37,6 @@ func StringToState(state string) State {
return Unpaid
case "PAID":
return Paid
case "PENDING":
return Pending
case "ISSUED":
return Issued
}
Expand Down
81 changes: 81 additions & 0 deletions mint/mint_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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/cashu/nuts/nut05"
"github.com/elnosh/gonuts/crypto"
"github.com/elnosh/gonuts/mint"
Expand Down Expand Up @@ -112,6 +113,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
3 changes: 3 additions & 0 deletions wallet/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,9 @@ func (w *Wallet) MintTokens(quoteId string) (cashu.Proofs, error) {
if !mintQuote.Paid || mintQuote.State == nut04.Unpaid {
return nil, errors.New("invoice not paid")
}
if mintQuote.State == nut04.Issued {
return nil, errors.New("quote has already been issued")
}

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

0 comments on commit 05e6a82

Please sign in to comment.