diff --git a/zetaclient/bitcoin_client.go b/zetaclient/bitcoin_client.go index 72bce508cb..a0cd8b19cf 100644 --- a/zetaclient/bitcoin_client.go +++ b/zetaclient/bitcoin_client.go @@ -459,14 +459,14 @@ func (ob *BitcoinChainClient) PostGasPrice() error { if feeResult.Errors != nil || feeResult.FeeRate == nil { return fmt.Errorf("error getting gas price: %s", feeResult.Errors) } - gasPrice := big.NewFloat(0) - gasPriceU64, _ := gasPrice.Mul(big.NewFloat(*feeResult.FeeRate), big.NewFloat(1e8)).Uint64() + feeRate := new(big.Int).SetInt64(int64(*feeResult.FeeRate * 1e8)) + feeRatePerByte := new(big.Int).Div(feeRate, big.NewInt(1000)) bn, err := ob.rpcClient.GetBlockCount() if err != nil { return err } // #nosec G701 always positive - zetaHash, err := ob.zetaClient.PostGasPrice(ob.chain, gasPriceU64, "100", uint64(bn)) + zetaHash, err := ob.zetaClient.PostGasPrice(ob.chain, feeRatePerByte.Uint64(), "100", uint64(bn)) if err != nil { ob.logger.WatchGasPrice.Err(err).Msg("PostGasPrice:") return err diff --git a/zetaclient/btc_signer.go b/zetaclient/btc_signer.go index a31f77bc85..1880089ec4 100644 --- a/zetaclient/btc_signer.go +++ b/zetaclient/btc_signer.go @@ -23,6 +23,8 @@ import ( const ( maxNoOfInputsPerTx = 20 + minFee = 5_000 // 0.00005 BTC + estimateSizeKB = 2 // 2K is a conservative estimate for a 20-input, 3-output tx ) type BTCSigner struct { @@ -59,9 +61,9 @@ func NewBTCSigner(cfg config.BTCConfig, tssSigner TSSSigner, logger zerolog.Logg } // SignWithdrawTx receives utxos sorted by value, amount in BTC, feeRate in BTC per Kb -func (signer *BTCSigner) SignWithdrawTx(to *btcutil.AddressWitnessPubKeyHash, amount float64, gasPrice *big.Int, btcClient *BitcoinChainClient, height uint64, nonce uint64, chain *common.Chain) (*wire.MsgTx, error) { - estimateFee := 0.0001 // 10,000 sats, should be good for testnet - minFee := 0.00005 +func (signer *BTCSigner) SignWithdrawTx(to *btcutil.AddressWitnessPubKeyHash, amount float64, gasPrice *big.Int, sizeLimit uint64, + btcClient *BitcoinChainClient, height uint64, nonce uint64, chain *common.Chain) (*wire.MsgTx, error) { + estimateFee := float64(gasPrice.Uint64()) * estimateSizeKB / 1e8 nonceMark := NonceMarkAmount(nonce) // refresh unspent UTXOs and continue with keysign regardless of error @@ -93,8 +95,15 @@ func (signer *BTCSigner) SignWithdrawTx(to *btcutil.AddressWitnessPubKeyHash, am return nil, err } + // size checking + txSize := uint64(tx.SerializeSize()) + // if txSize > sizeLimit { + // fmt.Printf("tx size %d is greater than sizeLimit %d; use sizeLimit", txSize, sizeLimit) + // txSize = sizeLimit + // } + // fee checking - fees := new(big.Int).Mul(big.NewInt(int64(tx.SerializeSize())), gasPrice) + fees := new(big.Int).Mul(big.NewInt(int64(txSize)), gasPrice) fees.Div(fees, big.NewInt(1000)) //FIXME: feeRate KB is 1000B or 1024B? // #nosec G701 always in range if fees.Int64() < int64(minFee*1e8) { @@ -102,6 +111,12 @@ func (signer *BTCSigner) SignWithdrawTx(to *btcutil.AddressWitnessPubKeyHash, am // #nosec G701 always in range fees = big.NewInt(int64(minFee * 1e8)) } + // #nosec G701 always in range + if fees.Int64() > int64(estimateFee*1e8) { + fmt.Printf("fees %d is greater than estimateFee %f; use estimateFee", fees, estimateFee*1e8) + // #nosec G701 always in range + fees = big.NewInt(int64(estimateFee * 1e8)) + } // calculate remaining btc to TSS self tssAddrWPKH := signer.tssSigner.BTCAddressWitnessPubkeyHash() @@ -253,8 +268,9 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out return } + sizelimit := params.OutboundTxGasLimit gasprice, ok := new(big.Int).SetString(params.OutboundTxGasPrice, 10) - if !ok { + if !ok || gasprice.Cmp(big.NewInt(0)) < 0 { logger.Error().Msgf("cannot convert gas price %s ", params.OutboundTxGasPrice) return } @@ -273,7 +289,7 @@ func (signer *BTCSigner) TryProcessOutTx(send *types.CrossChainTx, outTxMan *Out logger.Info().Msgf("SignWithdrawTx: to %s, value %d sats", addr.EncodeAddress(), params.Amount.Uint64()) logger.Info().Msgf("using utxos: %v", btcClient.utxos) - tx, err := signer.SignWithdrawTx(to, float64(params.Amount.Uint64())/1e8, gasprice, btcClient, height, + tx, err := signer.SignWithdrawTx(to, float64(params.Amount.Uint64())/1e8, gasprice, sizelimit, btcClient, height, outboundTxTssNonce, &btcClient.chain) if err != nil { logger.Warn().Err(err).Msgf("SignOutboundTx error: nonce %d chain %d", outboundTxTssNonce, params.ReceiverChainId)