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:
parent
117de1e3e5
commit
14d09409f9
@ -10,9 +10,8 @@ import Text from 'components/Text'
|
|||||||
import WalletBridges from 'components/Wallet/WalletBridges'
|
import WalletBridges from 'components/Wallet/WalletBridges'
|
||||||
import { BN_ZERO } from 'constants/math'
|
import { BN_ZERO } from 'constants/math'
|
||||||
import useBaseAsset from 'hooks/assets/useBasetAsset'
|
import useBaseAsset from 'hooks/assets/useBasetAsset'
|
||||||
import useEnableAutoLendGlobal from 'hooks/localStorage/useEnableAutoLendGlobal'
|
|
||||||
import useMarketAssets from 'hooks/markets/useMarketAssets'
|
import useMarketAssets from 'hooks/markets/useMarketAssets'
|
||||||
import useToggle from 'hooks/useToggle'
|
import useAutoLend from 'hooks/useAutoLend'
|
||||||
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
||||||
import useWalletBalances from 'hooks/useWalletBalances'
|
import useWalletBalances from 'hooks/useWalletBalances'
|
||||||
import useStore from 'store'
|
import useStore from 'store'
|
||||||
@ -33,11 +32,11 @@ export default function AccountFundContent(props: Props) {
|
|||||||
const deposit = useStore((s) => s.deposit)
|
const deposit = useStore((s) => s.deposit)
|
||||||
const walletAssetModal = useStore((s) => s.walletAssetsModal)
|
const walletAssetModal = useStore((s) => s.walletAssetsModal)
|
||||||
const [isConfirming, setIsConfirming] = useState(false)
|
const [isConfirming, setIsConfirming] = useState(false)
|
||||||
const [enableAutoLendGlobal] = useEnableAutoLendGlobal()
|
const { autoLendEnabledAccountIds } = useAutoLend()
|
||||||
|
const isLending = autoLendEnabledAccountIds.includes(props.accountId)
|
||||||
const [fundingAssets, setFundingAssets] = useState<BNCoin[]>([])
|
const [fundingAssets, setFundingAssets] = useState<BNCoin[]>([])
|
||||||
const { data: marketAssets } = useMarketAssets()
|
const { data: marketAssets } = useMarketAssets()
|
||||||
const { data: walletBalances } = useWalletBalances(props.address)
|
const { data: walletBalances } = useWalletBalances(props.address)
|
||||||
const [isLending, toggleIsLending] = useToggle(enableAutoLendGlobal)
|
|
||||||
const { simulateDeposits } = useUpdatedAccount(props.account)
|
const { simulateDeposits } = useUpdatedAccount(props.account)
|
||||||
const baseAsset = useBaseAsset()
|
const baseAsset = useBaseAsset()
|
||||||
|
|
||||||
|
@ -48,10 +48,12 @@ export default function WithdrawFromAccount(props: Props) {
|
|||||||
const max = withdrawWithBorrowing ? maxWithdrawWithBorrowAmount : maxWithdrawAmount
|
const max = withdrawWithBorrowing ? maxWithdrawWithBorrowAmount : maxWithdrawAmount
|
||||||
|
|
||||||
const accountDeposit = account.deposits.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
|
const accountDeposit = account.deposits.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
|
||||||
const shouldReclaim =
|
const accountLent = account.lends.find(byDenom(currentAsset.denom))?.amount ?? BN_ZERO
|
||||||
amount.isGreaterThan(accountDeposit) && !withdrawWithBorrowing && currentAsset.isAutoLendEnabled
|
const shouldReclaim = amount.isGreaterThan(accountDeposit) && !accountLent.isZero()
|
||||||
const reclaimAmount = shouldReclaim ? amount.minus(accountDeposit) : BN_ZERO
|
const isReclaimingMaxAmount = accountLent.isLessThanOrEqualTo(amount.minus(accountDeposit))
|
||||||
const isReclaimingMaxAmount = maxWithdrawAmount.isEqualTo(amount)
|
const reclaimAmount = isReclaimingMaxAmount
|
||||||
|
? amount
|
||||||
|
: accountLent.minus(amount).minus(accountDeposit)
|
||||||
|
|
||||||
function onChangeAmount(val: BigNumber) {
|
function onChangeAmount(val: BigNumber) {
|
||||||
setAmount(val)
|
setAmount(val)
|
||||||
@ -67,13 +69,14 @@ export default function WithdrawFromAccount(props: Props) {
|
|||||||
const borrow = !debtAmount.isZero()
|
const borrow = !debtAmount.isZero()
|
||||||
? [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)]
|
? [BNCoin.fromDenomAndBigNumber(currentAsset.denom, debtAmount)]
|
||||||
: []
|
: []
|
||||||
const reclaims = !reclaimAmount.isZero()
|
const reclaims =
|
||||||
? [
|
shouldReclaim && !reclaimAmount.isZero()
|
||||||
BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount).toActionCoin(
|
? [
|
||||||
isReclaimingMaxAmount,
|
BNCoin.fromDenomAndBigNumber(currentAsset.denom, reclaimAmount).toActionCoin(
|
||||||
),
|
isReclaimingMaxAmount,
|
||||||
]
|
),
|
||||||
: []
|
]
|
||||||
|
: []
|
||||||
|
|
||||||
withdraw({
|
withdraw({
|
||||||
accountId: account.id,
|
accountId: account.id,
|
||||||
|
@ -14,7 +14,7 @@ export default function SwitchAutoLend(props: Props) {
|
|||||||
const { accountId, className } = props
|
const { accountId, className } = props
|
||||||
const { autoLendEnabledAccountIds, disableAutoLend, enableAutoLend } = useAutoLend()
|
const { autoLendEnabledAccountIds, disableAutoLend, enableAutoLend } = useAutoLend()
|
||||||
const isAutoLendEnabledForAccount = autoLendEnabledAccountIds.includes(accountId)
|
const isAutoLendEnabledForAccount = autoLendEnabledAccountIds.includes(accountId)
|
||||||
const [isAutoLendEnabled, setIsAutoLendEnabled] = useEnableAutoLendGlobal()
|
const [_, setIsAutoLendEnabled] = useEnableAutoLendGlobal()
|
||||||
|
|
||||||
const handleToggle = useCallback(() => {
|
const handleToggle = useCallback(() => {
|
||||||
if (!isAutoLendEnabledForAccount) {
|
if (!isAutoLendEnabledForAccount) {
|
||||||
|
Loading…
Reference in New Issue
Block a user