Skip to content

Commit

Permalink
calc fee reserve method
Browse files Browse the repository at this point in the history
  • Loading branch information
elnosh committed Jun 1, 2024
1 parent 5a98e3f commit d8e6bd2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 42 deletions.
2 changes: 1 addition & 1 deletion mint/lightning/lightning.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
39 changes: 6 additions & 33 deletions mint/lightning/lnd.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import (
"encoding/json"
"errors"
"fmt"
"math"
"net/http"
"os"
"strconv"
"time"
)

Expand All @@ -22,8 +22,8 @@ const (
)

const (
InvoiceExpiryMins = 10
FeePercent = 1
InvoiceExpiryMins = 10
FeePercent float64 = 0.01
)

type LndClient struct {
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 5 additions & 8 deletions mint/mint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
Expand Down

0 comments on commit d8e6bd2

Please sign in to comment.