Skip to content

Commit

Permalink
Making useAbi work well with EOA's
Browse files Browse the repository at this point in the history
  • Loading branch information
brickpop committed Jul 15, 2024
1 parent 9bda179 commit c29cc51
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
42 changes: 29 additions & 13 deletions hooks/useAbi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,17 @@ export const useAbi = (contractAddress: Address) => {
error,
} = useQuery<AbiFunction[], Error>({
queryKey: ["abi", resolvedAddress || "", !!publicClient],
queryFn: () => {
queryFn: async () => {
if (!resolvedAddress || !isAddress(resolvedAddress) || !publicClient) {
return Promise.resolve([]);
return [];
} else if (!(await isContract(resolvedAddress, publicClient))) {
return [];
}

const abiLoader = getEtherscanAbiLoader();
return whatsabi
.autoload(resolvedAddress, {
provider: publicClient,
abiLoader,
abiLoader: getEtherscanAbiLoader(),
followProxies: false,
enableExperimentalMetadata: true,
})
Expand All @@ -68,15 +69,7 @@ export const useAbi = (contractAddress: Address) => {
type: item.type,
});
}
functionItems.sort((a, b) => {
const a_RO = ["pure", "view"].includes(a.stateMutability);
const b_RO = ["pure", "view"].includes(b.stateMutability);

if (a_RO === b_RO) return 0;
else if (a_RO) return 1;
else if (b_RO) return -1;
return 0;
});
functionItems.sort(abiSortCallback);
return functionItems;
})
.catch((err) => {
Expand Down Expand Up @@ -125,6 +118,11 @@ function getEtherscanAbiLoader() {
apiKey: PUB_ETHERSCAN_API_KEY,
baseURL: "https://api-sepolia.etherscan.io/api",
});
case "holesky":
return new whatsabi.loaders.EtherscanABILoader({
apiKey: PUB_ETHERSCAN_API_KEY,
baseURL: "https://api-holesky.etherscan.io/api",
});
case "mumbai":
return new whatsabi.loaders.EtherscanABILoader({
apiKey: PUB_ETHERSCAN_API_KEY,
Expand All @@ -134,3 +132,21 @@ function getEtherscanAbiLoader() {
throw new Error("Unknown chain");
}
}

function isContract(address: Address, publicClient: ReturnType<typeof usePublicClient>) {
if (!publicClient) return Promise.reject(new Error("Invalid client"));

return publicClient.getBytecode({ address }).then((bytecode) => {
return bytecode !== undefined && bytecode !== "0x";
});
}

function abiSortCallback(a: AbiFunction, b: AbiFunction) {
const a_RO = ["pure", "view"].includes(a.stateMutability);
const b_RO = ["pure", "view"].includes(b.stateMutability);

if (a_RO === b_RO) return 0;
else if (a_RO) return 1;
else if (b_RO) return -1;
return 0;
}
6 changes: 4 additions & 2 deletions utils/chains.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { polygon, mainnet, sepolia, arbitrum, polygonMumbai, Chain } from "@wagmi/core/chains";
import { polygon, mainnet, sepolia, holesky, arbitrum, polygonMumbai, Chain } from "@wagmi/core/chains";

const chainNames = ["mainnet", "polygon", "sepolia", "mumbai", "arbitrum"] as const;
const chainNames = ["mainnet", "polygon", "sepolia", "holesky", "mumbai", "arbitrum"] as const;
export type ChainName = (typeof chainNames)[number];

export function getChain(chainName: ChainName): Chain {
Expand All @@ -13,6 +13,8 @@ export function getChain(chainName: ChainName): Chain {
return arbitrum;
case "sepolia":
return sepolia;
case "holesky":
return holesky;
case "mumbai":
return polygonMumbai;
default:
Expand Down

0 comments on commit c29cc51

Please sign in to comment.