Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
yiweichi committed Oct 8, 2024
1 parent 9bef845 commit 3900ed8
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 10 deletions.
8 changes: 8 additions & 0 deletions rollup/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ func NewConfig(file string) (*Config, error) {
return common.HexToHash(s), nil
}

if to == reflect.TypeOf(common.Hash{}) {
s, ok := data.(string)
if !ok {
return nil, fmt.Errorf("invalid hash, data: %v", data)
}
return common.HexToHash(s), nil
}

return data, nil
},
),
Expand Down
2 changes: 1 addition & 1 deletion rollup/internal/config/relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ type AlternativeGasTokenConfig struct {
Enabled bool `json:"enabled"`
Mode ExchangeRateMode `json:"mode"`
FixedExchangeRate float64 `json:"fixed_exchange_rate"`
ApiEndpoint string `json:"api_endpoint"`
TokenSymbolPair string `json:"token_symbol_pair"`
}

// GasOracleConfig The config for updating gas price oracle.
Expand Down
4 changes: 2 additions & 2 deletions rollup/internal/controller/relayer/l1_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ func (r *Layer1Relayer) ProcessGasPriceOracle() {
case config.FIXED:
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.TokenSymbolPair)
if err != nil {
log.Error("Failed to get gas token exchange rate from Binance api", "endpoint", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint, "err", err)
log.Error("Failed to get gas token exchange rate from Binance api", "tokenSymbolPair", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.TokenSymbolPair, "err", err)
}
default:
log.Error("Invalid alternative gas token mode", "mode", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.Mode)
Expand Down
4 changes: 2 additions & 2 deletions rollup/internal/controller/relayer/l2_relayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,9 @@ func (r *Layer2Relayer) ProcessGasPriceOracle() {
case config.FIXED:
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.TokenSymbolPair)
if err != nil {
log.Error("Failed to get gas token exchange rate from Binance api", "endpoint", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.ApiEndpoint, "err", err)
log.Error("Failed to get gas token exchange rate from Binance api", "tokenSymbolPair", r.cfg.GasOracleConfig.AlternativeGasTokenConfig.TokenSymbolPair, "err", err)
}

default:
Expand Down
8 changes: 5 additions & 3 deletions rollup/internal/utils/exchange_rate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ import (
"strconv"
)

var BinanceApiEndpoint string = "https://api.binance.com/api/v3/ticker/price?symbol=%s"

type BinanceResponse struct {
Price string `json:"price"`
}

func GetExchangeRateFromBinanceApi(endpoint string) (float64, error) {
func GetExchangeRateFromBinanceApi(tokenSymbolPair string) (float64, error) {
// make HTTP GET request
resp, err := http.Get(endpoint)
resp, err := http.Get(fmt.Sprintf(BinanceApiEndpoint, tokenSymbolPair))
if err != nil {
return 0, fmt.Errorf("error making HTTP request: %w", err)
}
defer func() {
err := resp.Body.Close()
err = resp.Body.Close()
if err != nil {
fmt.Println("error closing response body:", err)
}
Expand Down
4 changes: 2 additions & 2 deletions rollup/internal/utils/exchange_rate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package utils
import "testing"

func TestGetExchangeRateFromBinanceApi(t *testing.T) {
endpoint := "https://api.binance.com/api/v3/ticker/price?symbol=UNIETH"
_, err := GetExchangeRateFromBinanceApi(endpoint)
tokenSymbolPair := "UNIETH"
_, err := GetExchangeRateFromBinanceApi(tokenSymbolPair)
if err != nil {
t.Fatalf("Error getting exchange rate from Binance API: %v", err)
}
Expand Down

0 comments on commit 3900ed8

Please sign in to comment.