Skip to content

Commit

Permalink
depreciate default alchemy key (#955)
Browse files Browse the repository at this point in the history
  • Loading branch information
technophile-04 authored Oct 10, 2024
1 parent 09b846b commit d8f6228
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion packages/hardhat/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
27 changes: 13 additions & 14 deletions packages/hardhat/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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: {
Expand Down
5 changes: 2 additions & 3 deletions packages/nextjs/scaffold.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions packages/nextjs/services/web3/wagmiConfig.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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,
Expand Down
6 changes: 4 additions & 2 deletions packages/nextjs/utils/scaffold-eth/fetchPriceFromUniswap.ts
Original file line number Diff line number Diff line change
@@ -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([
Expand Down
2 changes: 1 addition & 1 deletion packages/nextjs/utils/scaffold-eth/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export const RPC_CHAIN_NAMES: Record<number, string> = {
};

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;
};
Expand Down

0 comments on commit d8f6228

Please sign in to comment.