From a6aab98650eea6a12f1c748f7ab7d6c0bba37afc Mon Sep 17 00:00:00 2001 From: eli-d <64763513+eli-d@users.noreply.github.com> Date: Mon, 9 Dec 2024 13:09:05 +1030 Subject: [PATCH] estimate gas using wagmi hook instead of viem action --- web/src/components/SwapForm.tsx | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/web/src/components/SwapForm.tsx b/web/src/components/SwapForm.tsx index b86f7693..b9fcd29e 100644 --- a/web/src/components/SwapForm.tsx +++ b/web/src/components/SwapForm.tsx @@ -25,6 +25,7 @@ import { useClient, useChainId, useConnectorClient, + useEstimateGas, } from "wagmi"; import { formatEther, maxUint256 } from "viem"; import { useWeb3Modal } from "@web3modal/wagmi/react"; @@ -252,25 +253,14 @@ export const SwapForm = () => { ammContract, ]); - // TODO this is in ETH(/SPN), not USD + // this is in ETH(/SPN), not USD + const estimatedGas = useEstimateGas({ + ...swapOptions, + account: address, + }); useEffect(() => { - (async () => { - if (!client || !address || BigInt(token0AmountRaw ?? 0) === 0n) return; - try { - const estimatedGas = await estimateContractGas(client, { - ...swapOptions, - // Typescript doesn't support strongly typing this with destructuring - // https://github.com/microsoft/TypeScript/issues/46680 - // @ts-expect-error - args: swapOptions.args, - account: address, - }); - setGas(estimatedGas); - } catch (e) { - console.error(e); - } - })(); - }, [address, client, token1, token0AmountRaw, setGas, swapOptions]); + setGas(estimatedGas.data ?? 0n); + }, [estimatedGas.data, setGas]); const { error: quote2Error, isLoading: quote2IsLoading } = useSimulateContract({