Skip to content

Commit

Permalink
removed tx-fee-default and use tx-fee-max as default
Browse files Browse the repository at this point in the history
  • Loading branch information
gitferry committed Nov 23, 2022
1 parent 8d5afae commit dbbcf30
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 deletions.
14 changes: 8 additions & 6 deletions btcclient/client_wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,26 @@ func NewWallet(cfg *config.BTCConfig) (*Client, error) {
// if tx size is zero, it returns the default tx
// fee in config
func (c *Client) GetTxFee(txSize uint64) uint64 {
defaultFee := uint64(c.Cfg.TxFeeDefault.ToUnit(btcutil.AmountSatoshi))
defaultFee := uint64(c.Cfg.TxFeeMax)
if txSize == 0 {
return defaultFee
}
feeRate, err := c.Client.EstimateFee(c.Cfg.TargetBlockNum)
if err != nil {
return defaultFee
}
log.Debug("fee rate is %v", feeRate)
feeRateAmount, err := btcutil.NewAmount(feeRate)
if err != nil {
return defaultFee
// this means the returned fee rate is very wrong, e.g., infinity
panic(err)
}
fee := uint64(feeRateAmount.ToUnit(btcutil.AmountSatoshi)) * txSize
if fee > uint64(c.Cfg.TxFeeMax.ToUnit(btcutil.AmountSatoshi)) ||
fee < uint64(c.Cfg.TxFeeMax.ToUnit(btcutil.AmountSatoshi)) {
fee := feeRateAmount.MulF64(float64(txSize))
if !FeeApplicable(fee, c.Cfg.TxFeeMin, c.Cfg.TxFeeMax) {
return defaultFee
}
return fee

return uint64(btcutil.AmountSatoshi)
}

func (c *Client) GetWalletName() string {
Expand Down
7 changes: 7 additions & 0 deletions btcclient/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package btcclient

import "github.com/btcsuite/btcutil"

func FeeApplicable(fee btcutil.Amount, min btcutil.Amount, max btcutil.Amount) bool {
return fee >= min && fee <= max
}
12 changes: 3 additions & 9 deletions config/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ type BTCConfig struct {
WalletName string `mapstructure:"wallet-name"`
WalletCAFile string `mapstructure:"wallet-ca-file"`
WalletLockTime int64 `mapstructure:"wallet-lock-time"` // time duration in which the wallet remains unlocked, in seconds
TxFeeDefault btcutil.Amount `mapstructure:"tx-fee-default"` // default BTC tx fee, in BTC
TxFeeMin btcutil.Amount `mapstructure:"tx-fee-min"` // minimum tx fee, in BTC
TxFeeMax btcutil.Amount `mapstructure:"tx-fee-max"` // maximum tx fee, in BTC
TargetBlockNum int64 `mapstructure:"target-block-num"` // for tx fee estimation
TargetBlockNum int64 `mapstructure:"target-block-num"` // this implies how soon (by block numbers) the tx is estimated to be included in a block
NetParams string `mapstructure:"net-params"`
Username string `mapstructure:"username"`
Password string `mapstructure:"password"`
Expand Down Expand Up @@ -53,13 +52,9 @@ func (cfg *BTCConfig) Validate() error {
}

func DefaultBTCConfig() BTCConfig {
feeAmountDefault, err := btcutil.NewAmount(1e-5)
feeAmountMin, err := btcutil.NewAmount(1e-6)
feeAmountMax, err := btcutil.NewAmount(1e-3)
feeAmountMin, _ := btcutil.NewAmount(100)
feeAmountMax, _ := btcutil.NewAmount(10000)

if err != nil {
panic(err)
}
return BTCConfig{
DisableClientTLS: false,
CAFile: defaultBtcCAFile,
Expand All @@ -69,7 +64,6 @@ func DefaultBTCConfig() BTCConfig {
WalletName: "default",
WalletCAFile: defaultBtcWalletCAFile,
WalletLockTime: 10,
TxFeeDefault: feeAmountDefault,
TxFeeMin: feeAmountMin,
TxFeeMax: feeAmountMax,
TargetBlockNum: 1,
Expand Down
4 changes: 1 addition & 3 deletions sample-vigilante-docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ btc:
no-client-tls: false
ca-file: /bitcoin/rpc.cert
endpoint: host.docker.internal:18556
tx-fee: 2500
tx-fee-default: 2500
tx-fee-min: 1
tx-fee-min: 100
tx-fee-max: 100000
target-block-num: 1
wallet-endpoint: host.docker.internal:18554
Expand Down
3 changes: 1 addition & 2 deletions sample-vigilante.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ btc:
no-client-tls: false # use true for bitcoind as it does not support tls
ca-file: $TESTNET_PATH/bitcoin/rpc.cert
endpoint: localhost:18556 # use port 18443 for bitcoind regtest
tx-fee-default: 2500
tx-fee-min: 1
tx-fee-min: 100
tx-fee-max: 100000
target-block-num: 1
wallet-endpoint: localhost:18554
Expand Down

0 comments on commit dbbcf30

Please sign in to comment.