From d8e6bd240bcd6e274ab904a1655ed59987471843 Mon Sep 17 00:00:00 2001 From: elnosh Date: Sat, 1 Jun 2024 12:21:29 -0500 Subject: [PATCH] calc fee reserve method --- mint/lightning/lightning.go | 2 +- mint/lightning/lnd.go | 39 ++++++------------------------------- mint/mint.go | 13 +++++-------- 3 files changed, 12 insertions(+), 42 deletions(-) diff --git a/mint/lightning/lightning.go b/mint/lightning/lightning.go index f0c7b71..2ac5185 100644 --- a/mint/lightning/lightning.go +++ b/mint/lightning/lightning.go @@ -13,7 +13,7 @@ const ( type Client interface { CreateInvoice(amount uint64) (Invoice, error) InvoiceSettled(hash string) (bool, error) - FeeReserve(request string) (uint64, uint64, error) + FeeReserve(amount uint64) uint64 SendPayment(request string) (string, error) } diff --git a/mint/lightning/lnd.go b/mint/lightning/lnd.go index 52b6664..1b71058 100644 --- a/mint/lightning/lnd.go +++ b/mint/lightning/lnd.go @@ -9,9 +9,9 @@ import ( "encoding/json" "errors" "fmt" + "math" "net/http" "os" - "strconv" "time" ) @@ -22,8 +22,8 @@ const ( ) const ( - InvoiceExpiryMins = 10 - FeePercent = 1 + InvoiceExpiryMins = 10 + FeePercent float64 = 0.01 ) type LndClient struct { @@ -152,36 +152,9 @@ func (lnd *LndClient) InvoiceSettled(hash string) (bool, error) { return settled == "SETTLED", nil } -func (lnd *LndClient) FeeReserve(request string) (uint64, uint64, error) { - url := lnd.host + "/v1/payreq/" + request - - req, err := http.NewRequest(http.MethodGet, url, nil) - if err != nil { - return 0, 0, err - } - req.Header.Add("Grpc-Metadata-macaroon", lnd.macaroon) - - resp, err := lnd.client.Do(req) - if err != nil { - return 0, 0, err - } - defer resp.Body.Close() - - var res map[string]any - json.NewDecoder(resp.Body).Decode(&res) - - var satAmount int64 - if amt, ok := res["num_satoshis"]; !ok { - return 0, 0, errors.New("invoice has no amount") - } else { - satAmount, err = strconv.ParseInt(amt.(string), 10, 64) - if err != nil { - return 0, 0, fmt.Errorf("invalid amount: %v", err) - } - - } - - return uint64(satAmount), uint64(satAmount * FeePercent / 100), nil +func (lnd *LndClient) FeeReserve(amount uint64) uint64 { + fee := math.Ceil(float64(amount) * FeePercent) + return uint64(fee) } type SendPaymentResponse struct { diff --git a/mint/mint.go b/mint/mint.go index c1949a4..84847bd 100644 --- a/mint/mint.go +++ b/mint/mint.go @@ -254,7 +254,7 @@ func (m *Mint) MeltRequest(method, request, unit string) (MeltQuote, error) { } // check invoice passed is valid - _, err := decodepay.Decodepay(request) + bolt11, err := decodepay.Decodepay(request) if err != nil { msg := fmt.Sprintf("invalid invoice: %v", err) return MeltQuote{}, cashu.BuildCashuError(msg, cashu.InvoiceErrCode) @@ -268,18 +268,15 @@ func (m *Mint) MeltRequest(method, request, unit string) (MeltQuote, error) { } hash := sha256.Sum256(randomBytes) - // Fee reserved that is required by the mint - amount, fee, err := m.LightningClient.FeeReserve(request) - if err != nil { - msg := fmt.Sprintf("melt request error: %v", err) - return MeltQuote{}, cashu.BuildCashuError(msg, cashu.StandardErrCode) - } + satAmount := uint64(bolt11.MSatoshi) / 1000 + // Fee reserve that is required by the mint + fee := m.LightningClient.FeeReserve(satAmount) expiry := time.Now().Add(time.Minute * QuoteExpiryMins).Unix() meltQuote := MeltQuote{ Id: hex.EncodeToString(hash[:]), InvoiceRequest: request, - Amount: amount, + Amount: satAmount, FeeReserve: fee, Paid: false, Expiry: expiry,