diff --git a/packages/huma-widget/src/components/Lend/withdraw/2-ChooseAmount.tsx b/packages/huma-widget/src/components/Lend/withdraw/2-ChooseAmount.tsx index 1a7013b..0827011 100644 --- a/packages/huma-widget/src/components/Lend/withdraw/2-ChooseAmount.tsx +++ b/packages/huma-widget/src/components/Lend/withdraw/2-ChooseAmount.tsx @@ -9,18 +9,24 @@ import { ChooseAmountModal } from '../../ChooseAmountModal' type Props = { lenderPosition: BigNumber + poolBalance: BigNumber poolInfo: PoolInfoType } export function ChooseAmount({ lenderPosition, + poolBalance, poolInfo, }: Props): React.ReactElement | null { const dispatch = useAppDispatch() const { poolUnderlyingToken } = poolInfo const { symbol, decimals } = poolUnderlyingToken const [currentAmount, setCurrentAmount] = useState(0) - const withdrawableAmount = downScale(lenderPosition, decimals) + // Take the minimum of the pool balance or the lender position + const withdrawableAmountBN = lenderPosition.lt(poolBalance) + ? lenderPosition + : poolBalance + const withdrawableAmount = downScale(withdrawableAmountBN, decimals) const handleChangeAmount = useCallback( (newAmount: number) => { diff --git a/packages/huma-widget/src/components/Lend/withdraw/index.tsx b/packages/huma-widget/src/components/Lend/withdraw/index.tsx index 983cf62..677381c 100644 --- a/packages/huma-widget/src/components/Lend/withdraw/index.tsx +++ b/packages/huma-widget/src/components/Lend/withdraw/index.tsx @@ -3,6 +3,7 @@ import { POOL_NAME, POOL_TYPE, useLenderPosition, + usePoolBalance, usePoolInfo, } from '@huma-finance/shared' import React, { useCallback, useEffect } from 'react' @@ -51,21 +52,23 @@ export function LendWithdraw({ poolType, account, ) + const [poolBalance, refreshPoolBalance] = usePoolBalance(poolName, poolType) useEffect(() => { - if (!step && lenderPosition) { + if (!step && lenderPosition && poolBalance) { dispatch(setStep(WIDGET_STEP.CheckWithdrawable)) } - }, [dispatch, lenderPosition, step]) + }, [dispatch, lenderPosition, poolBalance, step]) const handleWithdrawSuccess = useCallback( (blockNumber: number) => { refreshLenderPosition() + refreshPoolBalance() if (handleSuccess) { handleSuccess(blockNumber) } }, - [handleSuccess, refreshLenderPosition], + [handleSuccess, refreshLenderPosition, refreshPoolBalance], ) if (!poolInfo) { @@ -82,7 +85,11 @@ export function LendWithdraw({ )} {step === WIDGET_STEP.ChooseAmount && ( - + )} {step === WIDGET_STEP.Transfer && } {step === WIDGET_STEP.Done && (