Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wallet: handle pending proofs #68

Merged
merged 4 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cashu/cashu.go
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ const (

MeltQuotePendingErrCode CashuErrCode = 20005
MeltQuoteAlreadyPaidErrCode CashuErrCode = 20006
MeltQuoteErrCode CashuErrCode = 20008
LightningPaymentErrCode CashuErrCode = 20008
MeltQuoteErrCode CashuErrCode = 20009
)

var (
Expand Down
65 changes: 63 additions & 2 deletions cmd/nutw/nutw.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func main() {
sendCmd,
receiveCmd,
payCmd,
quotesCmd,
p2pkLockCmd,
mnemonicCmd,
restoreCmd,
Expand Down Expand Up @@ -144,6 +145,11 @@ func getBalance(ctx *cli.Context) error {
}

fmt.Printf("\nTotal balance: %v sats\n", totalBalance)

pendingBalance := nutw.PendingBalance()
if pendingBalance > 0 {
fmt.Printf("Pending balance: %v sats\n", pendingBalance)
}
return nil
}

Expand Down Expand Up @@ -404,10 +410,65 @@ func pay(ctx *cli.Context) error {
printErr(err)
}

if meltResponse.State == nut05.Paid {
switch meltResponse.State {
case nut05.Paid:
fmt.Printf("Invoice paid sucessfully. Preimage: %v\n", meltResponse.Preimage)
case nut05.Pending:
fmt.Println("payment is pending")
case nut05.Unpaid:
fmt.Println("mint could not pay invoice")
}

return nil
}

const (
checkFlag = "check"
)

var quotesCmd = &cli.Command{
Name: "quotes",
Usage: "list and check status of pending melt quotes",
Before: setupWallet,
Flags: []cli.Flag{
&cli.StringFlag{
Name: checkFlag,
Usage: "check state of quote",
},
},
Action: quotes,
}

func quotes(ctx *cli.Context) error {
pendingQuotes := nutw.GetPendingMeltQuotes()

if ctx.IsSet(checkFlag) {
quote := ctx.String(checkFlag)

quoteResponse, err := nutw.CheckMeltQuoteState(quote)
if err != nil {
printErr(err)
}

switch quoteResponse.State {
case nut05.Paid:
fmt.Printf("Invoice for quote '%v' was paid. Preimage: %v\n", quote, quoteResponse.Preimage)
case nut05.Pending:
fmt.Println("payment is still pending")
case nut05.Unpaid:
fmt.Println("quote was not paid")
}

return nil
}

if len(pendingQuotes) > 0 {
fmt.Println("Pending quotes: ")
for _, quote := range pendingQuotes {
fmt.Printf("ID: %v\n", quote)
}
} else {
fmt.Println("unable to pay invoice")
fmt.Println("no pending quotes")
}

return nil
Expand Down
4 changes: 4 additions & 0 deletions mint/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mint

import (
"time"

"github.com/elnosh/gonuts/cashu/nuts/nut06"
"github.com/elnosh/gonuts/mint/lightning"
)
Expand All @@ -23,6 +25,8 @@ type Config struct {
Limits MintLimits
LightningClient lightning.Client
LogLevel LogLevel
// NOTE: using this value for testing
MeltTimeout *time.Duration
}

type MintInfo struct {
Expand Down
10 changes: 8 additions & 2 deletions mint/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
type MintServer struct {
httpServer *http.Server
mint *Mint
// NOTE: using this value for testing
meltTimeout *time.Duration
}

func (ms *MintServer) Start() error {
Expand All @@ -46,7 +48,7 @@ func SetupMintServer(config Config) (*MintServer, error) {
return nil, err
}

mintServer := &MintServer{mint: mint}
mintServer := &MintServer{mint: mint, meltTimeout: config.MeltTimeout}
err = mintServer.setupHttpServer(config.Port)
if err != nil {
return nil, err
Expand Down Expand Up @@ -449,7 +451,11 @@ func (ms *MintServer) meltTokens(rw http.ResponseWriter, req *http.Request) {
return
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute*1)
timeout := time.Minute * 1
if ms.meltTimeout != nil {
timeout = *ms.meltTimeout
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

meltQuote, err := ms.mint.MeltTokens(ctx, method, meltTokensRequest.Quote, meltTokensRequest.Inputs)
Expand Down
2 changes: 2 additions & 0 deletions testutils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ func mintConfig(
return nil, fmt.Errorf("error setting LND client: %v", err)
}

timeout := time.Second * 2
mintConfig := &mint.Config{
DerivationPathIdx: 0,
Port: port,
Expand All @@ -247,6 +248,7 @@ func mintConfig(
Limits: limits,
LightningClient: lndClient,
LogLevel: mint.Disable,
MeltTimeout: &timeout,
}

return mintConfig, nil
Expand Down
Loading
Loading