Skip to content

Commit

Permalink
changed call to hook
Browse files Browse the repository at this point in the history
  • Loading branch information
JacobHomanics committed Dec 7, 2024
1 parent ff43dbf commit 76a5472
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 19 deletions.
8 changes: 8 additions & 0 deletions packages/nextjs/hooks/scaffold-eth/useAllowedChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scaffoldConfig from "~~/scaffold.config";
import { useGlobalState } from "~~/services/store/store";
import { AllowedChainIds } from "~~/utils/scaffold-eth";

export function useAllowedChain(chainId: AllowedChainIds) {
const targetNetwork = useGlobalState(({ targetNetwork }) => targetNetwork);
return scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useEffect, useState } from "react";
import { useAllowedChain } from "./useAllowedChain";
import { useIsMounted } from "usehooks-ts";
import { usePublicClient } from "wagmi";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import {
Contract,
ContractCodeStatus,
Expand Down
3 changes: 2 additions & 1 deletion packages/nextjs/hooks/scaffold-eth/useScaffoldContract.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useAllowedChain } from "./useAllowedChain";
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 { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { Contract, ContractName } from "~~/utils/scaffold-eth/contract";

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useEffect, useState } from "react";
import { useAllowedChain } from "./useAllowedChain";
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 { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { replacer } from "~~/utils/scaffold-eth/common";
import {
ContractAbi,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { useEffect } from "react";
import { useAllowedChain } from "./useAllowedChain";
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 { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import {
AbiFunctionReturnType,
ContractAbi,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { useAllowedChain } from "./useAllowedChain";
import { Abi, ExtractAbiEventNames } from "abitype";
import { Log } from "viem";
import { useWatchContractEvent } from "wagmi";
import { addIndexedArgsToEvent, useDeployedContractInfo } from "~~/hooks/scaffold-eth";
import { AllowedChainIds, useAllowedChain } from "~~/utils/scaffold-eth";
import { AllowedChainIds } from "~~/utils/scaffold-eth";
import { ContractAbi, ContractName, UseScaffoldEventConfig } from "~~/utils/scaffold-eth/contract";

/**
Expand Down
13 changes: 5 additions & 8 deletions packages/nextjs/hooks/scaffold-eth/useScaffoldWriteContract.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { useState } from "react";
import { useTargetNetwork } from "./useTargetNetwork";
import { useAllowedChain } from "./useAllowedChain";
import { MutateOptions } from "@tanstack/react-query";
import { Abi, ExtractAbiFunctionNames } from "abitype";
import { Config, useAccount, useWriteContract } from "wagmi";
import { WriteContractErrorType, WriteContractReturnType } from "wagmi/actions";
import { WriteContractVariables } from "wagmi/query";
import { useDeployedContractInfo, useTransactor } from "~~/hooks/scaffold-eth";
import scaffoldConfig from "~~/scaffold.config";
import { AllowedChainIds, notification } from "~~/utils/scaffold-eth";
import {
ContractAbi,
Expand All @@ -30,16 +29,14 @@ export const useScaffoldWriteContract = <TContractName extends ContractName>({
const { chain: accountChain } = useAccount();
const writeTx = useTransactor();
const [isMining, setIsMining] = useState(false);
const { targetNetwork } = useTargetNetwork();

const wagmiContractWrite = useWriteContract(writeContractParams);

const selectedNetwork =
scaffoldConfig.targetNetworks.find(targetNetwork => targetNetwork.id === chainId) ?? targetNetwork;
const selectedChain = useAllowedChain(chainId as AllowedChainIds);

const { data: deployedContractData } = useDeployedContractInfo({
contractName,
chainId: selectedNetwork.id as AllowedChainIds,
chainId: selectedChain.id as AllowedChainIds,
});

const sendContractWriteAsyncTx = async <
Expand All @@ -58,7 +55,7 @@ export const useScaffoldWriteContract = <TContractName extends ContractName>({
return;
}

if (accountChain?.id !== selectedNetwork.id) {
if (accountChain?.id !== selectedChain.id) {
notification.error("Your wallet is connected to the wrong network");
return;
}
Expand Down Expand Up @@ -108,7 +105,7 @@ export const useScaffoldWriteContract = <TContractName extends ContractName>({
return;
}

if (accountChain?.id !== selectedNetwork.id) {
if (accountChain?.id !== selectedChain.id) {
notification.error("Your wallet is connected to the wrong network");
return;
}
Expand Down
6 changes: 0 additions & 6 deletions packages/nextjs/utils/scaffold-eth/networks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as chains from "viem/chains";
import scaffoldConfig from "~~/scaffold.config";
import { useGlobalState } from "~~/services/store/store";

type ChainAttributes = {
// color | [lightThemeColor, darkThemeColor]
Expand Down Expand Up @@ -139,8 +138,3 @@ 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;
}

0 comments on commit 76a5472

Please sign in to comment.