-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding cache support for ABI fetching
- Loading branch information
Jør∂¡
committed
Mar 13, 2024
1 parent
d14484c
commit 6b2d327
Showing
5 changed files
with
72 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,82 @@ | ||
import { useEffect, useState, useCallback } from "react"; | ||
import { Address } from "viem"; | ||
import { whatsabi } from "@shazow/whatsabi"; | ||
import { usePublicClient } from "wagmi"; | ||
import { AbiFunction } from "abitype"; | ||
import { useQuery } from "@tanstack/react-query"; | ||
import { isAddress } from "@/utils/evm"; | ||
import { PUB_CHAIN, PUB_ETHERSCAN_API_KEY } from "@/constants"; | ||
import { useAlertContext } from "@/context/AlertContext"; | ||
|
||
export const useAbi = (contractAddress: Address) => { | ||
const { addAlert } = useAlertContext(); | ||
const publicClient = usePublicClient({ chainId: PUB_CHAIN.id }); | ||
const [abi, setAbi] = useState<AbiFunction[]>(); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
|
||
useEffect(() => { | ||
if (!publicClient) return; | ||
else if (!isAddress(contractAddress)) return; | ||
const { | ||
data: abi, | ||
isLoading, | ||
error, | ||
} = useQuery<AbiFunction[], Error>({ | ||
queryKey: [contractAddress || "", !!publicClient], | ||
queryFn: () => { | ||
if (!contractAddress || !isAddress(contractAddress) || !publicClient) { | ||
return Promise.resolve([]); | ||
} | ||
|
||
setIsLoading(true); | ||
const abiLoader = new whatsabi.loaders.EtherscanABILoader({ | ||
apiKey: PUB_ETHERSCAN_API_KEY, | ||
}); | ||
const abiLoader = new whatsabi.loaders.EtherscanABILoader({ | ||
apiKey: PUB_ETHERSCAN_API_KEY, | ||
}); | ||
|
||
whatsabi | ||
.autoload(contractAddress!, { | ||
provider: publicClient, | ||
abiLoader, | ||
followProxies: true, | ||
enableExperimentalMetadata: true, | ||
}) | ||
.then(({ abi }) => { | ||
const functionItems: AbiFunction[] = []; | ||
for (const item of abi) { | ||
if (item.type === "event") continue; | ||
return whatsabi | ||
.autoload(contractAddress!, { | ||
provider: publicClient, | ||
abiLoader, | ||
followProxies: true, | ||
enableExperimentalMetadata: true, | ||
}) | ||
.then(({ abi }) => { | ||
const functionItems: AbiFunction[] = []; | ||
for (const item of abi) { | ||
if (item.type === "event") continue; | ||
|
||
functionItems.push({ | ||
name: (item as any).name ?? "(function)", | ||
inputs: item.inputs ?? [], | ||
outputs: item.outputs ?? [], | ||
stateMutability: item.stateMutability ?? "payable", | ||
type: item.type, | ||
}); | ||
} | ||
functionItems.sort((a, b) => { | ||
if ( | ||
["pure", "view"].includes(a.stateMutability) && | ||
["pure", "view"].includes(b.stateMutability) | ||
) { | ||
functionItems.push({ | ||
name: (item as any).name ?? "(function)", | ||
inputs: item.inputs ?? [], | ||
outputs: item.outputs ?? [], | ||
stateMutability: item.stateMutability ?? "payable", | ||
type: item.type, | ||
}); | ||
} | ||
functionItems.sort((a, b) => { | ||
if ( | ||
["pure", "view"].includes(a.stateMutability) && | ||
["pure", "view"].includes(b.stateMutability) | ||
) { | ||
return 0; | ||
} else if (["pure", "view"].includes(a.stateMutability)) return 1; | ||
else if (["pure", "view"].includes(b.stateMutability)) return -1; | ||
return 0; | ||
} else if (["pure", "view"].includes(a.stateMutability)) return 1; | ||
else if (["pure", "view"].includes(b.stateMutability)) return -1; | ||
return 0; | ||
}); | ||
setAbi(functionItems); | ||
setIsLoading(false); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
setIsLoading(false); | ||
addAlert("Cannot fetch", { | ||
description: "The details of the contract could not be fetched", | ||
type: "error", | ||
}); | ||
return functionItems; | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
addAlert("Cannot fetch", { | ||
description: "The details of the contract could not be fetched", | ||
type: "error", | ||
}); | ||
throw err; | ||
}); | ||
}); | ||
}, [contractAddress]); | ||
}, | ||
retry: 4, | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
retryOnMount: true, | ||
staleTime: Infinity, | ||
}); | ||
|
||
return { | ||
abi: abi ?? [], | ||
isLoading, | ||
error, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters