mirror of
https://github.com/luxfhe/example-poker.git
synced 2026-10-30 18:13:22 +00:00
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import { Account, Address, Chain, Transport, getContract } from "viem";
|
|
import { PublicClient, usePublicClient } from "wagmi";
|
|
import { GetWalletClientResult } from "wagmi/actions";
|
|
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
|
|
import { Contract, ContractName } from "~~/utils/scaffold-eth/contract";
|
|
|
|
/**
|
|
* Gets a viem instance of the contract present in deployedContracts.ts or externalContracts.ts corresponding to
|
|
* targetNetworks configured in scaffold.config.ts. Optional walletClient can be passed for doing write transactions.
|
|
* @param config - The config settings for the hook
|
|
* @param config.contractName - deployed contract name
|
|
* @param config.walletClient - optional walletClient from wagmi useWalletClient hook can be passed for doing write transactions
|
|
*/
|
|
export const useScaffoldContract = <
|
|
TContractName extends ContractName,
|
|
TWalletClient extends Exclude<GetWalletClientResult, null> | undefined,
|
|
>({
|
|
contractName,
|
|
walletClient,
|
|
}: {
|
|
contractName: TContractName;
|
|
walletClient?: TWalletClient | null;
|
|
}) => {
|
|
const { data: deployedContractData, isLoading: deployedContractLoading } = useDeployedContractInfo(contractName);
|
|
const publicClient = usePublicClient();
|
|
|
|
let contract = undefined;
|
|
if (deployedContractData) {
|
|
contract = getContract<
|
|
Transport,
|
|
Address,
|
|
Contract<TContractName>["abi"],
|
|
Chain,
|
|
Account,
|
|
PublicClient,
|
|
TWalletClient
|
|
>({
|
|
address: deployedContractData.address,
|
|
abi: deployedContractData.abi as Contract<TContractName>["abi"],
|
|
walletClient: walletClient ? walletClient : undefined,
|
|
publicClient,
|
|
});
|
|
}
|
|
|
|
return {
|
|
data: contract,
|
|
isLoading: deployedContractLoading,
|
|
};
|
|
};
|