diff --git a/boltcard.service b/boltcard.service index da84124..db82a32 100644 --- a/boltcard.service +++ b/boltcard.service @@ -43,7 +43,9 @@ Environment="LN_PORT=10009" Environment="LN_TLS_FILE=/home/ubuntu/boltcard/tls.cert" Environment="LN_MACAROON_FILE=/home/ubuntu/boltcard/SendPaymentV2.macaroon" -# FEE_LIMIT_SAT is the maximum lightning network fee to be paid +# The maximum lightning network fee to be paid in percent of the payment amount. +Environment="FEE_LIMIT_PERCENT=0.5" +# Acceptable flat fee in sats that can exceed the percentage limit. Environment="FEE_LIMIT_SAT=10" # email diff --git a/docs/FAQ.md b/docs/FAQ.md index be5fd93..8289c09 100644 --- a/docs/FAQ.md +++ b/docs/FAQ.md @@ -14,7 +14,7 @@ This will give you a new 128 bit random key as a 32 character hex value. This is due to your payment lightning node not finding a route to the merchant lightning node. It may help to open well funded channels to other well connected nodes. -It may also help to increase your maximum network fee in your service variables, **FEE_LIMIT_SAT** . +It may also help to increase your maximum network fee in your service variables, **FEE_LIMIT_SAT** / **FEE_LIMIT_PERCENT** . It can be useful to test paying invoices directly from your lightning node. > Why do my payments take so long ? diff --git a/lightning.go b/lightning.go index f4274f4..4d74a63 100644 --- a/lightning.go +++ b/lightning.go @@ -12,6 +12,7 @@ import ( "time" "crypto/sha256" + decodepay "github.com/fiatjaf/ln-decodepay" lnrpc "github.com/lightningnetwork/lnd/lnrpc" invoicesrpc "github.com/lightningnetwork/lnd/lnrpc/invoicesrpc" routerrpc "github.com/lightningnetwork/lnd/lnrpc/routerrpc" @@ -216,6 +217,21 @@ func pay_invoice(card_payment_id int, invoice string) { return } + fee_limit_percent_str := os.Getenv("FEE_LIMIT_PERCENT") + fee_limit_percent, err := strconv.ParseFloat(fee_limit_percent_str, 64) + if err != nil { + log.WithFields(log.Fields{"card_payment_id": card_payment_id}).Warn(err) + return + } + + bolt11, _ := decodepay.Decodepay(invoice) + invoice_msats := bolt11.MSatoshi + + fee_limit_product := int64((fee_limit_percent / 100) * (float64(invoice_msats) / 1000)) + if fee_limit_product > fee_limit_sat { + fee_limit_sat = fee_limit_product + } + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel()