-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add fee estimation #118
Changes from all commits
8d5afae
09cdf8a
f738fe7
c3ca3ba
0de53b5
5fdaa9e
58c9847
fd2b46f
bce3483
ab04295
fc82572
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +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 | ||
TxFee btcutil.Amount `mapstructure:"tx-fee"` // 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe panic if |
||
TargetBlockNum int64 `mapstructure:"target-block-num"` // this implies how soon the tx is estimated to be included in a block, e.g., 1 means the tx is estimated to be included in the next block | ||
NetParams string `mapstructure:"net-params"` | ||
Username string `mapstructure:"username"` | ||
Password string `mapstructure:"password"` | ||
|
@@ -46,14 +48,21 @@ func (cfg *BTCConfig) Validate() error { | |
} | ||
} | ||
|
||
if cfg.TargetBlockNum <= 0 { | ||
return errors.New("target-block-num should be positive") | ||
} | ||
|
||
if cfg.TxFeeMin > cfg.TxFeeMax { | ||
return errors.New("tx-fee-min is larger than tx-fee-max") | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DefaultBTCConfig() BTCConfig { | ||
feeAmount, err := btcutil.NewAmount(0.00001) | ||
if err != nil { | ||
panic(err) | ||
} | ||
feeAmountMin, _ := btcutil.NewAmount(100) | ||
feeAmountMax, _ := btcutil.NewAmount(10000) | ||
|
||
return BTCConfig{ | ||
DisableClientTLS: false, | ||
CAFile: defaultBtcCAFile, | ||
|
@@ -63,7 +72,9 @@ func DefaultBTCConfig() BTCConfig { | |
WalletName: "default", | ||
WalletCAFile: defaultBtcWalletCAFile, | ||
WalletLockTime: 10, | ||
TxFee: feeAmount, | ||
TxFeeMin: feeAmountMin, | ||
TxFeeMax: feeAmountMax, | ||
TargetBlockNum: 1, | ||
NetParams: types.BtcSimnet.String(), | ||
Username: "rpcuser", | ||
Password: "rpcpass", | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,10 @@ package relayer | |
|
||
import ( | ||
"errors" | ||
"github.com/babylonchain/vigilante/types" | ||
"github.com/btcsuite/btcd/btcec" | ||
"github.com/btcsuite/btcd/txscript" | ||
"github.com/btcsuite/btcd/wire" | ||
"github.com/btcsuite/btcutil" | ||
) | ||
|
||
|
@@ -15,3 +19,49 @@ func isSegWit(addr btcutil.Address) (bool, error) { | |
return false, errors.New("non-supported address type") | ||
} | ||
} | ||
|
||
func calTxSize(tx *wire.MsgTx, utxo *types.UTXO, changeScript []byte, isSegWit bool, privkey *btcec.PrivateKey) (uint64, error) { | ||
tx.AddTxOut(wire.NewTxOut(int64(utxo.Amount), changeScript)) | ||
|
||
tx, err := completeTxIn(tx, isSegWit, privkey, utxo) | ||
if err != nil { | ||
return 0, err | ||
} | ||
|
||
return uint64(tx.SerializeSizeStripped()), nil | ||
} | ||
|
||
func completeTxIn(tx *wire.MsgTx, isSegWit bool, privKey *btcec.PrivateKey, utxo *types.UTXO) (*wire.MsgTx, error) { | ||
if !isSegWit { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
sig, err := txscript.SignatureScript( | ||
tx, | ||
0, | ||
utxo.ScriptPK, | ||
txscript.SigHashAll, | ||
privKey, | ||
true, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx.TxIn[0].SignatureScript = sig | ||
} else { | ||
sighashes := txscript.NewTxSigHashes(tx) | ||
wit, err := txscript.WitnessSignature( | ||
tx, | ||
sighashes, | ||
0, | ||
int64(utxo.Amount), | ||
utxo.ScriptPK, | ||
txscript.SigHashAll, | ||
privKey, | ||
true, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
tx.TxIn[0].Witness = wit | ||
} | ||
|
||
return tx, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would we want a minimum transaction fee? Personally I would always set this to
0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having
0
might cause an error saying the tx has insufficient priority. I'm not sure of the exact min fee we should set tho.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we remove this attribute alltogether. Why would someone want to set the minimum fee into anything other than the minimum value possible (e.g.
1
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest we remove this attribute alltogether. Why would someone want to set the minimum fee into anything other than the minimum value possible (e.g.
1
)?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The estimation might return us an extremely low tx fee which is not reasonable. We set a reasonable tx fee here to prevent this case. It could be
1
,10
, or100
.