From c740f9e6de011482324c2624a168de784aa173c8 Mon Sep 17 00:00:00 2001 From: eli-d <64763513+eli-d@users.noreply.github.com> Date: Thu, 12 Dec 2024 15:26:33 +1030 Subject: [PATCH] fix type issue with wrapped `useWriteContract` --- .../app/stake/pool/confirm-withdraw/page.tsx | 14 +-- web/src/components/ConfirmStake.tsx | 24 +++-- web/src/components/ConfirmSwap.tsx | 91 +++++++++---------- web/src/fixtures/wagmi/useWriteContract.tsx | 12 +-- 4 files changed, 63 insertions(+), 78 deletions(-) diff --git a/web/src/app/stake/pool/confirm-withdraw/page.tsx b/web/src/app/stake/pool/confirm-withdraw/page.tsx index d815f0a8..d19fe104 100644 --- a/web/src/app/stake/pool/confirm-withdraw/page.tsx +++ b/web/src/app/stake/pool/confirm-withdraw/page.tsx @@ -49,7 +49,6 @@ export default function ConfirmWithdrawLiquidity() { const { token0, token0Amount, - token0AmountRaw, token1, token1Amount, delta, @@ -131,12 +130,13 @@ export default function ConfirmWithdrawLiquidity() { const divestPosition = useCallback( (id: bigint) => { - writeContractDivestPosition({ - address: leoContract.address, - abi: leoContract.abi, - functionName: "divestPosition", - args: [BigInt(id ?? 0), address], - }); + address && + writeContractDivestPosition({ + address: leoContract.address, + abi: leoContract.abi, + functionName: "divestPosition", + args: [BigInt(id ?? 0), address], + }); }, [ writeContractDivestPosition, diff --git a/web/src/components/ConfirmStake.tsx b/web/src/components/ConfirmStake.tsx index 98415fb0..a9529340 100644 --- a/web/src/components/ConfirmStake.tsx +++ b/web/src/components/ConfirmStake.tsx @@ -122,18 +122,14 @@ export const ConfirmStake = ({ const { data: allowanceDataToken0 } = useSimulateContract({ address: token0.address, abi: token0.abi, - // @ts-ignore this needs to use useSimulateContract which breaks the types functionName: "allowance", - // @ts-ignore args: [address as Hash, allowanceContract], }); const { data: allowanceDataToken1 } = useSimulateContract({ address: token1.address, abi: token1.abi, - // @ts-ignore this needs to use useSimulateContract which breaks the types functionName: "allowance", - // @ts-ignore args: [address as Hash, allowanceContract], }); @@ -210,12 +206,13 @@ export const ConfirmStake = ({ // divest if already vested before updating const divestPosition = useCallback( (id: bigint) => { - writeContractDivestPosition({ - address: leoContract.address, - abi: leoContract.abi, - functionName: "divestPosition", - args: [BigInt(id ?? 0), address], - }); + address && + writeContractDivestPosition({ + address: leoContract.address, + abi: leoContract.abi, + functionName: "divestPosition", + args: [BigInt(id ?? 0), address], + }); }, [ writeContractDivestPosition, @@ -266,7 +263,8 @@ export const ConfirmStake = ({ tickLower === undefined || tickUpper === undefined || tickLower >= tickUpper || - !tickSpacing + !tickSpacing || + !address ) return; @@ -359,7 +357,7 @@ export const ConfirmStake = ({ address: token1.address, abi: token1.abi, functionName: "approve", - args: [allowanceContract, token1AmountRaw], + args: [allowanceContract, BigInt(token1AmountRaw)], }); } else { handlePositionAction(); @@ -387,7 +385,7 @@ export const ConfirmStake = ({ address: token0.address, abi: token0.abi, functionName: "approve", - args: [allowanceContract, token0AmountRaw], + args: [allowanceContract, BigInt(token0AmountRaw)], }); } else { approveToken1(); diff --git a/web/src/components/ConfirmSwap.tsx b/web/src/components/ConfirmSwap.tsx index bfb4904c..bd41f33e 100644 --- a/web/src/components/ConfirmSwap.tsx +++ b/web/src/components/ConfirmSwap.tsx @@ -2,7 +2,7 @@ import { Button } from "@/components/ui/button"; import { redirect, useRouter } from "next/navigation"; -import { cn, getAmountFromMaybeTransfer, TransferTopic } from "@/lib/utils"; +import { cn, getAmountFromMaybeTransfer } from "@/lib/utils"; import { useSwapStore } from "@/stores/useSwapStore"; import { motion } from "framer-motion"; import { @@ -75,45 +75,6 @@ export const ConfirmSwap = () => { const isSwappingBaseAsset = token0.address === fUSDC.address; const isSwap1 = isSwappingBaseAsset || token1.address === fUSDC.address; - const swapOptions = useMemo(() => { - if (isSwappingBaseAsset) { - // if one of the assets is fusdc, use swap1 - return { - address: ammContract.address, - abi: ammContract.abi, - functionName: "swap904369BE", - args: [token1.address, false, BigInt(token0AmountRaw ?? 0), maxUint256], - } as const; - } else if (token1.address === fUSDC.address) { - return { - address: ammContract.address, - abi: ammContract.abi, - functionName: "swap904369BE", - args: [token0.address, true, BigInt(token0AmountRaw ?? 0), maxUint256], - } as const; - } else { - // if both of the assets aren't fusdc, use swap2 - return { - address: ammContract.address, - abi: ammContract.abi, - functionName: "swap2ExactIn41203F1D", - args: [ - token0.address, - token1.address, - BigInt(token0AmountRaw ?? 0), - BigInt(0), - ], - } as const; - } - }, [ - isSwappingBaseAsset, - ammContract, - token0AmountRaw, - token0.address, - token1.address, - fUSDC.address, - ]); - // set up write hooks const { writeContractAsync: writeContractApproval, @@ -182,9 +143,7 @@ export const ConfirmSwap = () => { const { data: allowanceData } = useSimulateContract({ address: token0.address, abi: token0.abi, - // @ts-ignore this needs to use useSimulateContract which breaks the types functionName: "allowance", - // @ts-ignore args: [address as Hash, ammContract.address], }); @@ -219,23 +178,55 @@ export const ConfirmSwap = () => { address: token0.address, abi: token0.abi, functionName: "deposit", - // TODO our type for wrapping writeContract incorrectly narrows this to undefined, when it should be bigint | undefined - // @ts-ignore value: BigInt(token0AmountRaw ?? 0n), }); } else approve(); }; const performSwap = useCallback(() => { - writeContractSwap({ - ...swapOptions, - args: swapOptions.args, - }); - }, [swapOptions, writeContractSwap]); + if (isSwappingBaseAsset) { + // if one of the assets is fusdc, use swap1 + writeContractSwap({ + address: ammContract.address, + abi: ammContract.abi, + functionName: "swap904369BE", + args: [token1.address, false, BigInt(token0AmountRaw ?? 0), maxUint256], + }); + } else if (token1.address === fUSDC.address) { + writeContractSwap({ + address: ammContract.address, + abi: ammContract.abi, + functionName: "swap904369BE", + args: [token0.address, true, BigInt(token0AmountRaw ?? 0), maxUint256], + }); + } else { + // if both of the assets aren't fusdc, use swap2 + writeContractSwap({ + address: ammContract.address, + abi: ammContract.abi, + functionName: "swap2ExactIn41203F1D", + args: [ + token0.address, + token1.address, + BigInt(token0AmountRaw ?? 0), + BigInt(0), + ], + }); + } + }, [ + writeContractSwap, + isSwappingBaseAsset, + ammContract, + token0AmountRaw, + token0.address, + token1.address, + fUSDC.address, + ]); const approve = useCallback(() => { if ( token0.abi && + token0AmountRaw !== undefined && (!allowanceData?.result || allowanceData.result < BigInt(token0AmountRaw ?? 0)) ) { @@ -243,7 +234,7 @@ export const ConfirmSwap = () => { address: token0.address, abi: token0.abi, functionName: "approve", - args: [ammContract.address, token0AmountRaw], + args: [ammContract.address, BigInt(token0AmountRaw)], }); } else { performSwap(); diff --git a/web/src/fixtures/wagmi/useWriteContract.tsx b/web/src/fixtures/wagmi/useWriteContract.tsx index d93b5bf8..188651da 100644 --- a/web/src/fixtures/wagmi/useWriteContract.tsx +++ b/web/src/fixtures/wagmi/useWriteContract.tsx @@ -1,12 +1,7 @@ -import { useWriteContract as baseUseWriteContract, Config } from "wagmi"; -import { WriteContractMutateAsync } from "wagmi/query"; +import { useWriteContract as baseUseWriteContract } from "wagmi"; import { useErrorReportingStore } from "@/stores/useErrorReport"; import { useCallback } from "react"; -type VariablesType = T extends (variables: infer V, ...args: any[]) => void - ? V - : never; - export default function useWriteContract() { const { writeContractAsync: baseWriteContractAsync, @@ -26,12 +21,13 @@ export default function useWriteContract() { [setError, setIsOpen], ); - const writeContractAsync = useCallback( - async function (props: VariablesType>) { + const writeContractAsync = useCallback( + async function (props) { try { return await baseWriteContractAsync(props); } catch (error) { handleError(error); + return "0x"; } }, [baseWriteContractAsync, handleError],