@@ -2,6 +2,9 @@ import Button from 'components/common/Button'
22import { AccountArrowDown } from 'components/common/Icons'
33import { useCallback , useState } from 'react'
44import useStore from 'store'
5+ import useCurrentWalletBalance from 'hooks/wallet/useCurrentWalletBalance'
6+ import { BN } from 'utils/helpers'
7+ import BigNumber from 'bignumber.js'
58
69export 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
0 commit comments