Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional chain parameter in Scaffold hooks #931

Merged
merged 18 commits into from
Dec 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
consolidated repetitive code
  • Loading branch information
JacobHomanics committed Dec 7, 2024
commit ff43dbfed1d18debadd64f81bfcfc325ab2585e3
7 changes: 2 additions & 5 deletions packages/nextjs/hooks/scaffold-eth/useDeployedContractInfo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useEffect, useState } from "react";
import { useTargetNetwork } from "./useTargetNetwork";
import { useIsMounted } from "usehooks-ts";
import { usePublicClient } from "wagmi";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import {
Contract,
ContractCodeStatus,
@@ -20,10 +19,8 @@ export const useDeployedContractInfo = <TContractName extends ContractName>({
chainId,
}: UseDeployedContractConfig<TContractName>) => {
const isMounted = useIsMounted();
const { targetNetwork } = useTargetNetwork();
const selectedChain =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;

const selectedChain = useAllowedChain(chainId as AllowedChainIds);
const deployedContract = contracts?.[selectedChain.id]?.[contractName as ContractName] as Contract<TContractName>;
const [status, setStatus] = useState<ContractCodeStatus>(ContractCodeStatus.LOADING);
const publicClient = usePublicClient({ chainId: selectedChain.id });
10 changes: 2 additions & 8 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldContract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useTargetNetwork } from "./useTargetNetwork";
import { Account, Address, Chain, Client, Transport, getContract } from "viem";
import { usePublicClient } from "wagmi";
import { GetWalletClientReturnType } from "wagmi/actions";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { Contract, ContractName } from "~~/utils/scaffold-eth/contract";

/**
@@ -26,11 +24,7 @@ export const useScaffoldContract = <
walletClient?: TWalletClient | null;
chainId?: AllowedChainIds;
}) => {
const { targetNetwork } = useTargetNetwork();

const selectedChain =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;

const selectedChain = useAllowedChain(chainId as AllowedChainIds);
const { data: deployedContractData, isLoading: deployedContractLoading } = useDeployedContractInfo({
contractName,
chainId: selectedChain?.id as AllowedChainIds,
8 changes: 2 additions & 6 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldEventHistory.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useEffect, useState } from "react";
import { useTargetNetwork } from "./useTargetNetwork";
import { useInfiniteQuery } from "@tanstack/react-query";
import { Abi, AbiEvent, ExtractAbiEventNames } from "abitype";
import { BlockNumber, GetLogsParameters } from "viem";
import { Config, UsePublicClientReturnType, useBlockNumber, usePublicClient } from "wagmi";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { replacer } from "~~/utils/scaffold-eth/common";
import {
ContractAbi,
@@ -84,9 +82,7 @@ export const useScaffoldEventHistory = <
watch,
enabled = true,
}: UseScaffoldEventHistoryConfig<TContractName, TEventName, TBlockData, TTransactionData, TReceiptData>) => {
const { targetNetwork } = useTargetNetwork();
const selectedChain =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
const selectedChain = useAllowedChain(chainId as AllowedChainIds);

const publicClient = usePublicClient({
chainId: selectedChain.id,
8 changes: 2 additions & 6 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldReadContract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { useEffect } from "react";
import { useTargetNetwork } from "./useTargetNetwork";
import { QueryObserverResult, RefetchOptions, useQueryClient } from "@tanstack/react-query";
import type { ExtractAbiFunctionNames } from "abitype";
import { ReadContractErrorType } from "viem";
import { useBlockNumber, useReadContract } from "wagmi";
import { useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import {
AbiFunctionReturnType,
ContractAbi,
@@ -32,9 +30,7 @@ export const useScaffoldReadContract = <
chainId,
...readConfig
}: UseScaffoldReadConfig<TContractName, TFunctionName>) => {
const { targetNetwork } = useTargetNetwork();
const selectedChain =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
const selectedChain = useAllowedChain(chainId as AllowedChainIds);
const { data: deployedContract } = useDeployedContractInfo({
contractName,
chainId: selectedChain.id as AllowedChainIds,
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useTargetNetwork } from "./useTargetNetwork";
import { Abi, ExtractAbiEventNames } from "abitype";
import { Log } from "viem";
import { useWatchContractEvent } from "wagmi";
import { addIndexedArgsToEvent, useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { ContractAbi, ContractName, UseScaffoldEventConfig } from "~~/utils/scaffold-eth/contract";

/**
@@ -24,9 +22,7 @@ export const useScaffoldWatchContractEvent = <
chainId,
onLogs,
}: UseScaffoldEventConfig<TContractName, TEventName>) => {
const { targetNetwork } = useTargetNetwork();
const selectedChain =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
const selectedChain = useAllowedChain(chainId as AllowedChainIds);
const { data: deployedContractData } = useDeployedContractInfo({
contractName,
chainId: selectedChain.id as AllowedChainIds,
6 changes: 6 additions & 0 deletions packages/nextjs/utils/scaffold-eth/networks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as chains from "viem/chains";
import scaffoldConfig from "~~/scaffold.config";
import { useGlobalState } from "~~/services/store/store";

type ChainAttributes = {
// color | [lightThemeColor, darkThemeColor]
@@ -138,3 +139,8 @@ export function getTargetNetworks(): ChainWithAttributes[] {
...NETWORKS_EXTRA_DATA[targetNetwork.id],
}));
}

export function useAllowedChain(chainId: AllowedChainIds) {
const targetNetwork = useGlobalState(({ targetNetwork }) => targetNetwork);
return scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
}
Loading