From 0dab1878c18c4568519ca7a831412bd9ee4a7ccc Mon Sep 17 00:00:00 2001 From: torof Date: Fri, 8 Nov 2024 11:51:51 +0100 Subject: [PATCH] fix(fixing adminaddresssetter): fixing the adminAddressSetter component with simulation issues --- src/app/app/[tab]/AppTabs.tsx | 5 +- src/components/admin/AdminAddressSetter.tsx | 72 +++++++++++++++------ 2 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/app/app/[tab]/AppTabs.tsx b/src/app/app/[tab]/AppTabs.tsx index 1ee8cbc9..82b0e184 100644 --- a/src/app/app/[tab]/AppTabs.tsx +++ b/src/app/app/[tab]/AppTabs.tsx @@ -58,7 +58,7 @@ const InnerAppTabs: FC = () => { Dashboard Affiliate Program Swap - + Bridge
@@ -83,6 +83,9 @@ const InnerAppTabs: FC = () => { + + +
diff --git a/src/components/admin/AdminAddressSetter.tsx b/src/components/admin/AdminAddressSetter.tsx index fedf11af..31b13d31 100644 --- a/src/components/admin/AdminAddressSetter.tsx +++ b/src/components/admin/AdminAddressSetter.tsx @@ -1,14 +1,13 @@ import { Address, Input, TxButton } from "@/components/ui"; import { useContractAddress } from "@/hooks/useContractAddress"; -import { ChangeEvent, FC, useEffect, useState } from "react"; +import { ChangeEvent, FC, useEffect, useCallback, useState } from "react"; import { - UseSimulateContractReturnType, useBlockNumber, useReadContract, useSimulateContract, + UseSimulateContractReturnType, } from "wagmi"; -import { getContractAbi } from "@/lib/getContractAbi"; -import { zeroAddress } from "viem"; +import { zeroAddress, Abi } from "viem"; import { useContractAbi } from "@/hooks/useContractAbi"; import { useQueryClient } from "@tanstack/react-query"; @@ -34,23 +33,57 @@ export const AdminAddressSetter: FC = ({ abi: contractAbi, functionName: getterFunctionName, }); + const [newAddress, setNewAddress] = useState(zeroAddress); - const preparation = useSimulateContract({ + const [hasUserInteracted, setHasUserInteracted] = useState(false); + + const simulation = useSimulateContract({ address: contractAddress, - abi: contractAbi, + abi: contractAbi as Abi, functionName: setterFunctionName, - args: [newAddress], + args: [newAddress] as const, + query: { + enabled: Boolean(contractAddress && contractAbi && newAddress && newAddress !== zeroAddress), + }, }); - const [hasUserInteracted, setHasUserInteracted] = useState(false); - // Refresh some data every 5 blocks - const queryKeys = [queryKey]; + // Convert simulation to expected type + const preparation = { + ...simulation, + data: simulation.data + ? { + ...simulation.data, + request: { + ...simulation.data.request, + __mode: "prepared" as const, + }, + } + : undefined, + } as UseSimulateContractReturnType; + + // Refresh data every 5 blocks const { data: blockNumber } = useBlockNumber({ watch: true }); const queryClient = useQueryClient(); + + const handleRefreshData = useCallback(() => { + if (queryKey) { + queryClient.invalidateQueries({ queryKey }); + } + }, [queryClient, queryKey]); + useEffect(() => { - if (blockNumber && blockNumber % 5n === 0n) - queryKeys.forEach((k) => queryClient.invalidateQueries({ queryKey: k })); - }, [blockNumber, ...queryKeys]); + if (blockNumber && blockNumber % 5n === 0n) { + handleRefreshData(); + } + }, [blockNumber, handleRefreshData]); + + const handleAddressChange = (e: ChangeEvent) => { + const value = e.target.value; + setNewAddress(value); + setHasUserInteracted(value !== ""); + }; + + if (!contractAddress || !contractAbi) return null; return (
@@ -61,16 +94,13 @@ export const AdminAddressSetter: FC = ({
) => { - setNewAddress(e.target.value); - if (hasUserInteracted === false) setHasUserInteracted(true); - if (e.target.value === "") setHasUserInteracted(false); - }} + onChange={handleAddressChange} + placeholder="Enter new address" /> {txButtonName} @@ -78,4 +108,4 @@ export const AdminAddressSetter: FC = ({
); -}; +}; \ No newline at end of file