diff --git a/src/components/Modals/AssetAmountSelectActionModal.tsx b/src/components/Modals/AssetAmountSelectActionModal.tsx index dfa41d9d..e0b2f05a 100644 --- a/src/components/Modals/AssetAmountSelectActionModal.tsx +++ b/src/components/Modals/AssetAmountSelectActionModal.tsx @@ -24,6 +24,7 @@ interface Props { onClose: () => void onChange: (value: BigNumber) => void onAction: (value: BigNumber, isMax: boolean) => void + onDebounce: () => void } export default function AssetAmountSelectActionModal(props: Props) { @@ -36,6 +37,7 @@ export default function AssetAmountSelectActionModal(props: Props) { onClose, onChange, onAction, + onDebounce, } = props const [amount, setAmount] = useState(BN_ZERO) const maxAmount = BN(coinBalances.find(byDenom(asset.denom))?.amount ?? 0) @@ -74,6 +76,7 @@ export default function AssetAmountSelectActionModal(props: Props) { getDebtAmountWithInterest(totalDebt, apy), - [totalDebt, apy], + const accountDebtWithInterest = useMemo( + () => getDebtAmountWithInterest(accountDebt, apy), + [accountDebt, apy], ) const overpayExeedsCap = useMemo(() => { const marketAsset = markets.find((market) => market.asset.denom === asset.denom) if (!marketAsset) return - const overpayAmount = totalDebtRepayAmount.minus(totalDebt) + const overpayAmount = accountDebtWithInterest.minus(accountDebt) const marketCapAfterOverpay = marketAsset.cap.used.plus(overpayAmount) return marketAsset.cap.max.isLessThanOrEqualTo(marketCapAfterOverpay) - }, [markets, asset.denom, totalDebt, totalDebtRepayAmount]) + }, [markets, asset.denom, accountDebt, accountDebtWithInterest]) const maxRepayAmount = useMemo(() => { const maxBalance = repayFromWallet ? BN(walletBalances.find(byDenom(asset.denom))?.amount ?? 0) : depositBalance.plus(lendBalance) return isRepay - ? BigNumber.min(maxBalance, overpayExeedsCap ? totalDebt : totalDebtRepayAmount) + ? BigNumber.min(maxBalance, overpayExeedsCap ? accountDebt : accountDebtWithInterest) : BN_ZERO }, [ depositBalance, lendBalance, isRepay, - totalDebtRepayAmount, + accountDebtWithInterest, overpayExeedsCap, - totalDebt, + accountDebt, walletBalances, asset.denom, repayFromWallet, @@ -149,7 +149,7 @@ function BorrowModal(props: Props) { repay({ accountId: account.id, coin: BNCoin.fromDenomAndBigNumber(asset.denom, amount), - accountBalance: amount.isEqualTo(totalDebtRepayAmount), + accountBalance: amount.isEqualTo(accountDebtWithInterest), lend: repayFromWallet ? BNCoin.fromDenomAndBigNumber(asset.denom, BN_ZERO) : lend, fromWallet: repayFromWallet, }) @@ -172,17 +172,39 @@ function BorrowModal(props: Props) { const handleChange = useCallback( (newAmount: BigNumber) => { - const coin = BNCoin.fromDenomAndBigNumber(asset.denom, newAmount) if (!amount.isEqualTo(newAmount)) setAmount(newAmount) - if (!isRepay) return - const repayCoin = coin.amount.isGreaterThan(totalDebt) - ? BNCoin.fromDenomAndBigNumber(asset.denom, totalDebt) - : coin - simulateRepay(repayCoin, repayFromWallet) }, - [amount, asset.denom, isRepay, simulateRepay, totalDebt, repayFromWallet], + [amount, setAmount], ) + const onDebounce = useCallback(() => { + if (isRepay) { + const repayCoin = BNCoin.fromDenomAndBigNumber( + asset.denom, + amount.isGreaterThan(accountDebt) ? accountDebt : amount, + ) + simulateRepay(repayCoin, repayFromWallet) + } else { + const borrowCoin = BNCoin.fromDenomAndBigNumber( + asset.denom, + amount.isGreaterThan(max) ? max : amount, + ) + const target = borrowToWallet ? 'wallet' : isAutoLendEnabled ? 'lend' : 'deposit' + simulateBorrow(target, borrowCoin) + } + }, [ + amount, + isRepay, + repayFromWallet, + maxRepayAmount, + max, + asset, + borrowToWallet, + isAutoLendEnabled, + simulateBorrow, + simulateRepay, + ]) + const maxBorrow = useMemo(() => { const maxBorrowAmount = isRepay ? BN_ZERO @@ -209,13 +231,6 @@ function BorrowModal(props: Props) { setAmount(max) }, [amount, max, handleChange]) - useEffect(() => { - if (isRepay) return - const coin = BNCoin.fromDenomAndBigNumber(asset.denom, amount.isGreaterThan(max) ? max : amount) - const target = borrowToWallet ? 'wallet' : isAutoLendEnabled ? 'lend' : 'deposit' - simulateBorrow(target, coin) - }, [isRepay, borrowToWallet, isAutoLendEnabled, simulateBorrow, asset, amount, max]) - if (!modal || !asset) return null return ( - Borrowed + Total Borrowed @@ -294,6 +309,7 @@ function BorrowModal(props: Props) { { + const onDebounce = useCallback(() => { const coin = BNCoin.fromDenomAndBigNumber(currentAsset.denom, withdrawAmount.plus(debtAmount)) simulateWithdraw(withdrawWithBorrowing, coin) - }, [ - amount, - withdrawWithBorrowing, - currentAsset.denom, - debtAmount, - simulateWithdraw, - withdrawAmount, - ]) + }, [withdrawWithBorrowing, currentAsset.denom, debtAmount, simulateWithdraw, withdrawAmount]) return ( <> @@ -104,6 +97,7 @@ export default function WithdrawFromAccount(props: Props) { { setAmount(BN_ZERO) setWithdrawWithBorrowing(false) diff --git a/src/components/Modals/LendAndReclaim/index.tsx b/src/components/Modals/LendAndReclaim/index.tsx index 28e9c00c..a1d269ed 100644 --- a/src/components/Modals/LendAndReclaim/index.tsx +++ b/src/components/Modals/LendAndReclaim/index.tsx @@ -1,4 +1,4 @@ -import { useCallback } from 'react' +import { useCallback, useState } from 'react' import AssetAmountSelectActionModal from 'components/Modals/AssetAmountSelectActionModal' import DetailsHeader from 'components/Modals/LendAndReclaim/DetailsHeader' @@ -27,6 +27,7 @@ function LendAndReclaimModal({ currentAccount, config }: Props) { const reclaim = useStore((s) => s.reclaim) const { close } = useLendAndReclaimModal() const { simulateLending } = useUpdatedAccount(currentAccount) + const [coin, setCoin] = useState() const { data, action } = config const { asset } = data @@ -37,12 +38,16 @@ function LendAndReclaimModal({ currentAccount, config }: Props) { const handleAmountChange = useCallback( (value: BigNumber) => { - const coin = BNCoin.fromDenomAndBigNumber(asset.denom, value) - simulateLending(isLendAction, coin) + setCoin(BNCoin.fromDenomAndBigNumber(asset.denom, value)) }, - [asset.denom, isLendAction, simulateLending], + [asset.denom], ) + const onDebounce = useCallback(() => { + if (!coin) return + simulateLending(isLendAction, coin) + }, [coin, isLendAction, simulateLending]) + const handleAction = useCallback( (value: BigNumber, isMax: boolean) => { const coin = BNCoin.fromDenomAndBigNumber(asset.denom, value) @@ -70,6 +75,7 @@ function LendAndReclaimModal({ currentAccount, config }: Props) { onClose={close} onAction={handleAction} onChange={handleAmountChange} + onDebounce={onDebounce} /> ) } diff --git a/src/components/account/AccountFund/AccountFundContent.tsx b/src/components/account/AccountFund/AccountFundContent.tsx index 4a5294d6..ac824c45 100644 --- a/src/components/account/AccountFund/AccountFundContent.tsx +++ b/src/components/account/AccountFund/AccountFundContent.tsx @@ -95,10 +95,6 @@ export default function AccountFundContent(props: Props) { } }, [baseBalance]) - useEffect(() => { - simulateDeposits(isLending ? 'lend' : 'deposit', fundingAssets) - }, [isLending, fundingAssets, simulateDeposits]) - useEffect(() => { const currentSelectedDenom = fundingAssets.map((asset) => asset.denom) @@ -125,6 +121,10 @@ export default function AccountFundContent(props: Props) { }) }, []) + const onDebounce = useCallback(() => { + simulateDeposits(isLending ? 'lend' : 'deposit', fundingAssets) + }, [isLending, fundingAssets, simulateDeposits]) + const depositCapReachedCoins = useMemo(() => { const depositCapReachedCoins: BNCoin[] = [] fundingAssets.forEach((asset) => { @@ -159,6 +159,7 @@ export default function AccountFundContent(props: Props) { amount={coin.amount ?? BN_ZERO} isConfirming={isConfirming} updateFundingAssets={updateFundingAssets} + onDebounce={onDebounce} /> ) diff --git a/src/components/account/AccountFund/AccountFundRow.tsx b/src/components/account/AccountFund/AccountFundRow.tsx index a5a37a26..d3675388 100644 --- a/src/components/account/AccountFund/AccountFundRow.tsx +++ b/src/components/account/AccountFund/AccountFundRow.tsx @@ -10,6 +10,7 @@ interface Props { denom: string isConfirming: boolean updateFundingAssets: (amount: BigNumber, denom: string) => void + onDebounce: () => void } export default function AccountFundRow(props: Props) { @@ -23,6 +24,7 @@ export default function AccountFundRow(props: Props) { props.updateFundingAssets(amount, asset.denom)} + onDebounce={props.onDebounce} amount={props.amount} max={balance} balances={props.balances} diff --git a/src/components/borrow/BorrowActionButtons.tsx b/src/components/borrow/BorrowActionButtons.tsx index 51ec2a5e..5d766d0c 100644 --- a/src/components/borrow/BorrowActionButtons.tsx +++ b/src/components/borrow/BorrowActionButtons.tsx @@ -11,7 +11,7 @@ interface Props { } export default function BorrowActionButtons(props: Props) { - const { asset, debt } = props.data + const { asset, accountDebt } = props.data const marketAssets = useMarketEnabledAssets() const currentAsset = marketAssets.find((a) => a.denom === asset.denom) @@ -33,10 +33,10 @@ export default function BorrowActionButtons(props: Props) { leftIcon={} onClick={borrowHandler} color='secondary' - text={debt ? 'Borrow more' : 'Borrow'} - className='min-w-40 text-center' + text={accountDebt ? 'Borrow more' : 'Borrow'} + className='text-center min-w-40' /> - {debt && ( + {accountDebt && (