-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d360f84
commit e9129d1
Showing
7 changed files
with
262 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
148 changes: 148 additions & 0 deletions
148
apps/cow-amm-deployer/src/app/amms/[id]/withdraw/(components)/WithdrawForm.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
import { formatNumber, toast } from "@bleu/ui"; | ||
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import * as Slider from "@radix-ui/react-slider"; | ||
import { useRouter } from "next/navigation"; | ||
import { Controller, useForm } from "react-hook-form"; | ||
import { parseUnits } from "viem"; | ||
|
||
import { Button } from "#/components/Button"; | ||
import Table from "#/components/Table"; | ||
import { TokenInfo } from "#/components/TokenInfo"; | ||
import { Form } from "#/components/ui/form"; | ||
import { useRawTxData } from "#/hooks/useRawTxData"; | ||
import { ammWithdrawSchema } from "#/lib/schema"; | ||
import { | ||
TRANSACTION_TYPES, | ||
withdrawCowAMMargs, | ||
} from "#/lib/transactionFactory"; | ||
import { ICowAmm } from "#/lib/types"; | ||
import { ChainId } from "#/utils/chainsPublicClients"; | ||
|
||
export function WithdrawForm({ cowAmm }: { cowAmm: ICowAmm }) { | ||
const form = useForm<typeof ammWithdrawSchema._type>({ | ||
resolver: zodResolver(ammWithdrawSchema), | ||
defaultValues: { | ||
withdrawPct: 50, | ||
}, | ||
}); | ||
const router = useRouter(); | ||
const { sendTransactions } = useRawTxData(); | ||
const { | ||
safe: { chainId }, | ||
} = useSafeAppsSDK(); | ||
|
||
const onSubmit = async (data: typeof ammWithdrawSchema._type) => { | ||
const amount0 = parseUnits( | ||
String((Number(cowAmm.token0.balance) * data.withdrawPct) / 100), | ||
cowAmm.token0.decimals | ||
); | ||
const amount1 = parseUnits( | ||
String((Number(cowAmm.token1.balance) * data.withdrawPct) / 100), | ||
cowAmm.token1.decimals | ||
); | ||
const txArgs = { | ||
type: TRANSACTION_TYPES.WITHDRAW, | ||
amm: cowAmm.constantProductAddress, | ||
amount0, | ||
amount1, | ||
chainId: chainId as ChainId, | ||
} as withdrawCowAMMargs; | ||
try { | ||
await sendTransactions([txArgs]); | ||
router.push(`/amms/${cowAmm.id}`); | ||
} catch { | ||
toast({ | ||
title: `Transaction failed`, | ||
description: "An error occurred while processing the transaction.", | ||
variant: "destructive", | ||
}); | ||
} | ||
}; | ||
|
||
const { | ||
control, | ||
formState: { isSubmitting }, | ||
} = form; | ||
|
||
const { withdrawPct } = form.watch(); | ||
|
||
return ( | ||
<Form | ||
{...form} | ||
onSubmit={onSubmit} | ||
className="flex flex-col gap-y-3 px-9 pb-9" | ||
> | ||
<div className="flex flex-col w-full"> | ||
<span className="mb-2 h-5 block"> | ||
Withdraw percentage: {withdrawPct}% | ||
</span> | ||
<Controller | ||
name="withdrawPct" | ||
control={control} | ||
render={({ field }) => ( | ||
<Slider.Root | ||
className="relative flex items-center select-none touch-none w-full h-5" | ||
max={100} | ||
step={0.1} | ||
min={0.1} | ||
onValueChange={field.onChange} | ||
value={[field.value]} | ||
> | ||
<Slider.Track className="relative grow rounded-full h-[3px]"> | ||
<Slider.Range className="absolute bg-primary rounded-full h-full" /> | ||
</Slider.Track> | ||
<Slider.Thumb className="block w-5 h-5 bg-primary rounded-[10px] hover:bg-primary/90 focus:outline-none" /> | ||
</Slider.Root> | ||
)} | ||
/> | ||
</div> | ||
<Table color="sand" classNames="overflow-y-auto"> | ||
<Table.HeaderRow> | ||
<Table.HeaderCell>Balance</Table.HeaderCell> | ||
<Table.HeaderCell> | ||
Withdraw ($ | ||
{formatNumber((cowAmm.totalUsdValue * withdrawPct) / 100, 4)}) | ||
</Table.HeaderCell> | ||
</Table.HeaderRow> | ||
<Table.Body> | ||
{[cowAmm.token0, cowAmm.token1].map((token) => { | ||
return ( | ||
<Table.BodyRow key={token.address}> | ||
<Table.BodyCell> | ||
<TokenInfo token={token} /> | ||
</Table.BodyCell> | ||
<Table.BodyCell> | ||
<div className="flex flex-col gap-1 justify-end"> | ||
<span className="font-semibold"> | ||
{formatNumber( | ||
(Number(token.balance) * withdrawPct) / 100, | ||
4 | ||
)}{" "} | ||
{token.symbol} | ||
</span> | ||
<span className="text-sm text-background/50"> | ||
$ | ||
{formatNumber( | ||
(Number(token.usdValue) * withdrawPct) / 100, | ||
4 | ||
)} | ||
</span> | ||
</div> | ||
</Table.BodyCell> | ||
</Table.BodyRow> | ||
); | ||
})} | ||
</Table.Body> | ||
</Table> | ||
<Button | ||
variant="highlight" | ||
type="submit" | ||
className="w-full" | ||
disabled={isSubmitting} | ||
> | ||
Withdraw | ||
</Button> | ||
</Form> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use client"; | ||
|
||
import { useSafeAppsSDK } from "@gnosis.pm/safe-apps-react-sdk"; | ||
import { ArrowLeftIcon } from "@radix-ui/react-icons"; | ||
|
||
import { LinkComponent } from "#/components/Link"; | ||
import { Spinner } from "#/components/Spinner"; | ||
import { UnsuportedChain } from "#/components/UnsuportedChain"; | ||
import WalletNotConnected from "#/components/WalletNotConnected"; | ||
import { useStandaloneAMM } from "#/hooks/useStandaloneAmm"; | ||
import { supportedChainIds } from "#/utils/chainsPublicClients"; | ||
|
||
import { WithdrawForm } from "./(components)/WithdrawForm"; | ||
|
||
export default function Page({ params }: { params: { id: `0x${string}` } }) { | ||
const { data: cowAmm, loading } = useStandaloneAMM(params.id); | ||
|
||
const { safe, connected } = useSafeAppsSDK(); | ||
|
||
if (!connected) { | ||
return <WalletNotConnected />; | ||
} | ||
|
||
if (!cowAmm || loading) { | ||
return <Spinner />; | ||
} | ||
|
||
if (!supportedChainIds.includes(safe.chainId)) { | ||
return <UnsuportedChain />; | ||
} | ||
|
||
return ( | ||
<div className="flex size-full items-center justify-center"> | ||
<div className="my-4 flex flex-col border-2 border-foreground bg-card border-card-foreground text-card-foreground"> | ||
<div className="relative flex size-full justify-center"> | ||
<LinkComponent | ||
href={`/amms/${params.id}`} | ||
content={ | ||
<div className="absolute left-8 flex h-full items-center"> | ||
<ArrowLeftIcon | ||
height={16} | ||
width={16} | ||
className="text-background duration-200 hover:text-highlight" | ||
/>{" "} | ||
</div> | ||
} | ||
/> | ||
<div className="flex w-[530px] flex-col items-center py-3"> | ||
<div className="text-xl">Proportional withdraw</div> | ||
</div> | ||
</div> | ||
<div className="flex flex-col w-[530px] overflow-auto size-full max-h-[550px]"> | ||
<WithdrawForm cowAmm={cowAmm} /> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.