-
Notifications
You must be signed in to change notification settings - Fork 0
SOV-5270: allow withdrawing supplied balances #20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
creed-victor
merged 3 commits into
develop
from
feat/SOV-5270-as-an-user-i-want-to-withdraw
Dec 16, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
224 changes: 224 additions & 0 deletions
224
apps/web-app/src/components/MoneyMarket/components/WithdrawDialog/WithdrawDialog.tsx
This file contains hidden or 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,224 @@ | ||
| import { AmountRenderer } from '@/components/ui/amount-renderer'; | ||
| import { Button } from '@/components/ui/button'; | ||
| import { | ||
| Dialog, | ||
| DialogClose, | ||
| DialogContent, | ||
| DialogDescription, | ||
| DialogFooter, | ||
| DialogHeader, | ||
| DialogTitle, | ||
| } from '@/components/ui/dialog'; | ||
| import { Item, ItemContent, ItemGroup } from '@/components/ui/item'; | ||
| import { useAppForm } from '@/hooks/app-form'; | ||
| import { sdk } from '@/lib/sdk'; | ||
| import { useSlayerTx } from '@/lib/transactions'; | ||
| import { shouldUseFullAmount } from '@/lib/utils'; | ||
| import { validateDecimal } from '@/lib/validations'; | ||
| import { Decimal } from '@sovryn/slayer-shared'; | ||
| import { useMemo } from 'react'; | ||
| import { useAccount } from 'wagmi'; | ||
| import z from 'zod'; | ||
| import { useStore } from 'zustand'; | ||
| import { useStoreWithEqualityFn } from 'zustand/traditional'; | ||
| import { MINIMUM_HEALTH_FACTOR } from '../../constants'; | ||
| import { useMoneyMarketPositions } from '../../hooks/use-money-positions'; | ||
| import { withdrawRequestStore } from '../../stores/withdraw-request.store'; | ||
|
|
||
| const WithdrawDialogForm = () => { | ||
| const { address } = useAccount(); | ||
|
|
||
| const position = useStore(withdrawRequestStore, (state) => state.position!); | ||
|
|
||
| const { data } = useMoneyMarketPositions({ | ||
| pool: position.pool.id || 'default', | ||
| address: address!, | ||
| }); | ||
|
|
||
| const { begin } = useSlayerTx({ | ||
| onClosed: (ok: boolean) => { | ||
| if (ok) { | ||
| // close withdrawal dialog if tx was successful | ||
| withdrawRequestStore.getState().reset(); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const maximumWithdrawAmount = useMemo(() => { | ||
| const summary = data?.data?.summary; | ||
| if (!summary) { | ||
| return Decimal.ZERO; | ||
| } | ||
|
|
||
| // if user has no borrows or this position is not used as collateral, allow full withdrawal | ||
| if (Decimal.from(summary.totalBorrowsUsd).eq(0) || !position.collateral) { | ||
| return Decimal.from(position.supplied, position.token.decimals); | ||
| } | ||
|
|
||
| // min collateral at which we reach minimum collateral ratio | ||
| const minCollateralUsd = Decimal.from(MINIMUM_HEALTH_FACTOR) | ||
| .mul(summary.totalBorrowsUsd) | ||
| .div(summary.currentLiquidationThreshold); | ||
| const maxWithdrawUsd = Decimal.from(summary.supplyBalanceUsd).sub( | ||
| minCollateralUsd, | ||
| ); | ||
|
|
||
| if (maxWithdrawUsd.lte(0)) { | ||
| return Decimal.ZERO; | ||
| } | ||
|
|
||
| return maxWithdrawUsd.gt(position.suppliedUsd) | ||
| ? Decimal.from(position.supplied, position.token.decimals) | ||
| : maxWithdrawUsd.div(position.reserve.priceUsd); | ||
| }, [ | ||
| data, | ||
| position.collateral, | ||
| position.reserve.priceUsd, | ||
| position.supplied, | ||
| position.suppliedUsd, | ||
| position.token.decimals, | ||
| ]); | ||
|
|
||
| const balance = useMemo( | ||
| () => ({ | ||
| value: maximumWithdrawAmount.toBigInt(), | ||
| decimals: position.token.decimals, | ||
| symbol: position.token.symbol, | ||
| }), | ||
| [position, maximumWithdrawAmount], | ||
| ); | ||
|
|
||
| const form = useAppForm({ | ||
| defaultValues: { | ||
| amount: '', | ||
| }, | ||
| validators: { | ||
| onChange: z.object({ | ||
| amount: validateDecimal({ | ||
| min: 1n, | ||
| max: balance.value ?? undefined, | ||
| }), | ||
| }), | ||
| }, | ||
| onSubmit: ({ value }) => { | ||
| begin(() => | ||
| sdk.moneyMarket.withdraw( | ||
| { | ||
| ...position.reserve, | ||
| pool: position.pool, | ||
| token: position.token, | ||
| }, | ||
| value.amount, | ||
| // if position can be withdrawn in full and user entered near full amount, use full withdrawal to avoid dust issues | ||
| maximumWithdrawAmount.eq(position.supplied) && | ||
| shouldUseFullAmount(value.amount, position.supplied), | ||
| { | ||
| account: address!, | ||
| }, | ||
| ), | ||
| ); | ||
| }, | ||
| onSubmitInvalid(props) { | ||
| console.log('Withdraw request submission invalid:', props); | ||
| }, | ||
| onSubmitMeta() { | ||
| console.log('Withdraw request submission meta:', form); | ||
| }, | ||
| }); | ||
|
|
||
| const handleSubmit = (e: React.FormEvent) => { | ||
| e.preventDefault(); | ||
| e.stopPropagation(); | ||
| form.handleSubmit(); | ||
| }; | ||
|
|
||
| const handleEscapes = (e: Event) => { | ||
| // withdrawRequestStore.getState().reset(); | ||
creed-victor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| e.preventDefault(); | ||
| }; | ||
|
|
||
| const calculateRemainingSupply = (withdrawAmount: string) => { | ||
| const amount = Decimal.from(withdrawAmount || '0', position.token.decimals); | ||
| const current = Decimal.from(position.supplied, position.token.decimals); | ||
| if (amount.gt(current)) { | ||
| return Decimal.ZERO.toString(); | ||
| } | ||
| return Decimal.from(position.supplied, position.token.decimals) | ||
| .sub(withdrawAmount || '0') | ||
| .toString(); | ||
| }; | ||
|
|
||
| return ( | ||
| <form onSubmit={handleSubmit} id={form.formId}> | ||
| <DialogContent | ||
| onInteractOutside={handleEscapes} | ||
| onEscapeKeyDown={handleEscapes} | ||
| onOpenAutoFocus={(e) => e.preventDefault()} | ||
| > | ||
| <DialogHeader> | ||
| <DialogTitle>Withdraw Asset</DialogTitle> | ||
| <DialogDescription className="sr-only"> | ||
| Withdraw your supplied assets from the money market. | ||
| </DialogDescription> | ||
| </DialogHeader> | ||
| <form.AppField name="amount"> | ||
| {(field) => ( | ||
| <field.AmountField | ||
| label="Amount to Withdraw" | ||
| placeholder="Amount" | ||
| balance={balance} | ||
| addonRight={balance.symbol} | ||
| /> | ||
| )} | ||
| </form.AppField> | ||
|
|
||
| <form.Subscribe selector={(state) => state.values.amount}> | ||
| {(withdrawAmount) => ( | ||
| <ItemGroup> | ||
| <Item size="sm" className="py-1"> | ||
| <ItemContent>Remaining supply:</ItemContent> | ||
| <ItemContent> | ||
| <AmountRenderer | ||
| value={calculateRemainingSupply(withdrawAmount)} | ||
| suffix={position.token.symbol} | ||
| showApproxSign | ||
| /> | ||
| </ItemContent> | ||
| </Item> | ||
| </ItemGroup> | ||
| )} | ||
| </form.Subscribe> | ||
|
|
||
| <DialogFooter> | ||
| <DialogClose asChild> | ||
| <Button variant="secondary" type="button"> | ||
| Close | ||
| </Button> | ||
| </DialogClose> | ||
| <form.AppForm> | ||
| <form.SubscribeButton label="Withdraw" /> | ||
| </form.AppForm> | ||
| </DialogFooter> | ||
| </DialogContent> | ||
| </form> | ||
| ); | ||
| }; | ||
|
|
||
| export const WithdrawDialog = () => { | ||
| const isOpen = useStoreWithEqualityFn( | ||
| withdrawRequestStore, | ||
| (state) => state.position !== null, | ||
| ); | ||
|
|
||
| const handleClose = (open: boolean) => { | ||
| if (!open) { | ||
| withdrawRequestStore.getState().reset(); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <Dialog open={isOpen} onOpenChange={handleClose}> | ||
| {isOpen && <WithdrawDialogForm />} | ||
| </Dialog> | ||
| ); | ||
| }; | ||
This file contains hidden or 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 @@ | ||
| export const MINIMUM_HEALTH_FACTOR = 1.1; |
26 changes: 26 additions & 0 deletions
26
apps/web-app/src/components/MoneyMarket/stores/withdraw-request.store.ts
This file contains hidden or 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,26 @@ | ||
| import type { MoneyMarketPoolPosition } from '@sovryn/slayer-sdk'; | ||
| import { createStore } from 'zustand'; | ||
| import { combine } from 'zustand/middleware'; | ||
|
|
||
| type State = { | ||
| position: MoneyMarketPoolPosition | null; | ||
| }; | ||
|
|
||
| type Actions = { | ||
| setPosition: (position: MoneyMarketPoolPosition) => void; | ||
| reset: () => void; | ||
| }; | ||
|
|
||
| type WithdrawRequestStore = State & Actions; | ||
|
|
||
| export const withdrawRequestStore = createStore<WithdrawRequestStore>( | ||
| combine( | ||
| { | ||
| position: null as MoneyMarketPoolPosition | null, | ||
| }, | ||
| (set) => ({ | ||
| setPosition: (position: MoneyMarketPoolPosition) => set({ position }), | ||
| reset: () => set({ position: null }), | ||
| }), | ||
| ), | ||
| ); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.