Skip to content

Commit

Permalink
Use NETWORKS_EXTRA_DATA for api keys in abi.ts, Use isZeroAddress fro…
Browse files Browse the repository at this point in the history
…m SE-2 to detect zero addresses
  • Loading branch information
portdeveloper committed Oct 3, 2024
1 parent 9a8e543 commit 12e52ed
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
25 changes: 7 additions & 18 deletions packages/nextjs/utils/abi.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NETWORKS_EXTRA_DATA } from "./scaffold-eth";
import { isZeroAddress } from "./scaffold-eth/common";
import { Address } from "viem";
import * as chains from "viem/chains";

Expand All @@ -14,25 +16,14 @@ const findChainById = (chainId: number): chains.Chain => {
};

const getEtherscanApiKey = (chainId: number): string => {
const apiKeys: { [key: number]: string | undefined } = {
1: process.env.NEXT_PUBLIC_MAINNET_ETHERSCAN_API_KEY,
10: process.env.NEXT_PUBLIC_OPTIMISM_ETHERSCAN_API_KEY,
8453: process.env.NEXT_PUBLIC_BASE_ETHERSCAN_API_KEY,
100: process.env.NEXT_PUBLIC_GNOSIS_ETHERSCAN_API_KEY,
324: process.env.NEXT_PUBLIC_ZKSYNC_ETHERSCAN_API_KEY,
137: process.env.NEXT_PUBLIC_POLYGON_ETHERSCAN_API_KEY,
42161: process.env.NEXT_PUBLIC_ARBITRUM_ETHERSCAN_API_KEY,
534352: process.env.NEXT_PUBLIC_SCROLL_ETHERSCAN_API_KEY,
56: process.env.NEXT_PUBLIC_BSC_ETHERSCAN_API_KEY,
};

const apiKey = apiKeys[chainId];
const networkData = NETWORKS_EXTRA_DATA[chainId];

if (!apiKey) {
if (!networkData || !networkData.etherscanApiKey) {
console.warn(`No API key found for chain ID ${chainId}`);
return "";
}

return apiKey || "";
return networkData.etherscanApiKey;
};

export const fetchContractABIFromEtherscan = async (verifiedContractAddress: Address, chainId: number) => {
Expand All @@ -48,8 +39,6 @@ export const fetchContractABIFromEtherscan = async (verifiedContractAddress: Add
// First call to get source code and check for implementation
const sourceCodeUrl = `${chain.blockExplorers.default.apiUrl}?module=contract&action=getsourcecode&address=${verifiedContractAddress}${apiKeyUrlParam}`;

console.log("Source code URL:", sourceCodeUrl);

const sourceCodeResponse = await fetch(sourceCodeUrl);
const sourceCodeData = await sourceCodeResponse.json();

Expand All @@ -62,7 +51,7 @@ export const fetchContractABIFromEtherscan = async (verifiedContractAddress: Add
const implementation = contractData.Implementation || null;

// If there's an implementation address, make a second call to get its ABI
if (implementation && implementation !== "0x0000000000000000000000000000000000000000") {
if (implementation && !isZeroAddress(implementation)) {
const abiUrl = `${chain.blockExplorers.default.apiUrl}?module=contract&action=getabi&address=${implementation}${apiKeyUrlParam}`;
const abiResponse = await fetch(abiUrl);
const abiData = await abiResponse.json();
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/utils/scaffold-eth/common.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// To be used in JSON.stringify when a field might be bigint
// https://wagmi.sh/react/faq#bigint-serialization
export const replacer = (_key: string, value: unknown) => (typeof value === "bigint" ? value.toString() : value);

export const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

export const isZeroAddress = (address: string) => address === ZERO_ADDRESS;
7 changes: 7 additions & 0 deletions packages/nextjs/utils/scaffold-eth/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const ARBITRUM_ETHERSCAN_API_KEY = process.env.NEXT_PUBLIC_ARBITRUM_ETHERSCAN_AP
const ZKSYNC_ETHERSCAN_API_KEY = process.env.NEXT_PUBLIC_ZKSYNC_ETHERSCAN_API_KEY || "";
const BASE_ETHERSCAN_API_KEY = process.env.NEXT_PUBLIC_BASE_ETHERSCAN_API_KEY || "";
const SCROLL_ETHERSCAN_API_KEY = process.env.NEXT_PUBLIC_SCROLL_ETHERSCAN_API_KEY || "";
const BSC_ETHERSCAN_API_KEY = process.env.NEXT_PUBLIC_BSC_ETHERSCAN_API_KEY || "";

export const NETWORKS_EXTRA_DATA: Record<string, ChainAttributes> = {
[chains.hardhat.id]: {
Expand Down Expand Up @@ -137,6 +138,12 @@ export const NETWORKS_EXTRA_DATA: Record<string, ChainAttributes> = {
etherscanApiKey: SCROLL_ETHERSCAN_API_KEY,
icon: "/scroll.svg",
},
[chains.bsc.id]: {
color: "#f0b90b",
etherscanEndpoint: "https://api.bscscan.com",
etherscanApiKey: BSC_ETHERSCAN_API_KEY,
icon: "/bsc.svg",
},
};

/**
Expand Down

0 comments on commit 12e52ed

Please sign in to comment.