2017-11-19 01:35:00 +03:00
|
|
|
package lnwallet
|
|
|
|
|
2017-11-23 09:11:02 +03:00
|
|
|
import (
|
2017-11-10 03:30:20 +03:00
|
|
|
"encoding/json"
|
|
|
|
|
2018-06-05 04:34:16 +03:00
|
|
|
"github.com/btcsuite/btcd/blockchain"
|
|
|
|
"github.com/btcsuite/btcd/rpcclient"
|
|
|
|
"github.com/btcsuite/btcutil"
|
2017-11-23 09:11:02 +03:00
|
|
|
)
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
const (
|
|
|
|
// FeePerKwFloor is the lowest fee rate in sat/kw that we should use for
|
|
|
|
// determining transaction fees.
|
|
|
|
FeePerKwFloor SatPerKWeight = 253
|
|
|
|
)
|
|
|
|
|
|
|
|
// SatPerKVByte represents a fee rate in sat/kb.
|
|
|
|
type SatPerKVByte btcutil.Amount
|
2018-02-13 16:18:46 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// FeeForVSize calculates the fee resulting from this fee rate and the given
|
|
|
|
// vsize in vbytes.
|
|
|
|
func (s SatPerKVByte) FeeForVSize(vbytes int64) btcutil.Amount {
|
|
|
|
return btcutil.Amount(s) * btcutil.Amount(vbytes) / 1000
|
2018-02-13 16:18:46 +03:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// FeePerKWeight converts the current fee rate from sat/kb to sat/kw.
|
|
|
|
func (s SatPerKVByte) FeePerKWeight() SatPerKWeight {
|
|
|
|
return SatPerKWeight(s / blockchain.WitnessScaleFactor)
|
2018-02-13 16:18:46 +03:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// SatPerKWeight represents a fee rate in sat/kw.
|
2018-02-13 16:18:46 +03:00
|
|
|
type SatPerKWeight btcutil.Amount
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// FeeForWeight calculates the fee resulting from this fee rate and the given
|
|
|
|
// weight in weight units (wu).
|
2018-02-13 16:18:46 +03:00
|
|
|
func (s SatPerKWeight) FeeForWeight(wu int64) btcutil.Amount {
|
|
|
|
// The resulting fee is rounded down, as specified in BOLT#03.
|
|
|
|
return btcutil.Amount(s) * btcutil.Amount(wu) / 1000
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// FeePerKVByte converts the current fee rate from sat/kw to sat/kb.
|
|
|
|
func (s SatPerKWeight) FeePerKVByte() SatPerKVByte {
|
|
|
|
return SatPerKVByte(s * blockchain.WitnessScaleFactor)
|
|
|
|
}
|
|
|
|
|
2017-11-19 01:35:00 +03:00
|
|
|
// FeeEstimator provides the ability to estimate on-chain transaction fees for
|
|
|
|
// various combinations of transaction sizes and desired confirmation time
|
|
|
|
// (measured by number of blocks).
|
|
|
|
type FeeEstimator interface {
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an
|
|
|
|
// initial confirmation and returns the estimated fee expressed in
|
|
|
|
// sat/kw.
|
|
|
|
EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error)
|
2017-11-23 09:11:21 +03:00
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
Start() error
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
Stop() error
|
2018-10-29 11:31:44 +03:00
|
|
|
|
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions
|
|
|
|
// to be relayed. This is also the basis for calculation of the dust
|
|
|
|
// limit.
|
|
|
|
RelayFeePerKW() SatPerKWeight
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// StaticFeeEstimator will return a static value for all fee calculation
|
|
|
|
// requests. It is designed to be replaced by a proper fee calculation
|
2018-12-18 11:02:27 +03:00
|
|
|
// implementation. The fees are not accessible directly, because changing them
|
|
|
|
// would not be thread safe.
|
2017-11-19 01:35:00 +03:00
|
|
|
type StaticFeeEstimator struct {
|
2018-12-18 11:02:27 +03:00
|
|
|
// feePerKW is the static fee rate in satoshis-per-vbyte that will be
|
2018-02-13 16:18:46 +03:00
|
|
|
// returned by this fee estimator.
|
2018-12-18 11:02:27 +03:00
|
|
|
feePerKW SatPerKWeight
|
2018-10-29 11:31:44 +03:00
|
|
|
|
2018-12-18 11:02:27 +03:00
|
|
|
// relayFee is the minimum fee rate required for transactions to be
|
2018-10-29 11:31:44 +03:00
|
|
|
// relayed.
|
2018-12-18 11:02:27 +03:00
|
|
|
relayFee SatPerKWeight
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewStaticFeeEstimator returns a new static fee estimator instance.
|
|
|
|
func NewStaticFeeEstimator(feePerKW,
|
|
|
|
relayFee SatPerKWeight) *StaticFeeEstimator {
|
|
|
|
|
|
|
|
return &StaticFeeEstimator{
|
|
|
|
feePerKW: feePerKW,
|
|
|
|
relayFee: relayFee,
|
|
|
|
}
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW will return a static value for fee calculations.
|
2017-11-23 09:11:42 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
2018-07-28 04:37:05 +03:00
|
|
|
func (e StaticFeeEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
2018-12-18 11:02:27 +03:00
|
|
|
return e.feePerKW, nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (e StaticFeeEstimator) RelayFeePerKW() SatPerKWeight {
|
2018-12-18 11:02:27 +03:00
|
|
|
return e.relayFee
|
2018-10-29 11:31:44 +03:00
|
|
|
}
|
|
|
|
|
2017-11-23 09:11:42 +03:00
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (e StaticFeeEstimator) Start() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (e StaticFeeEstimator) Stop() error {
|
|
|
|
return nil
|
2017-11-19 01:35:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that StaticFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*StaticFeeEstimator)(nil)
|
2017-11-23 09:26:07 +03:00
|
|
|
|
|
|
|
// BtcdFeeEstimator is an implementation of the FeeEstimator interface backed
|
|
|
|
// by the RPC interface of an active btcd node. This implementation will proxy
|
2017-12-18 05:40:05 +03:00
|
|
|
// any fee estimation requests to btcd's RPC interface.
|
2017-11-23 09:26:07 +03:00
|
|
|
type BtcdFeeEstimator struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
// fallbackFeePerKW is the fall back fee rate in sat/kw that is returned
|
|
|
|
// if the fee estimator does not yet have enough data to actually
|
|
|
|
// produce fee estimates.
|
|
|
|
fallbackFeePerKW SatPerKWeight
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// minFeePerKW is the minimum fee, in sat/kw, that we should enforce.
|
|
|
|
// This will be used as the default fee rate for a transaction when the
|
|
|
|
// estimated fee rate is too low to allow the transaction to propagate
|
|
|
|
// through the network.
|
|
|
|
minFeePerKW SatPerKWeight
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-23 09:26:07 +03:00
|
|
|
btcdConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBtcdFeeEstimator creates a new BtcdFeeEstimator given a fully populated
|
|
|
|
// rpc config that is able to successfully connect and authenticate with the
|
|
|
|
// btcd node, and also a fall back fee rate. The fallback fee rate is used in
|
|
|
|
// the occasion that the estimator has insufficient data, or returns zero for a
|
|
|
|
// fee estimate.
|
|
|
|
func NewBtcdFeeEstimator(rpcConfig rpcclient.ConnConfig,
|
2018-07-28 04:37:05 +03:00
|
|
|
fallBackFeeRate SatPerKWeight) (*BtcdFeeEstimator, error) {
|
2017-11-23 09:26:07 +03:00
|
|
|
|
|
|
|
rpcConfig.DisableConnectOnNew = true
|
|
|
|
rpcConfig.DisableAutoReconnect = false
|
|
|
|
chainConn, err := rpcclient.New(&rpcConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BtcdFeeEstimator{
|
2018-07-28 04:37:05 +03:00
|
|
|
fallbackFeePerKW: fallBackFeeRate,
|
|
|
|
btcdConn: chainConn,
|
2017-11-23 09:26:07 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) Start() error {
|
|
|
|
if err := b.btcdConn.Connect(20); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-05-14 20:45:39 +03:00
|
|
|
// Once the connection to the backend node has been established, we'll
|
|
|
|
// query it for its minimum relay fee.
|
|
|
|
info, err := b.btcdConn.GetInfo()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relayFee, err := btcutil.NewAmount(info.RelayFee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// The fee rate is expressed in sat/kb, so we'll manually convert it to
|
|
|
|
// our desired sat/kw rate.
|
|
|
|
minRelayFeePerKw := SatPerKVByte(relayFee).FeePerKWeight()
|
|
|
|
|
|
|
|
// By default, we'll use the backend node's minimum relay fee as the
|
|
|
|
// minimum fee rate we'll propose for transacations. However, if this
|
|
|
|
// happens to be lower than our fee floor, we'll enforce that instead.
|
|
|
|
b.minFeePerKW = minRelayFeePerKw
|
|
|
|
if b.minFeePerKW < FeePerKwFloor {
|
|
|
|
b.minFeePerKW = FeePerKwFloor
|
|
|
|
}
|
|
|
|
|
|
|
|
walletLog.Debugf("Using minimum fee rate of %v sat/kw",
|
|
|
|
int64(b.minFeePerKW))
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-23 09:26:07 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) Stop() error {
|
|
|
|
b.btcdConn.Shutdown()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns the estimated fee expressed in sat/kw.
|
2018-02-13 16:18:46 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
2018-07-28 04:37:05 +03:00
|
|
|
func (b *BtcdFeeEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
|
|
|
feeEstimate, err := b.fetchEstimate(numBlocks)
|
2017-11-23 09:26:07 +03:00
|
|
|
switch {
|
|
|
|
// If the estimator doesn't have enough data, or returns an error, then
|
|
|
|
// to return a proper value, then we'll return the default fall back
|
|
|
|
// fee rate.
|
|
|
|
case err != nil:
|
|
|
|
walletLog.Errorf("unable to query estimator: %v", err)
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
2018-07-28 04:37:05 +03:00
|
|
|
return b.fallbackFeePerKW, nil
|
2017-11-23 09:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BtcdFeeEstimator) RelayFeePerKW() SatPerKWeight {
|
|
|
|
return b.minFeePerKW
|
|
|
|
}
|
|
|
|
|
2018-04-18 05:03:27 +03:00
|
|
|
// fetchEstimate returns a fee estimate for a transaction to be confirmed in
|
2018-07-28 04:37:05 +03:00
|
|
|
// confTarget blocks. The estimate is returned in sat/kw.
|
|
|
|
func (b *BtcdFeeEstimator) fetchEstimate(confTarget uint32) (SatPerKWeight, error) {
|
2017-11-23 09:26:07 +03:00
|
|
|
// First, we'll fetch the estimate for our confirmation target.
|
|
|
|
btcPerKB, err := b.btcdConn.EstimateFee(int64(confTarget))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll convert the returned value to satoshis, as it's
|
|
|
|
// currently returned in BTC.
|
2017-11-26 22:35:25 +03:00
|
|
|
satPerKB, err := btcutil.NewAmount(btcPerKB)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Since we use fee rates in sat/kw internally, we'll convert the
|
|
|
|
// estimated fee rate from its sat/kb representation to sat/kw.
|
|
|
|
satPerKw := SatPerKVByte(satPerKB).FeePerKWeight()
|
|
|
|
|
|
|
|
// Finally, we'll enforce our fee floor.
|
|
|
|
if satPerKw < b.minFeePerKW {
|
|
|
|
walletLog.Debugf("Estimated fee rate of %v sat/kw is too low, "+
|
2018-09-21 05:20:01 +03:00
|
|
|
"using fee floor of %v sat/kw instead", satPerKw,
|
|
|
|
b.minFeePerKW)
|
2018-07-28 04:37:05 +03:00
|
|
|
satPerKw = b.minFeePerKW
|
2018-05-14 20:45:39 +03:00
|
|
|
}
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
walletLog.Debugf("Returning %v sat/kw for conf target of %v",
|
|
|
|
int64(satPerKw), confTarget)
|
2017-11-23 09:26:07 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
return satPerKw, nil
|
2017-11-23 09:26:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that BtcdFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*BtcdFeeEstimator)(nil)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
// BitcoindFeeEstimator is an implementation of the FeeEstimator interface
|
|
|
|
// backed by the RPC interface of an active bitcoind node. This implementation
|
2018-02-07 06:11:11 +03:00
|
|
|
// will proxy any fee estimation requests to bitcoind's RPC interface.
|
2017-11-10 03:30:20 +03:00
|
|
|
type BitcoindFeeEstimator struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
// fallbackFeePerKW is the fallback fee rate in sat/kw that is returned
|
|
|
|
// if the fee estimator does not yet have enough data to actually
|
|
|
|
// produce fee estimates.
|
|
|
|
fallbackFeePerKW SatPerKWeight
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// minFeePerKW is the minimum fee, in sat/kw, that we should enforce.
|
|
|
|
// This will be used as the default fee rate for a transaction when the
|
|
|
|
// estimated fee rate is too low to allow the transaction to propagate
|
|
|
|
// through the network.
|
|
|
|
minFeePerKW SatPerKWeight
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
bitcoindConn *rpcclient.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBitcoindFeeEstimator creates a new BitcoindFeeEstimator given a fully
|
|
|
|
// populated rpc config that is able to successfully connect and authenticate
|
|
|
|
// with the bitcoind node, and also a fall back fee rate. The fallback fee rate
|
|
|
|
// is used in the occasion that the estimator has insufficient data, or returns
|
|
|
|
// zero for a fee estimate.
|
|
|
|
func NewBitcoindFeeEstimator(rpcConfig rpcclient.ConnConfig,
|
2018-07-28 04:37:05 +03:00
|
|
|
fallBackFeeRate SatPerKWeight) (*BitcoindFeeEstimator, error) {
|
2017-11-10 03:30:20 +03:00
|
|
|
|
|
|
|
rpcConfig.DisableConnectOnNew = true
|
|
|
|
rpcConfig.DisableAutoReconnect = false
|
|
|
|
rpcConfig.DisableTLS = true
|
|
|
|
rpcConfig.HTTPPostMode = true
|
|
|
|
chainConn, err := rpcclient.New(&rpcConfig, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &BitcoindFeeEstimator{
|
2018-07-28 04:37:05 +03:00
|
|
|
fallbackFeePerKW: fallBackFeeRate,
|
|
|
|
bitcoindConn: chainConn,
|
2017-11-10 03:30:20 +03:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start signals the FeeEstimator to start any processes or goroutines
|
|
|
|
// it needs to perform its duty.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) Start() error {
|
2018-05-14 20:45:39 +03:00
|
|
|
// Once the connection to the backend node has been established, we'll
|
|
|
|
// query it for its minimum relay fee. Since the `getinfo` RPC has been
|
|
|
|
// deprecated for `bitcoind`, we'll need to send a `getnetworkinfo`
|
|
|
|
// command as a raw request.
|
|
|
|
resp, err := b.bitcoindConn.RawRequest("getnetworkinfo", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the response to retrieve the relay fee in sat/KB.
|
|
|
|
info := struct {
|
|
|
|
RelayFee float64 `json:"relayfee"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(resp, &info); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
relayFee, err := btcutil.NewAmount(info.RelayFee)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// The fee rate is expressed in sat/kb, so we'll manually convert it to
|
|
|
|
// our desired sat/kw rate.
|
|
|
|
minRelayFeePerKw := SatPerKVByte(relayFee).FeePerKWeight()
|
|
|
|
|
|
|
|
// By default, we'll use the backend node's minimum relay fee as the
|
|
|
|
// minimum fee rate we'll propose for transacations. However, if this
|
|
|
|
// happens to be lower than our fee floor, we'll enforce that instead.
|
|
|
|
b.minFeePerKW = minRelayFeePerKw
|
|
|
|
if b.minFeePerKW < FeePerKwFloor {
|
|
|
|
b.minFeePerKW = FeePerKwFloor
|
|
|
|
}
|
|
|
|
|
|
|
|
walletLog.Debugf("Using minimum fee rate of %v sat/kw",
|
|
|
|
int64(b.minFeePerKW))
|
2018-05-14 20:45:39 +03:00
|
|
|
|
2017-11-10 03:30:20 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops any spawned goroutines and cleans up the resources used
|
|
|
|
// by the fee estimator.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) Stop() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// EstimateFeePerKW takes in a target for the number of blocks until an initial
|
|
|
|
// confirmation and returns the estimated fee expressed in sat/kw.
|
2018-02-13 16:18:46 +03:00
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
2018-07-28 04:37:05 +03:00
|
|
|
func (b *BitcoindFeeEstimator) EstimateFeePerKW(numBlocks uint32) (SatPerKWeight, error) {
|
|
|
|
feeEstimate, err := b.fetchEstimate(numBlocks)
|
2017-11-10 03:30:20 +03:00
|
|
|
switch {
|
|
|
|
// If the estimator doesn't have enough data, or returns an error, then
|
|
|
|
// to return a proper value, then we'll return the default fall back
|
|
|
|
// fee rate.
|
|
|
|
case err != nil:
|
|
|
|
walletLog.Errorf("unable to query estimator: %v", err)
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case feeEstimate == 0:
|
2018-07-28 04:37:05 +03:00
|
|
|
return b.fallbackFeePerKW, nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return feeEstimate, nil
|
|
|
|
}
|
|
|
|
|
2018-10-29 11:31:44 +03:00
|
|
|
// RelayFeePerKW returns the minimum fee rate required for transactions to be
|
|
|
|
// relayed.
|
|
|
|
//
|
|
|
|
// NOTE: This method is part of the FeeEstimator interface.
|
|
|
|
func (b *BitcoindFeeEstimator) RelayFeePerKW() SatPerKWeight {
|
|
|
|
return b.minFeePerKW
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// fetchEstimate returns a fee estimate for a transaction to be confirmed in
|
|
|
|
// confTarget blocks. The estimate is returned in sat/kw.
|
|
|
|
func (b *BitcoindFeeEstimator) fetchEstimate(confTarget uint32) (SatPerKWeight, error) {
|
2017-11-10 03:30:20 +03:00
|
|
|
// First, we'll send an "estimatesmartfee" command as a raw request,
|
|
|
|
// since it isn't supported by btcd but is available in bitcoind.
|
|
|
|
target, err := json.Marshal(uint64(confTarget))
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// TODO: Allow selection of economical/conservative modifiers.
|
2018-07-28 04:37:05 +03:00
|
|
|
resp, err := b.bitcoindConn.RawRequest(
|
|
|
|
"estimatesmartfee", []json.RawMessage{target},
|
|
|
|
)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, we'll parse the response to get the BTC per KB.
|
|
|
|
feeEstimate := struct {
|
2018-07-28 04:37:05 +03:00
|
|
|
FeeRate float64 `json:"feerate"`
|
2017-11-10 03:30:20 +03:00
|
|
|
}{}
|
|
|
|
err = json.Unmarshal(resp, &feeEstimate)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Next, we'll convert the returned value to satoshis, as it's currently
|
|
|
|
// returned in BTC.
|
|
|
|
satPerKB, err := btcutil.NewAmount(feeEstimate.FeeRate)
|
2017-11-10 03:30:20 +03:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
// Since we use fee rates in sat/kw internally, we'll convert the
|
|
|
|
// estimated fee rate from its sat/kb representation to sat/kw.
|
|
|
|
satPerKw := SatPerKVByte(satPerKB).FeePerKWeight()
|
|
|
|
|
|
|
|
// Finally, we'll enforce our fee floor.
|
|
|
|
if satPerKw < b.minFeePerKW {
|
|
|
|
walletLog.Debugf("Estimated fee rate of %v sat/kw is too low, "+
|
2018-08-24 05:36:33 +03:00
|
|
|
"using fee floor of %v sat/kw instead", satPerKw,
|
|
|
|
b.minFeePerKW)
|
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
satPerKw = b.minFeePerKW
|
2018-05-14 20:45:39 +03:00
|
|
|
}
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
walletLog.Debugf("Returning %v sat/kw for conf target of %v",
|
|
|
|
int64(satPerKw), confTarget)
|
2017-11-10 03:30:20 +03:00
|
|
|
|
2018-07-28 04:37:05 +03:00
|
|
|
return satPerKw, nil
|
2017-11-10 03:30:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile-time assertion to ensure that BitcoindFeeEstimator implements the
|
|
|
|
// FeeEstimator interface.
|
|
|
|
var _ FeeEstimator = (*BitcoindFeeEstimator)(nil)
|