# Conflicts:
#	package.json
#	src/components/Modals/BorrowModal.tsx
#	src/components/Trade/TradeChart/TVChartContainer.tsx
#	src/components/Trade/TradeChart/index.tsx
#	src/components/Trade/TradeModule/SwapForm/TradeSummary.tsx
#	src/components/Trade/TradeModule/SwapForm/index.tsx
#	src/components/Trade/TradeModule/index.tsx
#	src/components/Wallet/WalletFetchBalancesAndAccounts.tsx
#	src/pages/TradePage.tsx
This commit is contained in:
Linkie Link 2023-12-12 12:41:56 +01:00
commit 701071d981
No known key found for this signature in database
GPG Key ID: 5318B0F2564D38EA
3 changed files with 27 additions and 9 deletions

View File

@ -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'

View File

@ -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,

View File

@ -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: [