Skip to content

Commit

Permalink
Merge pull request #81 from 00labs/withdraw-lp-check
Browse files Browse the repository at this point in the history
Take min of pool balance or lender position for withdraw
  • Loading branch information
mliu authored Nov 13, 2023
2 parents 0a076b2 + be89942 commit fa13511
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<number>(lenderPosition, decimals)
// Take the minimum of the pool balance or the lender position
const withdrawableAmountBN = lenderPosition.lt(poolBalance)
? lenderPosition
: poolBalance
const withdrawableAmount = downScale<number>(withdrawableAmountBN, decimals)

const handleChangeAmount = useCallback(
(newAmount: number) => {
Expand Down
15 changes: 11 additions & 4 deletions packages/huma-widget/src/components/Lend/withdraw/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
POOL_NAME,
POOL_TYPE,
useLenderPosition,
usePoolBalance,
usePoolInfo,
} from '@huma-finance/shared'
import React, { useCallback, useEffect } from 'react'
Expand Down Expand Up @@ -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) {
Expand All @@ -82,7 +85,11 @@ export function LendWithdraw({
<CheckWithdrawable poolInfo={poolInfo} handleClose={handleClose} />
)}
{step === WIDGET_STEP.ChooseAmount && (
<ChooseAmount poolInfo={poolInfo} lenderPosition={lenderPosition!} />
<ChooseAmount
poolInfo={poolInfo}
lenderPosition={lenderPosition!}
poolBalance={poolBalance!}
/>
)}
{step === WIDGET_STEP.Transfer && <Transfer poolInfo={poolInfo} />}
{step === WIDGET_STEP.Done && (
Expand Down

0 comments on commit fa13511

Please sign in to comment.