diff --git a/src/components/Account/AccountFund/AccountFundContent.tsx b/src/components/Account/AccountFund/AccountFundContent.tsx index df8ce9f1..2e67df83 100644 --- a/src/components/Account/AccountFund/AccountFundContent.tsx +++ b/src/components/Account/AccountFund/AccountFundContent.tsx @@ -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([]) const { data: marketAssets } = useMarketAssets() const { data: walletBalances } = useWalletBalances(props.address) - const [isLending, toggleIsLending] = useToggle(enableAutoLendGlobal) const { simulateDeposits } = useUpdatedAccount(props.account) const baseAsset = useBaseAsset() diff --git a/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx b/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx index 9e0b5a62..061cfb0d 100644 --- a/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx +++ b/src/components/Modals/FundWithdraw/WithdrawFromAccount.tsx @@ -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, diff --git a/src/components/Switch/SwitchAutoLend.tsx b/src/components/Switch/SwitchAutoLend.tsx index 27f36f4e..6bfc28c0 100644 --- a/src/components/Switch/SwitchAutoLend.tsx +++ b/src/components/Switch/SwitchAutoLend.tsx @@ -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) {