Skip to content

Commit

Permalink
let specialHandleFeeRate return the fee rate number and calls PostGas…
Browse files Browse the repository at this point in the history
…Price in single spot
  • Loading branch information
ws4charlie committed Jul 10, 2024
1 parent f7656f5 commit 3dc39b6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 36 deletions.
66 changes: 31 additions & 35 deletions zetaclient/chains/bitcoin/observer/observer.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,33 +342,42 @@ func (ob *Observer) WatchGasPrice() {
// PostGasPrice posts gas price to zetacore
// TODO(revamp): move to gas price file
func (ob *Observer) PostGasPrice() error {
var err error
feeRateEstimated := uint64(0)

Check warning on line 346 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L345-L346

Added lines #L345 - L346 were not covered by tests

// special handle regnet and testnet gas rate
if ob.Chain().NetworkType != chains.NetworkType_mainnet {
return ob.specialHandleFeeRate()
}

// EstimateSmartFee returns the fees per kilobyte (BTC/kb) targeting given block confirmation
feeResult, err := ob.btcClient.EstimateSmartFee(1, &btcjson.EstimateModeEconomical)
if err != nil {
return err
}
if feeResult.Errors != nil || feeResult.FeeRate == nil {
return fmt.Errorf("error getting gas price: %s", feeResult.Errors)
}
if *feeResult.FeeRate > math.MaxInt64 {
return fmt.Errorf("gas price is too large: %f", *feeResult.FeeRate)
feeRateEstimated, err = ob.specialHandleFeeRate()

Check warning on line 350 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L349-L350

Added lines #L349 - L350 were not covered by tests
if err != nil {
ob.logger.GasPrice.Err(err).Msg("error specialHandleFeeRate")

Check warning on line 352 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L352

Added line #L352 was not covered by tests
return err
}
} else {

Check warning on line 355 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L355

Added line #L355 was not covered by tests
// EstimateSmartFee returns the fees per kilobyte (BTC/kb) targeting given block confirmation
feeResult, err := ob.btcClient.EstimateSmartFee(1, &btcjson.EstimateModeEconomical)

Check warning on line 357 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L357

Added line #L357 was not covered by tests
if err != nil {
ob.logger.GasPrice.Err(err).Msg("error EstimateSmartFee")

Check warning on line 359 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L359

Added line #L359 was not covered by tests
return err
}
if feeResult.Errors != nil || feeResult.FeeRate == nil {
return fmt.Errorf("error getting gas price: %s", feeResult.Errors)

Check warning on line 363 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L362-L363

Added lines #L362 - L363 were not covered by tests
}
if *feeResult.FeeRate > math.MaxInt64 {
return fmt.Errorf("gas price is too large: %f", *feeResult.FeeRate)

Check warning on line 366 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L365-L366

Added lines #L365 - L366 were not covered by tests
}
feeRateEstimated = bitcoin.FeeRateToSatPerByte(*feeResult.FeeRate).Uint64()

Check warning on line 368 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L368

Added line #L368 was not covered by tests
}
feeRatePerByte := bitcoin.FeeRateToSatPerByte(*feeResult.FeeRate)

// query the current block number
blockNumber, err := ob.btcClient.GetBlockCount()
if err != nil {
return err
}

// #nosec G701 always positive
_, err = ob.ZetacoreClient().PostGasPrice(ob.Chain(), feeRatePerByte.Uint64(), "100", uint64(blockNumber))
_, err = ob.ZetacoreClient().PostGasPrice(ob.Chain(), feeRateEstimated, "100", uint64(blockNumber))

Check warning on line 378 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L378

Added line #L378 was not covered by tests
if err != nil {
ob.logger.GasPrice.Err(err).Msg("PostGasPrice:")
ob.logger.GasPrice.Err(err).Msg("err PostGasPrice")

Check warning on line 380 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L380

Added line #L380 was not covered by tests
return err
}

Expand Down Expand Up @@ -635,33 +644,20 @@ func (ob *Observer) LoadBroadcastedTxMap() error {
}

// specialHandleFeeRate handles the fee rate for regnet and testnet
func (ob *Observer) specialHandleFeeRate() error {
blockNumber, err := ob.btcClient.GetBlockCount()
if err != nil {
return errors.Wrapf(err, "specialHandleFeeRate: error GetBlockCount")
}

feeRateEstimated := uint64(0)
func (ob *Observer) specialHandleFeeRate() (uint64, error) {
switch ob.Chain().NetworkType {
case chains.NetworkType_privnet:

Check warning on line 649 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L647-L649

Added lines #L647 - L649 were not covered by tests
// hardcode gas price here since RPC 'EstimateSmartFee' is not available on regtest (privnet)
feeRateEstimated = 1
return 1, nil
case chains.NetworkType_testnet:
feeRateEstimated, err = rpc.GetRecentFeeRate(ob.btcClient, ob.netParams)
feeRateEstimated, err := rpc.GetRecentFeeRate(ob.btcClient, ob.netParams)
if err != nil {
return errors.Wrapf(err, "specialHandleFeeRate: error GetRecentFeeRate")
return 0, errors.Wrapf(err, "error GetRecentFeeRate")

Check warning on line 655 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L651-L655

Added lines #L651 - L655 were not covered by tests
}
return feeRateEstimated, nil
default:
return fmt.Errorf("specialHandleFeeRate: unsupported bitcoin network type %d", ob.Chain().NetworkType)
return 0, fmt.Errorf(" unsupported bitcoin network type %d", ob.Chain().NetworkType)

Check warning on line 659 in zetaclient/chains/bitcoin/observer/observer.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/observer/observer.go#L657-L659

Added lines #L657 - L659 were not covered by tests
}

// #nosec G701 always in range
_, err = ob.ZetacoreClient().PostGasPrice(ob.Chain(), feeRateEstimated, "100", uint64(blockNumber))
if err != nil {
return errors.Wrapf(err, "specialHandleFeeRate: error PostGasPrice")
}

return nil
}

// isTssTransaction checks if a given transaction was sent by TSS itself.
Expand Down
3 changes: 2 additions & 1 deletion zetaclient/chains/bitcoin/rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func GetRawTxResult(
}

// GetRecentFeeRate gets the highest fee rate from recent blocks
// Note: this method is only used for testnet
func GetRecentFeeRate(rpcClient interfaces.BTCRPCClient, netParams *chaincfg.Params) (uint64, error) {
blockNumber, err := rpcClient.GetBlockCount()
if err != nil {
Expand Down Expand Up @@ -148,7 +149,7 @@ func GetRecentFeeRate(rpcClient interfaces.BTCRPCClient, netParams *chaincfg.Par
}
}

// use 10 sat/byte as default estimation when fee rate is 0
// use 10 sat/byte as default estimation if recent fee rate drops to 0
if highestRate == 0 {
highestRate = defaultTestnetFeeRate

Check warning on line 154 in zetaclient/chains/bitcoin/rpc/rpc.go

View check run for this annotation

Codecov / codecov/patch

zetaclient/chains/bitcoin/rpc/rpc.go#L153-L154

Added lines #L153 - L154 were not covered by tests
}
Expand Down

0 comments on commit 3dc39b6

Please sign in to comment.