From 13b76532494e48f083adf9218cd3a5dc96ac5f71 Mon Sep 17 00:00:00 2001 From: Dmitry Holodov Date: Wed, 12 Jul 2023 22:30:29 -0500 Subject: [PATCH] made optimism endpoint configurable and changed default (#502) --- .github/workflows/unit-tests.yml | 1 + Makefile | 6 ++++-- pricefeed/pricefeed.go | 17 +++++++++++++---- pricefeed/pricefeed_test.go | 2 +- 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index b5958857..5291aa24 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -64,6 +64,7 @@ jobs: env: ETH_MAINNET_ENDPOINT: ${{ secrets.ETH_MAINNET_ENDPOINT }} ETH_SEPOLIA_ENDPOINT: ${{ secrets.ETH_SEPOLIA_ENDPOINT }} + ETH_OPTIMISM_ENDPOINT: ${{ secrets.ETH_OPTIMISM_ENDPOINT }} run: ./scripts/run-unit-tests.sh - name: Upload code coverage diff --git a/Makefile b/Makefile index 3b651e70..68c6f0dd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ GOPATH ?= $(shell go env GOPATH) +NPM_DIR = $(shell npm config get prefix) .PHONY: all all: install @@ -14,7 +15,7 @@ lint-shell: .PHONY: lint-solidity lint-solidity: - "$$(npm config get prefix)/bin/solhint" $$(find ethereum -name '*.sol' -not -path '**/@openzeppelin/**') + "$(NPM_DIR)/bin/solhint" $$(find ethereum -name '*.sol' -not -path '**/@openzeppelin/**') .PHONY: lint lint: lint-go lint-shell lint-solidity @@ -30,7 +31,8 @@ format-shell: .PHONY: format-solidity format-solidity: - "$$(npm config get prefix)/bin/prettier" --print-width 100 --write \ + "$(NPM_DIR)/bin/prettier" --print-width 100 --write \ + --plugin="$(NPM_DIR)/lib/node_modules/prettier-plugin-solidity/src/index.js" \ $$(find ethereum -name '*.sol' -not -path '**/@openzeppelin/**') .PHONY: format diff --git a/pricefeed/pricefeed.go b/pricefeed/pricefeed.go index b1c9a873..0af3a446 100644 --- a/pricefeed/pricefeed.go +++ b/pricefeed/pricefeed.go @@ -8,6 +8,7 @@ package pricefeed import ( "context" "errors" + "os" "time" "github.com/cockroachdb/apd/v3" @@ -21,10 +22,10 @@ import ( ) const ( - // optimismEndpoint is an RPC endpoint for optimism mainnet. Note that we + // fallbackOptimismEndpoint is an RPC endpoint for optimism mainnet. Note that we // tried https://mainnet.optimism.io first, but it is severely rate limited // to around 2 requests/second. - optimismEndpoint = "https://1rpc.io/op" + fallbackOptimismEndpoint = "https://optimism.blockpi.network/v1/rpc/public" // https://data.chain.link/optimism/mainnet/crypto-usd/eth-usd chainlinkETHToUSDProxy = "0x13e3ee699d1909e989722e753853ae30b17e08c5" @@ -45,6 +46,14 @@ type PriceFeed struct { UpdatedAt time.Time } +func getOptimismEndpoint() string { + endpoint := os.Getenv("ETH_OPTIMISM_ENDPOINT") + if endpoint == "" { + endpoint = fallbackOptimismEndpoint + } + return endpoint +} + // GetETHUSDPrice returns the current ETH/USD price from the Chainlink oracle. func GetETHUSDPrice(ctx context.Context, ec *ethclient.Client) (*PriceFeed, error) { chainID, err := ec.ChainID(ctx) @@ -56,7 +65,7 @@ func GetETHUSDPrice(ctx context.Context, ec *ethclient.Client) (*PriceFeed, erro case common.OpMainnetChainID: // No extra work to do case common.MainnetChainID, common.SepoliaChainID: - ec, err = ethclient.Dial(optimismEndpoint) + ec, err = ethclient.Dial(getOptimismEndpoint()) if err != nil { return nil, err } @@ -86,7 +95,7 @@ func GetXMRUSDPrice(ctx context.Context, ec *ethclient.Client) (*PriceFeed, erro // No extra work to do case common.MainnetChainID, common.SepoliaChainID: // Push stagenet/sepolia users to a mainnet endpoint - ec, err = ethclient.Dial(optimismEndpoint) + ec, err = ethclient.Dial(getOptimismEndpoint()) if err != nil { return nil, err } diff --git a/pricefeed/pricefeed_test.go b/pricefeed/pricefeed_test.go index 2ee67f88..921f86fd 100644 --- a/pricefeed/pricefeed_test.go +++ b/pricefeed/pricefeed_test.go @@ -20,7 +20,7 @@ func init() { } func newOptimismClient(t *testing.T) *ethclient.Client { - ec, err := ethclient.Dial(optimismEndpoint) + ec, err := ethclient.Dial(getOptimismEndpoint()) require.NoError(t, err) t.Cleanup(func() { ec.Close()