From d8f62286aac3991973d8187f37626926ef8fd0f8 Mon Sep 17 00:00:00 2001 From: "Shiv Bhonde | shivbhonde.eth" Date: Thu, 10 Oct 2024 09:08:55 +0700 Subject: [PATCH] depreciate default alchemy key (#955) --- README.md | 1 + packages/hardhat/.env.example | 2 +- packages/hardhat/hardhat.config.ts | 27 +++++++++---------- packages/nextjs/scaffold.config.ts | 5 ++-- packages/nextjs/services/web3/wagmiConfig.tsx | 7 +++-- .../scaffold-eth/fetchPriceFromUniswap.ts | 6 +++-- .../nextjs/utils/scaffold-eth/networks.ts | 2 +- 7 files changed, 27 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2943c7dc1..767baca82 100644 --- a/README.md +++ b/README.md @@ -67,6 +67,7 @@ Visit your app on: `http://localhost:3000`. You can interact with your smart con - Edit your frontend homepage at `packages/nextjs/app/page.tsx`. For guidance on [routing](https://nextjs.org/docs/app/building-your-application/routing/defining-routes) and configuring [pages/layouts](https://nextjs.org/docs/app/building-your-application/routing/pages-and-layouts) checkout the Next.js documentation. - Edit your deployment scripts in `packages/hardhat/deploy` - Edit your smart contract test in: `packages/hardhat/test`. To run test use `yarn hardhat:test` +- You can add your Alchemy API Key in `scaffold.config.ts` if you want more reliability in your RPC requests. ## Documentation diff --git a/packages/hardhat/.env.example b/packages/hardhat/.env.example index a9ccb0860..38117830b 100644 --- a/packages/hardhat/.env.example +++ b/packages/hardhat/.env.example @@ -6,6 +6,6 @@ # but we recommend getting your own API Keys for Production Apps. # To access the values stored in this .env file you can use: process.env.VARIABLENAME -ALCHEMY_API_KEY= +FORKING_URL= DEPLOYER_PRIVATE_KEY= ETHERSCAN_API_KEY= diff --git a/packages/hardhat/hardhat.config.ts b/packages/hardhat/hardhat.config.ts index 7b74685f1..e82e01be4 100644 --- a/packages/hardhat/hardhat.config.ts +++ b/packages/hardhat/hardhat.config.ts @@ -10,14 +10,13 @@ import "@nomicfoundation/hardhat-verify"; import "hardhat-deploy"; import "hardhat-deploy-ethers"; -// If not set, it uses ours Alchemy's default API key. -// You can get your own at https://dashboard.alchemyapi.io -const providerApiKey = process.env.ALCHEMY_API_KEY || "oKxs-03sij-U_N0iOlrSsZFr29-IqbuF"; // If not set, it uses the hardhat account 0 private key. const deployerPrivateKey = process.env.DEPLOYER_PRIVATE_KEY ?? "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; // If not set, it uses ours Etherscan default API key. const etherscanApiKey = process.env.ETHERSCAN_API_KEY || "DNXJA8RX2Q3VZ4URQIWP7Z68CJXQZSC6AW"; +// forking rpc url +const forkingURL = process.env.FORKING_URL || ""; const config: HardhatUserConfig = { solidity: { @@ -46,48 +45,48 @@ const config: HardhatUserConfig = { // If the network you are looking for is not here you can add new network settings hardhat: { forking: { - url: `https://eth-mainnet.alchemyapi.io/v2/${providerApiKey}`, + url: forkingURL, enabled: process.env.MAINNET_FORKING_ENABLED === "true", }, }, mainnet: { - url: `https://eth-mainnet.alchemyapi.io/v2/${providerApiKey}`, + url: `https://cloudflare-eth.com`, accounts: [deployerPrivateKey], }, sepolia: { - url: `https://eth-sepolia.g.alchemy.com/v2/${providerApiKey}`, + url: `https://rpc2.sepolia.org`, accounts: [deployerPrivateKey], }, arbitrum: { - url: `https://arb-mainnet.g.alchemy.com/v2/${providerApiKey}`, + url: `https://arb1.arbitrum.io/rpc`, accounts: [deployerPrivateKey], }, arbitrumSepolia: { - url: `https://arb-sepolia.g.alchemy.com/v2/${providerApiKey}`, + url: `https://sepolia-rollup.arbitrum.io/rpc`, accounts: [deployerPrivateKey], }, optimism: { - url: `https://opt-mainnet.g.alchemy.com/v2/${providerApiKey}`, + url: `https://mainnet.optimism.io`, accounts: [deployerPrivateKey], }, optimismSepolia: { - url: `https://opt-sepolia.g.alchemy.com/v2/${providerApiKey}`, + url: `https://sepolia.optimism.io`, accounts: [deployerPrivateKey], }, polygon: { - url: `https://polygon-mainnet.g.alchemy.com/v2/${providerApiKey}`, + url: `https://polygon-rpc.com`, accounts: [deployerPrivateKey], }, polygonMumbai: { - url: `https://polygon-mumbai.g.alchemy.com/v2/${providerApiKey}`, + url: `https://rpc.ankr.com/polygon_mumbai`, accounts: [deployerPrivateKey], }, polygonZkEvm: { - url: `https://polygonzkevm-mainnet.g.alchemy.com/v2/${providerApiKey}`, + url: `https://zkevm-rpc.com`, accounts: [deployerPrivateKey], }, polygonZkEvmTestnet: { - url: `https://polygonzkevm-testnet.g.alchemy.com/v2/${providerApiKey}`, + url: `https://rpc.public.zkevm-test.net`, accounts: [deployerPrivateKey], }, gnosis: { diff --git a/packages/nextjs/scaffold.config.ts b/packages/nextjs/scaffold.config.ts index 86c737a21..74ea4d247 100644 --- a/packages/nextjs/scaffold.config.ts +++ b/packages/nextjs/scaffold.config.ts @@ -16,11 +16,10 @@ const scaffoldConfig = { // it has no effect if you only target the local network (default is 4000) pollingInterval: 30000, - // This is ours Alchemy's default API key. - // You can get your own at https://dashboard.alchemyapi.io + // You can get your Alchemy's default API key at https://dashboard.alchemyapi.io // It's recommended to store it in an env variable: // .env.local for local testing, and in the Vercel/system env config for live apps. - alchemyApiKey: process.env.NEXT_PUBLIC_ALCHEMY_API_KEY || "oKxs-03sij-U_N0iOlrSsZFr29-IqbuF", + alchemyApiKey: process.env.NEXT_PUBLIC_ALCHEMY_API_KEY || "", // This is ours WalletConnect's default project ID. // You can get your own at https://cloud.walletconnect.com diff --git a/packages/nextjs/services/web3/wagmiConfig.tsx b/packages/nextjs/services/web3/wagmiConfig.tsx index e20784321..f3eed0940 100644 --- a/packages/nextjs/services/web3/wagmiConfig.tsx +++ b/packages/nextjs/services/web3/wagmiConfig.tsx @@ -1,5 +1,5 @@ import { wagmiConnectors } from "./wagmiConnectors"; -import { Chain, createClient, http } from "viem"; +import { Chain, createClient, fallback, http } from "viem"; import { hardhat, mainnet } from "viem/chains"; import { createConfig } from "wagmi"; import scaffoldConfig from "~~/scaffold.config"; @@ -17,9 +17,12 @@ export const wagmiConfig = createConfig({ connectors: wagmiConnectors, ssr: true, client({ chain }) { + const alchemyHttpUrl = getAlchemyHttpUrl(chain.id); + const rpcFallbacks = alchemyHttpUrl ? [http(alchemyHttpUrl), http()] : [http()]; + return createClient({ chain, - transport: http(getAlchemyHttpUrl(chain.id)), + transport: fallback(rpcFallbacks), ...(chain.id !== (hardhat as Chain).id ? { pollingInterval: scaffoldConfig.pollingInterval, diff --git a/packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts b/packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts index 2a5ab560c..39d1553de 100644 --- a/packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts +++ b/packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts @@ -1,12 +1,14 @@ import { ChainWithAttributes, getAlchemyHttpUrl } from "./networks"; import { CurrencyAmount, Token } from "@uniswap/sdk-core"; import { Pair, Route } from "@uniswap/v2-sdk"; -import { Address, createPublicClient, http, parseAbi } from "viem"; +import { Address, createPublicClient, fallback, http, parseAbi } from "viem"; import { mainnet } from "viem/chains"; +const alchemyHttpUrl = getAlchemyHttpUrl(mainnet.id); +const rpcFallbacks = alchemyHttpUrl ? [http(alchemyHttpUrl), http()] : [http()]; const publicClient = createPublicClient({ chain: mainnet, - transport: http(getAlchemyHttpUrl(mainnet.id)), + transport: fallback(rpcFallbacks), }); const ABI = parseAbi([ diff --git a/packages/nextjs/utils/scaffold-eth/networks.ts b/packages/nextjs/utils/scaffold-eth/networks.ts index 6b8276fbd..39b41913c 100644 --- a/packages/nextjs/utils/scaffold-eth/networks.ts +++ b/packages/nextjs/utils/scaffold-eth/networks.ts @@ -34,7 +34,7 @@ export const RPC_CHAIN_NAMES: Record = { }; export const getAlchemyHttpUrl = (chainId: number) => { - return RPC_CHAIN_NAMES[chainId] + return scaffoldConfig.alchemyApiKey && RPC_CHAIN_NAMES[chainId] ? `https://${RPC_CHAIN_NAMES[chainId]}.g.alchemy.com/v2/${scaffoldConfig.alchemyApiKey}` : undefined; };