Skip to content

Commit

Permalink
Use and show implementation address
Browse files Browse the repository at this point in the history
  • Loading branch information
portdeveloper committed Mar 20, 2024
1 parent 536a83f commit c6831db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
13 changes: 10 additions & 3 deletions packages/nextjs/components/scaffold-eth/Contract/ContractUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,11 @@ const mainNetworks = getTargetNetworks();
**/
export const ContractUI = ({ className = "", initialContractData }: ContractUIProps) => {
const [refreshDisplayVariables, triggerRefreshDisplayVariables] = useReducer(value => !value, false);
const mainChainId = useAbiNinjaState(state => state.mainChainId);
const mainNetwork = mainNetworks.find(network => network.id === mainChainId);
const { implementationAddress, chainId } = useAbiNinjaState(state => ({
chainId: state.mainChainId,
implementationAddress: state.implementationAddress,
}));
const mainNetwork = mainNetworks.find(network => network.id === chainId);
const networkColor = useNetworkColor(mainNetwork);
const router = useRouter();
const { network } = router.query as { network?: string };
Expand Down Expand Up @@ -124,7 +127,7 @@ export const ContractUI = ({ className = "", initialContractData }: ContractUIPr
const { data: contractNameData, isLoading: isContractNameLoading } = useContractRead({
address: initialContractData.address,
abi: initialContractData.abi,
chainId: mainChainId,
chainId: chainId,
functionName: "name",
});

Expand Down Expand Up @@ -219,6 +222,10 @@ export const ContractUI = ({ className = "", initialContractData }: ContractUIPr
<span className="font-medium text-base mr-4"> {displayContractName} </span>
<Address address={initialContractData.address} />
</div>
<div className="flex items-center gap-1">
<span className="font-medium text-base mr-4 text-green-600">Implementation Address</span>
<Address address={implementationAddress} />
</div>
<div className="flex items-center gap-1">
<span className="text-sm font-bold">Balance:</span>
<Balance address={initialContractData.address} className="h-1.5 min-h-[0.375rem] px-0" />
Expand Down
20 changes: 18 additions & 2 deletions packages/nextjs/pages/[contractAddress]/[network].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState } from "react";
import Link from "next/link";
import { useRouter } from "next/router";
import { ParsedUrlQuery } from "querystring";
import { Abi, isAddress } from "viem";
import { Abi, Address, isAddress } from "viem";
import * as chains from "viem/chains";
import { ExclamationTriangleIcon } from "@heroicons/react/24/outline";
import { Footer } from "~~/components/Footer";
Expand All @@ -11,6 +11,7 @@ import { MiniHeader } from "~~/components/MiniHeader";
import { ContractUI } from "~~/components/scaffold-eth";
import { useAbiNinjaState } from "~~/services/store/store";
import { fetchContractABIFromAnyABI, fetchContractABIFromEtherscan } from "~~/utils/abi";
import detectProxyTarget from "~~/utils/abi.ninja/proxyContracts";

interface ParsedQueryContractDetailsPage extends ParsedUrlQuery {
contractAddress: string;
Expand All @@ -22,6 +23,15 @@ type ContractData = {
address: string;
};

const getImplementationAddress = async (proxyAddress: Address) => {
try {
const target = await detectProxyTarget(proxyAddress);
return target;
} catch (e) {
console.error(e);
}
};

const ContractDetailPage = () => {
const router = useRouter();
const { contractAddress, network } = router.query as ParsedQueryContractDetailsPage;
Expand All @@ -33,10 +43,12 @@ const ContractDetailPage = () => {
contractAbi: storedAbi,
setMainChainId,
chainId,
setImplementationAddress,
} = useAbiNinjaState(state => ({
contractAbi: state.contractAbi,
setMainChainId: state.setMainChainId,
chainId: state.mainChainId,
setImplementationAddress: state.setImplementationAddress,
}));

const getNetworkName = (chainId: number) => {
Expand Down Expand Up @@ -73,7 +85,11 @@ const ContractDetailPage = () => {
}

try {
const abi = await fetchContractABIFromAnyABI(contractAddress, parsedNetworkId);
const implementationAddress = await getImplementationAddress(contractAddress);
if (implementationAddress) {
setImplementationAddress(implementationAddress);
}
const abi = await fetchContractABIFromAnyABI(implementationAddress || contractAddress, parsedNetworkId);
if (!abi) throw new Error("Got empty or undefined ABI from AnyABI");
setContractData({ abi, address: contractAddress });
setError(null);
Expand Down
6 changes: 5 additions & 1 deletion packages/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ const Home: NextPage = () => {
chainId: parseInt(network),
});

const { setContractAbi, setAbiContractAddress } = useAbiNinjaState(state => ({
const { setContractAbi, setAbiContractAddress, setImplementationAddress } = useAbiNinjaState(state => ({
setContractAbi: state.setContractAbi,
setAbiContractAddress: state.setAbiContractAddress,
setImplementationAddress: state.setImplementationAddress,
}));

const [isAbiAvailable, setIsAbiAvailable] = useState(false);
Expand All @@ -60,6 +61,9 @@ const Home: NextPage = () => {
setIsFetchingAbi(true);
try {
const implementationAddress = await getImplementationAddress(verifiedContractAddress);
if (implementationAddress) {
setImplementationAddress(implementationAddress);
}
const abi = await fetchContractABIFromAnyABI(
implementationAddress || verifiedContractAddress,
parseInt(network),
Expand Down
4 changes: 4 additions & 0 deletions packages/nextjs/services/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type AbiNinjaState = {
setContractAbi: (newAbi: Abi) => void;
abiContractAddress: Address;
setAbiContractAddress: (newAbiContractAddress: Address) => void;
implementationAddress: Address;
setImplementationAddress: (newImplementationAddress: Address) => void;
};

export const useGlobalState = create<GlobalState>(set => ({
Expand All @@ -33,4 +35,6 @@ export const useAbiNinjaState = create<AbiNinjaState>(set => ({
setContractAbi: (newAbi: Abi): void => set({ contractAbi: newAbi }),
abiContractAddress: "",
setAbiContractAddress: (newAddress: Address): void => set({ abiContractAddress: newAddress }),
implementationAddress: "",
setImplementationAddress: (newAddress: Address): void => set({ implementationAddress: newAddress }),
}));

0 comments on commit c6831db

Please sign in to comment.