2019-11-11 18:24:33 +03:00
|
|
|
package lnrpc
|
|
|
|
|
|
|
|
import (
|
2019-11-11 18:44:08 +03:00
|
|
|
"errors"
|
|
|
|
|
2019-11-11 18:24:33 +03:00
|
|
|
"github.com/btcsuite/btcutil"
|
|
|
|
"github.com/lightningnetwork/lnd/lnwire"
|
|
|
|
)
|
|
|
|
|
2019-11-11 18:44:08 +03:00
|
|
|
var (
|
|
|
|
// ErrSatMsatMutualExclusive is returned when both a sat and an msat
|
|
|
|
// amount are set.
|
|
|
|
ErrSatMsatMutualExclusive = errors.New(
|
|
|
|
"sat and msat arguments are mutually exclusive",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2019-11-11 18:24:33 +03:00
|
|
|
// 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()),
|
|
|
|
)
|
|
|
|
|
2019-11-11 18:44:08 +03:00
|
|
|
case *FeeLimit_FixedMsat:
|
|
|
|
return lnwire.MilliSatoshi(feeLimit.GetFixedMsat())
|
|
|
|
|
2019-11-11 18:24:33 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2019-11-11 18:44:08 +03:00
|
|
|
|
|
|
|
// UnmarshallAmt returns a strong msat type for a sat/msat pair of rpc fields.
|
|
|
|
func UnmarshallAmt(amtSat, amtMsat int64) (lnwire.MilliSatoshi, error) {
|
|
|
|
if amtSat != 0 && amtMsat != 0 {
|
|
|
|
return 0, ErrSatMsatMutualExclusive
|
|
|
|
}
|
|
|
|
|
|
|
|
if amtSat != 0 {
|
|
|
|
return lnwire.NewMSatFromSatoshis(btcutil.Amount(amtSat)), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return lnwire.MilliSatoshi(amtMsat), nil
|
|
|
|
}
|