Skip to content

Commit

Permalink
Merge pull request #136 from lidofinance/develop
Browse files Browse the repository at this point in the history
Merge develop into main
  • Loading branch information
AnnaSila authored Sep 6, 2023
2 parents 4272c17 + 550bc12 commit e655568
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 31 deletions.
8 changes: 3 additions & 5 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# https://{NETWORK}.infura.io/v3/{INFURA_API_KEY}
INFURA_API_KEY=

# https://eth-{NETWORK}.alchemyapi.io/v2/{ALCHEMY_API_KEY}
ALCHEMY_API_KEY=
# EL_RPC_URLS_{CHAIN_ID} list or URLs delimeted by commas, first entry is primary, else are fallbacks
EL_RPC_URLS_1=
EL_RPC_URLS_5=

# etherscan api key
ETHERSCAN_API_KEY=
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Step 1. Copy the contents of `.env.sample` to `.env.local`
cp .env.sample .env.local
```

Step 2. Fill out the `.env.local`. You may need to sign up for [Infura](https://infura.io/) or [Alchemy](https://www.alchemy.com/), if you haven't already, to be able to use Ethereum JSON RPC connection.
Step 2. Fill out the `.env.local`. You will need to provide RPC provider urls with keys included.

Step 3. Install dependencies

Expand Down
11 changes: 4 additions & 7 deletions examples/sample.env-develop.env
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# https://{NETWORK}.infura.io/v3/{INFURA_API_KEY}
INFURA_API_KEY=

# https://eth-{NETWORK}.alchemyapi.io/v2/{ALCHEMY_API_KEY}
ALCHEMY_API_KEY=
# EL_RPC_URLS_{CHAIN_ID} list or URLs delimeted by commas, first entry is primary, else are fallbacks
EL_RPC_URLS_5=

# etherscan api key
ETHERSCAN_API_KEY=

# supported networks for connecting wallet
SUPPORTED_CHAINS=4,5
SUPPORTED_CHAINS=5

# this chain uses when a wallet is not connected
DEFAULT_CHAIN=4
DEFAULT_CHAIN=5

# comma-separated trusted hosts for Content Security Policy
CSP_TRUSTED_HOSTS=
Expand Down
7 changes: 2 additions & 5 deletions examples/sample.env-production.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# https://{NETWORK}.infura.io/v3/{INFURA_API_KEY}
INFURA_API_KEY=

# https://eth-{NETWORK}.alchemyapi.io/v2/{ALCHEMY_API_KEY}
ALCHEMY_API_KEY=
# EL_RPC_URLS_{CHAIN_ID} list or URLs delimeted by commas, first entry is primary, else are fallbacks
EL_RPC_URLS_1=

# etherscan api key
ETHERSCAN_API_KEY=
Expand Down
7 changes: 2 additions & 5 deletions examples/sample.env-staging.env
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
# https://{NETWORK}.infura.io/v3/{INFURA_API_KEY}
INFURA_API_KEY=

# https://eth-{NETWORK}.alchemyapi.io/v2/{ALCHEMY_API_KEY}
ALCHEMY_API_KEY=
# EL_RPC_URLS_{CHAIN_ID} list or URLs delimeted by commas, first entry is primary, else are fallbacks
EL_RPC_URLS_1=

# etherscan api key
ETHERSCAN_API_KEY=
Expand Down
17 changes: 15 additions & 2 deletions next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
const basePath = process.env.BASE_PATH || ''
const infuraApiKey = process.env.INFURA_API_KEY
const alchemyApiKey = process.env.ALCHEMY_API_KEY

const rpcUrls_1 = (process.env.EL_RPC_URLS_1 &&
process.env.EL_RPC_URLS_1.split(',')) || [
alchemyApiKey && `https://eth-mainnet.alchemyapi.io/v2/${alchemyApiKey}`,
infuraApiKey && `https://mainnet.infura.io/v3/${infuraApiKey}`,
].filter(Boolean);

const rpcUrls_5 = (process.env.EL_RPC_URLS_5 &&
process.env.EL_RPC_URLS_5.split(',')) || [
alchemyApiKey && `https://eth-goerli.alchemyapi.io/v2/${alchemyApiKey}`,
infuraApiKey && `https://goerli.infura.io/v3/${infuraApiKey}`,
].filter(Boolean);

const etherscanApiKey = process.env.ETHERSCAN_API_KEY

const defaultChain = process.env.DEFAULT_CHAIN || '1'
Expand Down Expand Up @@ -111,8 +124,8 @@ export default {
},
serverRuntimeConfig: {
basePath,
infuraApiKey,
alchemyApiKey,
rpcUrls_1,
rpcUrls_5,
etherscanApiKey,
cspTrustedHosts,
cspReportOnly,
Expand Down
14 changes: 8 additions & 6 deletions pages/api/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import clone from 'just-clone'
import getConfig from 'next/config'
import { getAlchemyRPCUrl, getInfuraRPCUrl } from '@lido-sdk/fetch'
import { CHAINS } from '@lido-sdk/constants'
import { parseChainId } from 'modules/blockChain/chains'
import { fetchWithFallback } from 'modules/network/utils/fetchWithFallback'
import type { NextApiRequest, NextApiResponse } from 'next'

const { serverRuntimeConfig } = getConfig()
const { infuraApiKey, alchemyApiKey } = serverRuntimeConfig
const { rpcUrls_1, rpcUrls_5 } = serverRuntimeConfig

export default async function rpc(req: NextApiRequest, res: NextApiResponse) {
const RPC_URLS: Record<number, string[]> = {
[CHAINS.Mainnet]: rpcUrls_1,
[CHAINS.Goerli]: rpcUrls_5,
}

const requestInfo = {
type: 'API request',
path: 'rpc',
Expand All @@ -22,10 +27,7 @@ export default async function rpc(req: NextApiRequest, res: NextApiResponse) {

try {
const chainId = parseChainId(String(req.query.chainId))
const urls = [
alchemyApiKey ? getAlchemyRPCUrl(chainId, alchemyApiKey) : '',
infuraApiKey ? getInfuraRPCUrl(chainId, infuraApiKey) : '',
].filter(Boolean)
const urls = RPC_URLS[chainId]

const requested = await fetchWithFallback(urls, chainId, {
method: 'POST',
Expand Down

0 comments on commit e655568

Please sign in to comment.