From 09c1c08ffa7c931377ddb8c66068cbe8624b0560 Mon Sep 17 00:00:00 2001 From: Shiv Bhonde Date: Sun, 7 Apr 2024 18:48:39 +0530 Subject: [PATCH] allow networks more networks for proxy contracts --- .../pages/[contractAddress]/[network].tsx | 27 ++++++++++++++----- packages/nextjs/pages/index.tsx | 27 ++++++++++++++----- 2 files changed, 42 insertions(+), 12 deletions(-) diff --git a/packages/nextjs/pages/[contractAddress]/[network].tsx b/packages/nextjs/pages/[contractAddress]/[network].tsx index c0867360..22a19267 100644 --- a/packages/nextjs/pages/[contractAddress]/[network].tsx +++ b/packages/nextjs/pages/[contractAddress]/[network].tsx @@ -1,10 +1,10 @@ import { useEffect, useState } from "react"; import Link from "next/link"; import { useRouter } from "next/router"; -import { AlchemyProvider } from "@ethersproject/providers"; +import { JsonRpcProvider } from "@ethersproject/providers"; import detectProxyTarget from "evm-proxy-detection"; import { ParsedUrlQuery } from "querystring"; -import { Abi, isAddress } from "viem"; +import { Abi, extractChain, isAddress } from "viem"; import * as chains from "viem/chains"; import { ExclamationTriangleIcon } from "@heroicons/react/24/outline"; import { MetaHeader } from "~~/components/MetaHeader"; @@ -24,6 +24,8 @@ type ContractData = { address: string; }; +type AllowedNetwork = (typeof scaffoldConfig.targetNetworks)[number]["id"]; + const ContractDetailPage = () => { const router = useRouter(); const { contractAddress, network } = router.query as ParsedQueryContractDetailsPage; @@ -77,10 +79,23 @@ const ContractDetailPage = () => { } try { - const alchemyProvider = new AlchemyProvider(undefined, scaffoldConfig.alchemyApiKey); - const requestFunc = ({ method, params }: { method: string; params: any }) => - alchemyProvider.send(method, params); - const implementationAddress = await detectProxyTarget(contractAddress, requestFunc); + const chain = extractChain({ + id: parseInt(network) as AllowedNetwork, + chains: Object.values(scaffoldConfig.targetNetworks), + }); + // @ts-expect-error this might be present or might not be + const alchmeyRPCURL = chain.rpcUrls?.alchemy?.http[0]; + let implementationAddress = undefined; + if (alchmeyRPCURL) { + const alchemyProvider = new JsonRpcProvider( + `${alchmeyRPCURL}/${scaffoldConfig.alchemyApiKey}`, + parseInt(network), + ); + const requestFunc = ({ method, params }: { method: string; params: any }) => + alchemyProvider.send(method, params); + implementationAddress = await detectProxyTarget(contractAddress, requestFunc); + } + if (implementationAddress) { setImplementationAddress(implementationAddress); } diff --git a/packages/nextjs/pages/index.tsx b/packages/nextjs/pages/index.tsx index 5141d71f..4d12c041 100644 --- a/packages/nextjs/pages/index.tsx +++ b/packages/nextjs/pages/index.tsx @@ -2,10 +2,10 @@ import { useEffect, useState } from "react"; import Image from "next/image"; import Link from "next/link"; import { useRouter } from "next/router"; -import { AlchemyProvider } from "@ethersproject/providers"; +import { JsonRpcProvider } from "@ethersproject/providers"; import detectProxyTarget from "evm-proxy-detection"; import type { NextPage } from "next"; -import { Address, isAddress } from "viem"; +import { Address, extractChain, isAddress } from "viem"; import { usePublicClient } from "wagmi"; import { MetaHeader } from "~~/components/MetaHeader"; import { MiniFooter } from "~~/components/MiniFooter"; @@ -21,6 +21,8 @@ enum TabName { addressAbi, } +type AllowedNetwork = (typeof scaffoldConfig.targetNetworks)[number]["id"]; + const tabValues = Object.values(TabName) as TabName[]; const networks = getTargetNetworks(); @@ -35,9 +37,6 @@ const Home: NextPage = () => { const [isCheckingContractAddress, setIsCheckingContractAddress] = useState(false); const [isContract, setIsContract] = useState(false); - const alchemyProvider = new AlchemyProvider(undefined, scaffoldConfig.alchemyApiKey); - const requestFunc = ({ method, params }: { method: string; params: any }) => alchemyProvider.send(method, params); - const publicClient = usePublicClient({ chainId: parseInt(network), }); @@ -56,7 +55,23 @@ const Home: NextPage = () => { const fetchContractAbi = async () => { setIsFetchingAbi(true); try { - const implementationAddress = await detectProxyTarget(verifiedContractAddress, requestFunc); + const chain = extractChain({ + id: parseInt(network) as AllowedNetwork, + chains: Object.values(scaffoldConfig.targetNetworks), + }); + // @ts-expect-error this might be present or might not be + const alchmeyRPCURL = chain.rpcUrls?.alchemy?.http[0]; + let implementationAddress = undefined; + if (alchmeyRPCURL) { + const alchemyProvider = new JsonRpcProvider( + `${alchmeyRPCURL}/${scaffoldConfig.alchemyApiKey}`, + parseInt(network), + ); + const requestFunc = ({ method, params }: { method: string; params: any }) => + alchemyProvider.send(method, params); + implementationAddress = await detectProxyTarget(verifiedContractAddress, requestFunc); + } + if (implementationAddress) { setImplementationAddress(implementationAddress); }