Skip to content

Commit

Permalink
fix type issue with wrapped useWriteContract
Browse files Browse the repository at this point in the history
  • Loading branch information
eli-d committed Dec 12, 2024
1 parent 5c526e5 commit c740f9e
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 78 deletions.
14 changes: 7 additions & 7 deletions web/src/app/stake/pool/confirm-withdraw/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export default function ConfirmWithdrawLiquidity() {
const {
token0,
token0Amount,
token0AmountRaw,
token1,
token1Amount,
delta,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 11 additions & 13 deletions web/src/components/ConfirmStake.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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],
});

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -266,7 +263,8 @@ export const ConfirmStake = ({
tickLower === undefined ||
tickUpper === undefined ||
tickLower >= tickUpper ||
!tickSpacing
!tickSpacing ||
!address
)
return;

Expand Down Expand Up @@ -359,7 +357,7 @@ export const ConfirmStake = ({
address: token1.address,
abi: token1.abi,
functionName: "approve",
args: [allowanceContract, token1AmountRaw],
args: [allowanceContract, BigInt(token1AmountRaw)],
});
} else {
handlePositionAction();
Expand Down Expand Up @@ -387,7 +385,7 @@ export const ConfirmStake = ({
address: token0.address,
abi: token0.abi,
functionName: "approve",
args: [allowanceContract, token0AmountRaw],
args: [allowanceContract, BigInt(token0AmountRaw)],
});
} else {
approveToken1();
Expand Down
91 changes: 41 additions & 50 deletions web/src/components/ConfirmSwap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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],
});

Expand Down Expand Up @@ -219,31 +178,63 @@ 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))
) {
writeContractApproval({
address: token0.address,
abi: token0.abi,
functionName: "approve",
args: [ammContract.address, token0AmountRaw],
args: [ammContract.address, BigInt(token0AmountRaw)],
});
} else {
performSwap();
Expand Down
12 changes: 4 additions & 8 deletions web/src/fixtures/wagmi/useWriteContract.tsx
Original file line number Diff line number Diff line change
@@ -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> = T extends (variables: infer V, ...args: any[]) => void
? V
: never;

export default function useWriteContract() {
const {
writeContractAsync: baseWriteContractAsync,
Expand All @@ -26,12 +21,13 @@ export default function useWriteContract() {
[setError, setIsOpen],
);

const writeContractAsync = useCallback(
async function (props: VariablesType<WriteContractMutateAsync<Config>>) {
const writeContractAsync = useCallback<typeof baseWriteContractAsync>(
async function (props) {
try {
return await baseWriteContractAsync(props);
} catch (error) {
handleError(error);
return "0x";
}
},
[baseWriteContractAsync, handleError],
Expand Down

0 comments on commit c740f9e

Please sign in to comment.