Skip to content

Commit

Permalink
reuse withdraw prompt on unstake
Browse files Browse the repository at this point in the history
  • Loading branch information
alecananian committed Jan 3, 2025
1 parent 24ebd12 commit f9e7ccc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 54 deletions.
59 changes: 6 additions & 53 deletions app/components/pools/PoolIncentiveUnstake.tsx
Original file line number Diff line number Diff line change
@@ -1,59 +1,33 @@
import { useCallback, useState } from "react";
import { type Address, formatEther, parseEther, parseUnits } from "viem";
import { parseEther } from "viem";

import type { SerializeFrom } from "@remix-run/node";
import { useRouteLoaderData } from "@remix-run/react";
import { useAccount } from "~/contexts/account";
import { useTokenBalance } from "~/hooks/useTokenBalance";
import { useUnstake } from "~/hooks/useUnstake";
import { useWithdrawBatch } from "~/hooks/useWithdrawBatch";
import type { Pool } from "~/lib/pools.server";
import type { loader } from "~/routes/pools_.$id";
import type { NumberString } from "~/types";
import { SelectionPopup } from "../SelectionPopup";
import { TransactionButton } from "../ui/Button";
import { Dialog } from "../ui/Dialog";
import { PoolInput } from "./PoolInput";

type Props = {
pool: Pool;
staked: bigint;
onSuccess?: () => void;
};

export const PoolIncentiveUnstake = ({ pool, staked }: Props) => {
const { poolIncentives } = useRouteLoaderData(
"routes/pools_.$id",
) as SerializeFrom<typeof loader>;
const [isClaimDialogOpen, setIsClaimDialogOpen] = useState(false);

const nftIncentive = [...poolIncentives]
.sort((a, b) => Number(b.startTime) - Number(a.startTime))
.find((poolIncentive) => poolIncentive.rewardToken?.isNFT);

export const PoolIncentiveUnstake = ({ pool, staked, onSuccess }: Props) => {
const [rawAmount, setRawAmount] = useState("0");
const { isConnected, address } = useAccount();
const { isConnected } = useAccount();

const amount = parseEther(rawAmount as NumberString);
const hasAmount = amount > 0;

const { withdrawBatch, isLoading } = useWithdrawBatch({
vaultAddress: nftIncentive?.rewardTokenAddress as Address | undefined,
});

const { data: nftRewardsBalance, refetch: nftRewardsRefetch } =
useTokenBalance({
id: nftIncentive?.rewardTokenAddress as Address | undefined,
address,
});

const { unstake } = useUnstake({
pool,
amount,
onSuccess: useCallback(() => {
setRawAmount("0");
nftRewardsRefetch();
setIsClaimDialogOpen(true);
}, [nftRewardsRefetch]),
onSuccess?.();
}, [onSuccess]),
});

return (
Expand All @@ -73,27 +47,6 @@ export const PoolIncentiveUnstake = ({ pool, staked }: Props) => {
>
Unstake
</TransactionButton>
{nftIncentive?.rewardToken && (
<Dialog open={isClaimDialogOpen} onOpenChange={() => {}}>
<SelectionPopup
type="vault"
token={nftIncentive.rewardToken}
onSubmit={async (tokens) => {
await withdrawBatch(
tokens.map((token) => ({
id: BigInt(token.tokenId),
amount: BigInt(token.quantity),
collectionId: token.collectionAddr as Address,
})),
);
setIsClaimDialogOpen(false);
}}
isSubmitDisabled={isLoading}
keepOpenOnSubmit
requiredAmount={Number(formatEther(nftRewardsBalance))}
/>
</Dialog>
)}
</>
);
};
6 changes: 5 additions & 1 deletion app/routes/pools_.$id.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ const PoolManagementView = ({
/>
) : null}
{tab === "unstake" ? (
<PoolIncentiveUnstake pool={pool} staked={lpStaked} />
<PoolIncentiveUnstake
pool={pool}
staked={lpStaked}
onSuccess={onSuccess}
/>
) : null}
</div>
);
Expand Down

0 comments on commit f9e7ccc

Please sign in to comment.