lnrpc: extract fee limit calculation

This commit is contained in:
Joost Jager 2019-11-11 16:24:33 +01:00
parent 3b22540fc1
commit e0a3f803d5
No known key found for this signature in database
GPG Key ID: A61B9D4C393C59C7
3 changed files with 33 additions and 45 deletions

30
lnrpc/marshall_utils.go Normal file

@ -0,0 +1,30 @@
package lnrpc
import (
"github.com/btcsuite/btcutil"
"github.com/lightningnetwork/lnd/lnwire"
)
// CalculateFeeLimit returns the fee limit in millisatoshis. If a percentage
// based fee limit has been requested, we'll factor in the ratio provided with
// the amount of the payment.
func CalculateFeeLimit(feeLimit *FeeLimit,
amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
switch feeLimit.GetLimit().(type) {
case *FeeLimit_Fixed:
return lnwire.NewMSatFromSatoshis(
btcutil.Amount(feeLimit.GetFixed()),
)
case *FeeLimit_Percent:
return amount * lnwire.MilliSatoshi(feeLimit.GetPercent()) / 100
default:
// If a fee limit was not specified, we'll use the payment's
// amount as an upper bound in order to avoid payment attempts
// from incurring fees higher than the payment amount itself.
return amount
}
}

@ -136,7 +136,7 @@ func (r *RouterBackend) QueryRoutes(ctx context.Context,
} }
// Unmarshall restrictions from request. // Unmarshall restrictions from request.
feeLimit := calculateFeeLimit(in.FeeLimit, amtMSat) feeLimit := lnrpc.CalculateFeeLimit(in.FeeLimit, amtMSat)
ignoredNodes := make(map[route.Vertex]struct{}) ignoredNodes := make(map[route.Vertex]struct{})
for _, ignorePubKey := range in.IgnoredNodes { for _, ignorePubKey := range in.IgnoredNodes {
@ -308,27 +308,6 @@ func (r *RouterBackend) rpcEdgeToPair(e *lnrpc.EdgeLocator) (
return pair, nil return pair, nil
} }
// calculateFeeLimit returns the fee limit in millisatoshis. If a percentage
// based fee limit has been requested, we'll factor in the ratio provided with
// the amount of the payment.
func calculateFeeLimit(feeLimit *lnrpc.FeeLimit,
amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
switch feeLimit.GetLimit().(type) {
case *lnrpc.FeeLimit_Fixed:
return lnwire.NewMSatFromSatoshis(
btcutil.Amount(feeLimit.GetFixed()),
)
case *lnrpc.FeeLimit_Percent:
return amount * lnwire.MilliSatoshi(feeLimit.GetPercent()) / 100
default:
// If a fee limit was not specified, we'll use the payment's
// amount as an upper bound in order to avoid payment attempts
// from incurring fees higher than the payment amount itself.
return amount
}
}
// MarshallRoute marshalls an internal route to an rpc route struct. // MarshallRoute marshalls an internal route to an rpc route struct.
func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error) { func (r *RouterBackend) MarshallRoute(route *route.Route) (*lnrpc.Route, error) {
resp := &lnrpc.Route{ resp := &lnrpc.Route{

@ -2908,27 +2908,6 @@ type rpcPaymentRequest struct {
route *route.Route route *route.Route
} }
// calculateFeeLimit returns the fee limit in millisatoshis. If a percentage
// based fee limit has been requested, we'll factor in the ratio provided with
// the amount of the payment.
func calculateFeeLimit(feeLimit *lnrpc.FeeLimit,
amount lnwire.MilliSatoshi) lnwire.MilliSatoshi {
switch feeLimit.GetLimit().(type) {
case *lnrpc.FeeLimit_Fixed:
return lnwire.NewMSatFromSatoshis(
btcutil.Amount(feeLimit.GetFixed()),
)
case *lnrpc.FeeLimit_Percent:
return amount * lnwire.MilliSatoshi(feeLimit.GetPercent()) / 100
default:
// If a fee limit was not specified, we'll use the payment's
// amount as an upper bound in order to avoid payment attempts
// from incurring fees higher than the payment amount itself.
return amount
}
}
// SendPayment dispatches a bi-directional streaming RPC for sending payments // SendPayment dispatches a bi-directional streaming RPC for sending payments
// through the Lightning Network. A single RPC invocation creates a persistent // through the Lightning Network. A single RPC invocation creates a persistent
// bi-directional stream allowing clients to rapidly send payments through the // bi-directional stream allowing clients to rapidly send payments through the
@ -3114,7 +3093,7 @@ func extractPaymentIntent(rpcPayReq *rpcPaymentRequest) (rpcPaymentIntent, error
} }
// Calculate the fee limit that should be used for this payment. // Calculate the fee limit that should be used for this payment.
payIntent.feeLimit = calculateFeeLimit( payIntent.feeLimit = lnrpc.CalculateFeeLimit(
rpcPayReq.FeeLimit, payIntent.msat, rpcPayReq.FeeLimit, payIntent.msat,
) )
@ -3154,7 +3133,7 @@ func extractPaymentIntent(rpcPayReq *rpcPaymentRequest) (rpcPaymentIntent, error
) )
// Calculate the fee limit that should be used for this payment. // Calculate the fee limit that should be used for this payment.
payIntent.feeLimit = calculateFeeLimit( payIntent.feeLimit = lnrpc.CalculateFeeLimit(
rpcPayReq.FeeLimit, payIntent.msat, rpcPayReq.FeeLimit, payIntent.msat,
) )