Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow more networks for proxy contracts #81

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions packages/nextjs/pages/[contractAddress]/[network].tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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];
Comment on lines +82 to +87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marking this, for future use

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);
}
Expand Down
27 changes: 21 additions & 6 deletions packages/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -21,6 +21,8 @@ enum TabName {
addressAbi,
}

type AllowedNetwork = (typeof scaffoldConfig.targetNetworks)[number]["id"];

const tabValues = Object.values(TabName) as TabName[];

const networks = getTargetNetworks();
Expand All @@ -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),
});
Expand All @@ -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);
}
Expand Down
Loading