mirror of
https://github.com/luxfhe/example-poker.git
synced 2026-10-30 18:13:22 +00:00
49 lines
1.9 KiB
TypeScript
49 lines
1.9 KiB
TypeScript
import { useTargetNetwork } from "./useTargetNetwork";
|
|
import type { ExtractAbiFunctionNames } from "abitype";
|
|
import { useContractRead } from "wagmi";
|
|
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
|
|
import {
|
|
AbiFunctionReturnType,
|
|
ContractAbi,
|
|
ContractName,
|
|
UseScaffoldReadConfig,
|
|
} from "~~/utils/scaffold-eth/contract";
|
|
|
|
/**
|
|
* Wrapper around wagmi's useContractRead hook which automatically loads (by name) the contract ABI and address from
|
|
* the contracts present in deployedContracts.ts & externalContracts.ts corresponding to targetNetworks configured in scaffold.config.ts
|
|
* @param config - The config settings, including extra wagmi configuration
|
|
* @param config.contractName - deployed contract name
|
|
* @param config.functionName - name of the function to be called
|
|
* @param config.args - args to be passed to the function call
|
|
*/
|
|
export const useScaffoldContractRead = <
|
|
TContractName extends ContractName,
|
|
TFunctionName extends ExtractAbiFunctionNames<ContractAbi<TContractName>, "pure" | "view">,
|
|
>({
|
|
contractName,
|
|
functionName,
|
|
args,
|
|
...readConfig
|
|
}: UseScaffoldReadConfig<TContractName, TFunctionName>) => {
|
|
const { data: deployedContract } = useDeployedContractInfo(contractName);
|
|
const { targetNetwork } = useTargetNetwork();
|
|
|
|
return useContractRead<ContractAbi<TContractName>, TFunctionName>({
|
|
chainId: targetNetwork.id,
|
|
functionName,
|
|
address: deployedContract?.address,
|
|
abi: deployedContract?.abi,
|
|
watch: true,
|
|
args,
|
|
enabled: !Array.isArray(args) || !args.some(arg => arg === undefined),
|
|
...(readConfig as any),
|
|
}) as Omit<ReturnType<typeof useContractRead>, "data" | "refetch"> & {
|
|
data: AbiFunctionReturnType<ContractAbi, TFunctionName> | undefined;
|
|
refetch: (options?: {
|
|
throwOnError: boolean;
|
|
cancelRefetch: boolean;
|
|
}) => Promise<AbiFunctionReturnType<ContractAbi, TFunctionName>>;
|
|
};
|
|
};
|