repay from wallet hotfix (#691)
* fix: removed overpay from assets that are close to their cap or reached it * fix: calculate the amount of overpay * fix: added repay_from_wallet
This commit is contained in:
parent
59838dcb45
commit
dc6a2a6b16
@ -35,8 +35,8 @@ export default function DepositCapMessage(props: Props) {
|
|||||||
<FormattedNumber
|
<FormattedNumber
|
||||||
amount={coin.amount.toNumber()}
|
amount={coin.amount.toNumber()}
|
||||||
options={{
|
options={{
|
||||||
abbreviated: true,
|
|
||||||
decimals: asset.decimals,
|
decimals: asset.decimals,
|
||||||
|
maxDecimals: asset.decimals,
|
||||||
suffix: ` ${asset.symbol}`,
|
suffix: ` ${asset.symbol}`,
|
||||||
}}
|
}}
|
||||||
className='text-xs text-white/60'
|
className='text-xs text-white/60'
|
||||||
|
@ -18,6 +18,7 @@ import { BN_ZERO } from 'constants/math'
|
|||||||
import useAutoLend from 'hooks/useAutoLend'
|
import useAutoLend from 'hooks/useAutoLend'
|
||||||
import useCurrentAccount from 'hooks/useCurrentAccount'
|
import useCurrentAccount from 'hooks/useCurrentAccount'
|
||||||
import useHealthComputer from 'hooks/useHealthComputer'
|
import useHealthComputer from 'hooks/useHealthComputer'
|
||||||
|
import useMarketAssets from 'hooks/useMarketAssets'
|
||||||
import useToggle from 'hooks/useToggle'
|
import useToggle from 'hooks/useToggle'
|
||||||
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
import { useUpdatedAccount } from 'hooks/useUpdatedAccount'
|
||||||
import { getDepositAndLendCoinsToSpend } from 'hooks/useUpdatedAccount/functions'
|
import { getDepositAndLendCoinsToSpend } from 'hooks/useUpdatedAccount/functions'
|
||||||
@ -90,6 +91,7 @@ function BorrowModal(props: Props) {
|
|||||||
const isAutoLendEnabled = autoLendEnabledAccountIds.includes(account.id)
|
const isAutoLendEnabled = autoLendEnabledAccountIds.includes(account.id)
|
||||||
const { computeMaxBorrowAmount } = useHealthComputer(account)
|
const { computeMaxBorrowAmount } = useHealthComputer(account)
|
||||||
const totalDebt = BN(getDebtAmount(modal))
|
const totalDebt = BN(getDebtAmount(modal))
|
||||||
|
const { data: marketAssets } = useMarketAssets()
|
||||||
|
|
||||||
const [depositBalance, lendBalance] = useMemo(
|
const [depositBalance, lendBalance] = useMemo(
|
||||||
() => [
|
() => [
|
||||||
@ -104,11 +106,22 @@ function BorrowModal(props: Props) {
|
|||||||
[totalDebt, apy],
|
[totalDebt, apy],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const overpayExeedsCap = useMemo(() => {
|
||||||
|
const marketAsset = marketAssets.find(byDenom(asset.denom))
|
||||||
|
if (!marketAsset) return
|
||||||
|
const overpayAmount = totalDebtRepayAmount.minus(totalDebt)
|
||||||
|
const marketCapAfterOverpay = marketAsset.cap.used.plus(overpayAmount)
|
||||||
|
|
||||||
|
return marketAsset.cap.max.isLessThanOrEqualTo(marketCapAfterOverpay)
|
||||||
|
}, [marketAssets, asset.denom])
|
||||||
|
|
||||||
const maxRepayAmount = useMemo(() => {
|
const maxRepayAmount = useMemo(() => {
|
||||||
const maxBalance = repayFromWallet
|
const maxBalance = repayFromWallet
|
||||||
? BN(walletBalances.find(byDenom(asset.denom))?.amount ?? 0)
|
? BN(walletBalances.find(byDenom(asset.denom))?.amount ?? 0)
|
||||||
: depositBalance.plus(lendBalance)
|
: depositBalance.plus(lendBalance)
|
||||||
return isRepay ? BigNumber.min(maxBalance, totalDebtRepayAmount) : BN_ZERO
|
return isRepay
|
||||||
|
? BigNumber.min(maxBalance, overpayExeedsCap ? totalDebt : totalDebtRepayAmount)
|
||||||
|
: BN_ZERO
|
||||||
}, [
|
}, [
|
||||||
depositBalance,
|
depositBalance,
|
||||||
lendBalance,
|
lendBalance,
|
||||||
|
@ -586,7 +586,6 @@ export default function createBroadcastSlice(
|
|||||||
fromWallet?: boolean
|
fromWallet?: boolean
|
||||||
}) => {
|
}) => {
|
||||||
const actions: Action[] = [
|
const actions: Action[] = [
|
||||||
...(options.fromWallet ? [{ deposit: options.coin.toCoin() }] : []),
|
|
||||||
{
|
{
|
||||||
repay: {
|
repay: {
|
||||||
coin: options.coin.toActionCoin(options.accountBalance),
|
coin: options.coin.toActionCoin(options.accountBalance),
|
||||||
@ -597,12 +596,18 @@ export default function createBroadcastSlice(
|
|||||||
if (options.lend && options.lend.amount.isGreaterThan(0))
|
if (options.lend && options.lend.amount.isGreaterThan(0))
|
||||||
actions.unshift({ reclaim: options.lend.toActionCoin() })
|
actions.unshift({ reclaim: options.lend.toActionCoin() })
|
||||||
|
|
||||||
const msg: CreditManagerExecuteMsg = {
|
const msg: CreditManagerExecuteMsg = options.fromWallet
|
||||||
update_credit_account: {
|
? {
|
||||||
account_id: options.accountId,
|
repay_from_wallet: {
|
||||||
actions,
|
account_id: options.accountId,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
: {
|
||||||
|
update_credit_account: {
|
||||||
|
account_id: options.accountId,
|
||||||
|
actions,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
const response = get().executeMsg({
|
const response = get().executeMsg({
|
||||||
messages: [
|
messages: [
|
||||||
|
Loading…
Reference in New Issue
Block a user