Skip to content

Commit 684dcaa

Browse files
authored
Merge pull request #1540 from mars-protocol/vault-withdraw
Vault withdraw
2 parents 54590e8 + ff845ac commit 684dcaa

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

src/components/managedVaults/vaultDetails/table/columns/WithdrawButton.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import Button from 'components/common/Button'
22
import { AccountArrowDown } from 'components/common/Icons'
33
import { useCallback, useState } from 'react'
44
import useStore from 'store'
5+
import useCurrentWalletBalance from 'hooks/wallet/useCurrentWalletBalance'
6+
import { BN } from 'utils/helpers'
7+
import BigNumber from 'bignumber.js'
58

69
export const WITHDRAW_META = {
710
id: 'withdraw',
@@ -21,13 +24,19 @@ export default function WithdrawButton(props: Props) {
2124
const address = useStore((s) => s.address)
2225
const [isConfirming, setIsConfirming] = useState(false)
2326
const withdrawFromManagedVault = useStore((s) => s.withdrawFromManagedVault)
27+
const walletBalance = useCurrentWalletBalance(vaultTokenDenom)
2428

2529
const handleWithdraw = useCallback(async () => {
2630
setIsConfirming(true)
2731
try {
32+
// Ensure the withdrawal amount never exceeds the user's wallet balance
33+
const walletBalanceAmount = BN(walletBalance?.amount || '0')
34+
const requestedAmount = BN(amount)
35+
const actualAmount = BigNumber.minimum(requestedAmount, walletBalanceAmount).toString()
36+
2837
await withdrawFromManagedVault({
2938
vaultAddress,
30-
amount,
39+
amount: actualAmount,
3140
vaultToken: vaultTokenDenom,
3241
recipient: address,
3342
})
@@ -36,7 +45,14 @@ export default function WithdrawButton(props: Props) {
3645
} finally {
3746
setIsConfirming(false)
3847
}
39-
}, [address, amount, vaultAddress, vaultTokenDenom, withdrawFromManagedVault])
48+
}, [
49+
address,
50+
amount,
51+
vaultAddress,
52+
vaultTokenDenom,
53+
withdrawFromManagedVault,
54+
walletBalance?.amount,
55+
])
4056

4157
return (
4258
<Button

src/components/managedVaults/vaultDetails/table/useUserWithdrawals.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { useMemo } from 'react'
66
import { BNCoin } from 'types/classes/BNCoin'
77
import { BN } from 'utils/helpers'
88
import { FormattedNumber } from 'components/common/FormattedNumber'
9+
import { Tooltip } from 'components/common/Tooltip'
10+
import { InfoCircle } from 'components/common/Icons'
911

1012
interface Props {
1113
isLoading: boolean
@@ -22,6 +24,17 @@ export default function useUserWithdrawals(props: Props) {
2224
id: 'name',
2325
accessorKey: 'Unlock Amount',
2426
meta: { className: 'min-w-30' },
27+
header: () => (
28+
<div className='flex items-center gap-1'>
29+
<span>Unlock Amount</span>
30+
<Tooltip
31+
content='Dollar values are estimates and may change based on current vault share price when you withdraw.'
32+
type='info'
33+
>
34+
<InfoCircle className='w-3 h-3 text-white/50 hover:text-white/70' />
35+
</Tooltip>
36+
</div>
37+
),
2538
cell: ({ row }) => {
2639
const coin = BNCoin.fromDenomAndBigNumber(
2740
details.base_tokens_denom,

0 commit comments

Comments
 (0)