2019-10-30 19:43:05 -07:00
|
|
|
package chainfee
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
2022-02-23 14:48:00 +01:00
|
|
|
"github.com/btcsuite/btcd/btcutil"
|
2024-05-24 21:56:30 +08:00
|
|
|
"github.com/lightningnetwork/lnd/lntypes"
|
2019-10-30 19:43:05 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// FeePerKwFloor is the lowest fee rate in sat/kw that we should use for
|
2020-04-20 23:37:34 -07:00
|
|
|
// estimating transaction fees before signing.
|
2019-10-30 19:43:05 -07:00
|
|
|
FeePerKwFloor SatPerKWeight = 253
|
2020-04-20 23:37:34 -07:00
|
|
|
|
|
|
|
// AbsoluteFeePerKwFloor is the lowest fee rate in sat/kw of a
|
2024-03-07 14:05:47 +08:00
|
|
|
// transaction that we should ever _create_. This is the equivalent
|
2020-04-20 23:37:34 -07:00
|
|
|
// of 1 sat/byte in sat/kw.
|
|
|
|
AbsoluteFeePerKwFloor SatPerKWeight = 250
|
2019-10-30 19:43:05 -07:00
|
|
|
)
|
|
|
|
|
2023-07-26 10:48:00 +02:00
|
|
|
// SatPerVByte represents a fee rate in sat/vbyte.
|
|
|
|
type SatPerVByte btcutil.Amount
|
|
|
|
|
|
|
|
// FeePerKWeight converts the current fee rate from sat/vb to sat/kw.
|
|
|
|
func (s SatPerVByte) FeePerKWeight() SatPerKWeight {
|
|
|
|
return SatPerKWeight(s * 1000 / blockchain.WitnessScaleFactor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FeePerKVByte converts the current fee rate from sat/vb to sat/kvb.
|
|
|
|
func (s SatPerVByte) FeePerKVByte() SatPerKVByte {
|
|
|
|
return SatPerKVByte(s * 1000)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a human-readable string of the fee rate.
|
|
|
|
func (s SatPerVByte) String() string {
|
|
|
|
return fmt.Sprintf("%v sat/vb", int64(s))
|
|
|
|
}
|
|
|
|
|
2019-10-30 19:43:05 -07:00
|
|
|
// SatPerKVByte represents a fee rate in sat/kb.
|
|
|
|
type SatPerKVByte btcutil.Amount
|
|
|
|
|
|
|
|
// FeeForVSize calculates the fee resulting from this fee rate and the given
|
|
|
|
// vsize in vbytes.
|
2024-05-24 23:03:51 +08:00
|
|
|
func (s SatPerKVByte) FeeForVSize(vbytes lntypes.VByte) btcutil.Amount {
|
2019-10-30 19:43:05 -07:00
|
|
|
return btcutil.Amount(s) * btcutil.Amount(vbytes) / 1000
|
|
|
|
}
|
|
|
|
|
|
|
|
// FeePerKWeight converts the current fee rate from sat/kb to sat/kw.
|
|
|
|
func (s SatPerKVByte) FeePerKWeight() SatPerKWeight {
|
|
|
|
return SatPerKWeight(s / blockchain.WitnessScaleFactor)
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a human-readable string of the fee rate.
|
|
|
|
func (s SatPerKVByte) String() string {
|
2024-07-04 20:32:41 +08:00
|
|
|
return fmt.Sprintf("%v sat/kvb", int64(s))
|
2019-10-30 19:43:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SatPerKWeight represents a fee rate in sat/kw.
|
|
|
|
type SatPerKWeight btcutil.Amount
|
|
|
|
|
2024-02-29 13:18:23 +08:00
|
|
|
// NewSatPerKWeight creates a new fee rate in sat/kw.
|
2024-05-24 21:56:30 +08:00
|
|
|
func NewSatPerKWeight(fee btcutil.Amount, wu lntypes.WeightUnit) SatPerKWeight {
|
|
|
|
return SatPerKWeight(fee.MulF64(1000 / float64(wu)))
|
2024-02-29 13:18:23 +08:00
|
|
|
}
|
|
|
|
|
2019-10-30 19:43:05 -07:00
|
|
|
// FeeForWeight calculates the fee resulting from this fee rate and the given
|
|
|
|
// weight in weight units (wu).
|
2024-05-24 21:56:30 +08:00
|
|
|
func (s SatPerKWeight) FeeForWeight(wu lntypes.WeightUnit) btcutil.Amount {
|
2019-10-30 19:43:05 -07:00
|
|
|
// The resulting fee is rounded down, as specified in BOLT#03.
|
|
|
|
return btcutil.Amount(s) * btcutil.Amount(wu) / 1000
|
|
|
|
}
|
|
|
|
|
2024-05-24 22:49:20 +08:00
|
|
|
// FeeForVByte calculates the fee resulting from this fee rate and the given
|
|
|
|
// size in vbytes (vb).
|
2024-05-24 23:03:51 +08:00
|
|
|
func (s SatPerKWeight) FeeForVByte(vb lntypes.VByte) btcutil.Amount {
|
2024-05-24 22:49:20 +08:00
|
|
|
return s.FeePerKVByte().FeeForVSize(vb)
|
|
|
|
}
|
|
|
|
|
2019-10-30 19:43:05 -07:00
|
|
|
// FeePerKVByte converts the current fee rate from sat/kw to sat/kb.
|
|
|
|
func (s SatPerKWeight) FeePerKVByte() SatPerKVByte {
|
|
|
|
return SatPerKVByte(s * blockchain.WitnessScaleFactor)
|
|
|
|
}
|
|
|
|
|
2023-10-13 15:13:50 +08:00
|
|
|
// FeePerVByte converts the current fee rate from sat/kw to sat/vb.
|
|
|
|
func (s SatPerKWeight) FeePerVByte() SatPerVByte {
|
|
|
|
return SatPerVByte(s * blockchain.WitnessScaleFactor / 1000)
|
|
|
|
}
|
|
|
|
|
2019-10-30 19:43:05 -07:00
|
|
|
// String returns a human-readable string of the fee rate.
|
|
|
|
func (s SatPerKWeight) String() string {
|
|
|
|
return fmt.Sprintf("%v sat/kw", int64(s))
|
|
|
|
}
|