Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
yiweichi committed Oct 7, 2024
1 parent f39eb8b commit 9bef845
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
12 changes: 6 additions & 6 deletions rollup/internal/controller/relayer/l1_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,22 +154,22 @@ func (r *Layer1Relayer) ProcessGasPriceOracle() {

// include the token exchange rate in the fee data if alternative gas token enabled
if r.cfg.GasOracleConfig.AlternativeGasTokenConfig != nil && r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Enabled {
switch r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode {
// The exchange rate represent the number of native token on L1 required to exchange for 1 native token on L2.
var exchangeRate float64
switch r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode {
case config.FIXED:
baseFee = uint64(math.Ceil(float64(baseFee) / r.cfg.GasOracleConfig.AlternativeGasTokenConfig.FixedExchangeRate))
blobBaseFee = uint64(math.Ceil(float64(blobBaseFee) / r.cfg.GasOracleConfig.AlternativeGasTokenConfig.FixedExchangeRate))
exchangeRate = r.cfg.GasOracleConfig.AlternativeGasTokenConfig.FixedExchangeRate
case config.BINANCE_API:
exchangeRate, err := rutils.GetExchangeRateFromBinanceApi(r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint)
exchangeRate, err = rutils.GetExchangeRateFromBinanceApi(r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint)
if err != nil {
log.Error("Failed to get gas token exchange rate from Binance api", "endpoint", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint, "err", err)
}
baseFee = uint64(math.Ceil(float64(baseFee) / exchangeRate))
blobBaseFee = uint64(math.Ceil(float64(blobBaseFee) / exchangeRate))
default:
log.Error("Invalid alternative gas token mode", "mode", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode)
return
}
baseFee = uint64(math.Ceil(float64(baseFee) / exchangeRate))
blobBaseFee = uint64(math.Ceil(float64(blobBaseFee) / exchangeRate))
}

if r.shouldUpdateGasOracle(baseFee, blobBaseFee, isCurie) {
Expand Down
10 changes: 6 additions & 4 deletions rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,20 +328,22 @@ func (r *Layer2Relayer) ProcessGasPriceOracle() {

// include the token exchange rate in the fee data if alternative gas token enabled
if r.cfg.GasOracleConfig.AlternativeGasTokenConfig != nil && r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Enabled {
switch r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode {
// The exchange rate represent the number of native token on L1 required to exchange for 1 native token on L2.
var exchangeRate float64
switch r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode {
case config.FIXED:
suggestGasPriceUint64 = uint64(math.Ceil(float64(suggestGasPriceUint64) * r.cfg.GasOracleConfig.AlternativeGasTokenConfig.FixedExchangeRate))
exchangeRate = r.cfg.GasOracleConfig.AlternativeGasTokenConfig.FixedExchangeRate
case config.BINANCE_API:
exchangeRate, err := rutils.GetExchangeRateFromBinanceApi(r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint)
exchangeRate, err = rutils.GetExchangeRateFromBinanceApi(r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint)
if err != nil {
log.Error("Failed to get gas token exchange rate from Binance api", "endpoint", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint, "err", err)
}
suggestGasPriceUint64 = uint64(math.Ceil(float64(suggestGasPriceUint64) * exchangeRate))

default:
log.Error("Invalid alternative gas token mode", "mode", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode)
return
}
suggestGasPriceUint64 = uint64(math.Ceil(float64(suggestGasPriceUint64) * exchangeRate))
suggestGasPrice = new(big.Int).SetUint64(suggestGasPriceUint64)
}

Expand Down
13 changes: 7 additions & 6 deletions rollup/internal/utils/exchange_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@ func GetExchangeRateFromBinanceApi(endpoint string) (float64, error) {
if err != nil {
return 0, fmt.Errorf("error making HTTP request: %w", err)
}
defer resp.Body.Close()
defer func() {
err := resp.Body.Close()
if err != nil {
fmt.Println("error closing response body:", err)
}
}()

// check for successful response
if resp.StatusCode != http.StatusOK {
Expand All @@ -42,11 +47,7 @@ func GetExchangeRateFromBinanceApi(endpoint string) (float64, error) {
price, err := strconv.ParseFloat(data.Price, 64)
if err != nil {
return 0, fmt.Errorf("error parsing price string: %w", err)
}

if err := resp.Body.Close(); err != nil {
return 0, fmt.Errorf("error closing response body: %v", err)
}
}

return price, nil
}

0 comments on commit 9bef845

Please sign in to comment.