fix: fixed unlend on withdraw with borrow (#720)

* fix: fixed unlend on withdraw with borrow

* fix: fixed the logic

* fix: lending on funding
This commit is contained in:
Linkie Link 2024-01-08 17:46:33 +01:00 committed by GitHub
parent 117de1e3e5
commit 14d09409f9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 16 deletions

View File

@ -10,9 +10,8 @@ import Text from 'components/Text'
import WalletBridges from 'components/Wallet/WalletBridges'
import { BN_ZERO } from 'constants/math'
import useBaseAsset from 'hooks/assets/useBasetAsset'
import useEnableAutoLendGlobal from 'hooks/localStorage/useEnableAutoLendGlobal'
import useMarketAssets from 'hooks/markets/useMarketAssets'
import useToggle from 'hooks/useToggle'
import useAutoLend from 'hooks/useAutoLend'
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
import useWalletBalances from 'hooks/useWalletBalances'
import useStore from 'store'
@ -33,11 +32,11 @@ export default function AccountFundContent(props: Props) {
const deposit = useStore((s) => s.deposit)
const walletAssetModal = useStore((s) => s.walletAssetsModal)
const [isConfirming, setIsConfirming] = useState(false)
const [enableAutoLendGlobal] = useEnableAutoLendGlobal()
const { autoLendEnabledAccountIds } = useAutoLend()
const isLending = autoLendEnabledAccountIds.includes(props.accountId)
const [fundingAssets, setFundingAssets] = useState<BNCoin[]>([])
const { data: marketAssets } = useMarketAssets()
const { data: walletBalances } = useWalletBalances(props.address)
const [isLending, toggleIsLending] = useToggle(enableAutoLendGlobal)
const { simulateDeposits } = useUpdatedAccount(props.account)
const baseAsset = useBaseAsset()

View File

@ -48,10 +48,12 @@ export default function WithdrawFromAccount(props: Props) {
const max = withdrawWithBorrowing ? maxWithdrawWithBorrowAmount : maxWithdrawAmount
const accountDeposit = account.deposits.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
const shouldReclaim =
amount.isGreaterThan(accountDeposit) && !withdrawWithBorrowing && currentAsset.isAutoLendEnabled
const reclaimAmount = shouldReclaim ? amount.minus(accountDeposit) : BN_ZERO
const isReclaimingMaxAmount = maxWithdrawAmount.isEqualTo(amount)
const accountLent = account.lends.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
const shouldReclaim = amount.isGreaterThan(accountDeposit) && !accountLent.isZero()
const isReclaimingMaxAmount = accountLent.isLessThanOrEqualTo(amount.minus(accountDeposit))
const reclaimAmount = isReclaimingMaxAmount
? amount
: accountLent.minus(amount).minus(accountDeposit)
function onChangeAmount(val: BigNumber) {
setAmount(val)
@ -67,13 +69,14 @@ export default function WithdrawFromAccount(props: Props) {
const borrow = !debtAmount.isZero()
? [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)]
: []
const reclaims = !reclaimAmount.isZero()
? [
BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount).toActionCoin(
isReclaimingMaxAmount,
),
]
: []
const reclaims =
shouldReclaim && !reclaimAmount.isZero()
? [
BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount).toActionCoin(
isReclaimingMaxAmount,
),
]
: []
withdraw({
accountId: account.id,

View File

@ -14,7 +14,7 @@ export default function SwitchAutoLend(props: Props) {
const { accountId, className } = props
const { autoLendEnabledAccountIds, disableAutoLend, enableAutoLend } = useAutoLend()
const isAutoLendEnabledForAccount = autoLendEnabledAccountIds.includes(accountId)
const [isAutoLendEnabled, setIsAutoLendEnabled] = useEnableAutoLendGlobal()
const [_, setIsAutoLendEnabled] = useEnableAutoLendGlobal()
const handleToggle = useCallback(() => {
if (!isAutoLendEnabledForAccount) {