Skip to content

Commit

Permalink
fix health issue
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqiancaosissi committed Dec 31, 2023
1 parent 52fd165 commit 8d2401e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 128 deletions.
5 changes: 1 addition & 4 deletions api/get-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ const getBalance = async (
pool_id: +pool_id,
},
)) as string;
const shadow_used = Decimal.max(
shadowRecords?.[pool_id]?.["shadow_in_farm"] || "0",
shadowRecords?.[pool_id]?.["shadow_in_burrow"] || "0",
);
const shadow_used = shadowRecords?.[pool_id]?.["shadow_in_burrow"] || "0";
balanceInYocto = new Decimal(poolShares).minus(shadow_used).toFixed();
} else {
balanceInYocto = (await view(
Expand Down
34 changes: 9 additions & 25 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { recomputeHealthFactorWithdraw } from "../../redux/selectors/recomputeHe
import { recomputeHealthFactorSupply } from "../../redux/selectors/recomputeHealthFactorSupply";
import { recomputeHealthFactorRepay } from "../../redux/selectors/recomputeHealthFactorRepay";
import { recomputeHealthFactorRepayFromDeposits } from "../../redux/selectors/recomputeHealthFactorRepayFromDeposits";
import { recomputeHealthFactorRepayFromDepositsLp } from "../../redux/selectors/recomputeHealthFactorRepayFromDepositsLp";
import { formatWithCommas_number } from "../../utils/uiNumber";
import { DEFAULT_POSITION } from "../../utils/config";
import { Wrapper } from "./style";
Expand Down Expand Up @@ -62,19 +61,14 @@ const Modal = () => {
: action === "Supply"
? recomputeHealthFactorSupply(tokenId, +amount)
: action === "Repay" && isRepayFromDeposits
? recomputeHealthFactorRepayFromDeposits(tokenId, +amount)
? recomputeHealthFactorRepayFromDeposits(tokenId, +amount, selectedCollateralType)
: action === "Repay" && !isRepayFromDeposits
? recomputeHealthFactorRepay(tokenId, +amount, selectedCollateralType)
: recomputeHealthFactor(tokenId, +amount, selectedCollateralType),
);
const { healthFactor: healthFactor_repay_lp, maxBorrowValue: adjustedMaxBorrowValue_repay_lp } =
useAppSelector(
action === "Repay" && isRepayFromDeposits && selectedCollateralType !== DEFAULT_POSITION
? recomputeHealthFactorRepayFromDepositsLp(tokenId, +amount, selectedCollateralType)
: () => {
return { healthFactor: 0, maxBorrowValue: 0 };
},
);
const { healthFactor: single_healthFactor } = useAppSelector(
recomputeHealthFactorWithdraw(tokenId, +amount),
);
const maxBorrowAmountPositions = useAppSelector(getBorrowMaxAmount(tokenId));
const maxWithdrawAmount = useAppSelector(getWithdrawMaxAmount(tokenId));
const repayPositions = useAppSelector(getRepayPositions(tokenId));
Expand Down Expand Up @@ -124,10 +118,6 @@ const Modal = () => {
}
const repay_to_lp =
action === "Repay" && isRepayFromDeposits && selectedCollateralType !== DEFAULT_POSITION;
const not_borrow_from_regular =
action === "Repay" &&
isRepayFromDeposits &&
!((portfolioAssets[1] || []) as any[]).find((a) => a.tokenId === tokenId);
return (
<MUIModal open={isOpen} onClose={handleClose}>
<Wrapper
Expand Down Expand Up @@ -170,13 +160,10 @@ const Modal = () => {
available$={available$}
/>
<div className="flex flex-col gap-4 mt-6">
{repay_to_lp ? <HealthFactor value={healthFactor_repay_lp} /> : null}
{not_borrow_from_regular ? null : (
<HealthFactor
value={healthFactor}
title={`${repay_to_lp ? "Health Factor(Single)" : ""}`}
/>
)}
<HealthFactor value={healthFactor} />
{repay_to_lp ? (
<HealthFactor value={single_healthFactor} title="Health Factor(Single)" />
) : null}
<Rates rates={rates} />
{!canUseAsCollateral ? (
<CollateralTip />
Expand All @@ -187,10 +174,7 @@ const Modal = () => {
tokenId={asset.tokenId}
/>
)}
<BorrowLimit
from={maxBorrowValue}
to={repay_to_lp ? adjustedMaxBorrowValue_repay_lp : adjustedMaxBorrowValue}
/>
<BorrowLimit from={maxBorrowValue} to={adjustedMaxBorrowValue} />
</div>
<Alerts data={alerts} />
<Action
Expand Down
27 changes: 22 additions & 5 deletions redux/selectors/getBorrowMaxAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,20 @@ export const computeBorrowMaxAmount = (tokenId: string, assets: Assets, account,
.map((position: string) => {
const adjustedCollateralSum = getAdjustedSum("collateral", portfolio, assets, position);
const adjustedBorrowedSum = getAdjustedSum("borrowed", portfolio, assets, position);
// const volatiliyRatio = asset.config.volatility_ratio || 0;
const volatiliyRatio = asset.config.volatility_ratio || 0;
const price = asset.price?.usd || Infinity;
const maxBorrowPriced = adjustedCollateralSum
const maxBorrowPricedForToken = adjustedCollateralSum
.sub(adjustedBorrowedSum)
// .mul(volatiliyRatio)
// .div(MAX_RATIO)
.mul(volatiliyRatio)
.div(MAX_RATIO)
.mul(95)
.div(100);
const maxBorrowAmountTemp = maxBorrowPriced.div(price);
const maxBorrowAmountTemp = maxBorrowPricedForToken.div(price);
const maxBorrowAmount = Decimal.min(
Math.max(0, maxBorrowAmountTemp.toNumber()),
uiAsset.availableLiquidity || 0,
);
const maxBorrowPriced = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
return {
[position]: {
maxBorrowAmount: Math.max(maxBorrowAmount.toNumber(), 0),
Expand All @@ -40,6 +41,22 @@ export const computeBorrowMaxAmount = (tokenId: string, assets: Assets, account,
})
.reduce((acc, cur) => ({ ...acc, ...cur }), {});
};
export const computeBorrowMaxAmount2 = (tokenId: string, assets: Assets, portfolio: Portfolio) => {
const asset = assets[tokenId];
const adjustedCollateralSum = getAdjustedSum("collateral", portfolio, assets);
const adjustedBorrowedSum = getAdjustedSum("borrowed", portfolio, assets);
const volatiliyRatio = asset.config.volatility_ratio || 0;
const price = asset.price?.usd || Infinity;

const maxBorrowAmount = adjustedCollateralSum
.sub(adjustedBorrowedSum)
.mul(volatiliyRatio)
.div(MAX_RATIO)
.div(price)
.mul(95)
.div(100);
return maxBorrowAmount;
};

export const getBorrowMaxAmount = (tokenId: string) =>
createSelector(
Expand Down
48 changes: 12 additions & 36 deletions redux/selectors/recomputeHealthFactorRepayFromDeposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import { expandTokenDecimal, MAX_RATIO } from "../../store";
import { RootState } from "../store";
import { hasAssets } from "../utils";
import { getAdjustedSum } from "./getWithdrawMaxAmount";
import { decimalMax, decimalMin } from "../../utils";
import { DEFAULT_POSITION } from "../../utils/config";
import { decimalMax } from "../../utils";

export const recomputeHealthFactorRepayFromDeposits = (tokenId: string, amount: number) =>
export const recomputeHealthFactorRepayFromDeposits = (
tokenId: string,
amount: number,
position: string,
) =>
createSelector(
(state: RootState) => state.assets,
(state: RootState) => state.account,
Expand All @@ -18,61 +21,34 @@ export const recomputeHealthFactorRepayFromDeposits = (tokenId: string, amount:
if (
!account.portfolio ||
!tokenId ||
!account.portfolio.positions[DEFAULT_POSITION]?.borrowed?.[tokenId]
!account.portfolio.positions[position]?.borrowed?.[tokenId]
)
return { healthFactor: 0, maxBorrowValue: 0 };
const asset = assets.data[tokenId];
const { metadata, config } = asset;
const decimals = metadata.decimals + config.extra_decimals;
const amountDecimal = expandTokenDecimal(amount, decimals);
// new collateral balance on REGULAR
const collateralBalance = new Decimal(
account.portfolio.positions[DEFAULT_POSITION]?.collateral?.[tokenId]?.balance || "0",
);
const suppliedBalance = new Decimal(account.portfolio.supplied[tokenId]?.balance || "0");

const newCollateralBalance = decimalMax(
0,
decimalMin(collateralBalance, collateralBalance.plus(suppliedBalance).minus(amountDecimal)),
);
// new borrowed balance on REGULAR;
// new borrowed balance
const borrowedBalance = new Decimal(
account.portfolio.positions[DEFAULT_POSITION].borrowed[tokenId].balance,
account.portfolio.positions[position].borrowed[tokenId].balance,
);
const newBorrowedBalance = decimalMax(0, borrowedBalance.minus(amountDecimal));

const clonedAccount = clone(account);
// update borrowed balance in position
clonedAccount.portfolio.positions[DEFAULT_POSITION].borrowed[tokenId].balance =
clonedAccount.portfolio.positions[position].borrowed[tokenId].balance =
newBorrowedBalance.toFixed();
// update collateral balance in REGULAR
if (config.can_use_as_collateral) {
if (clonedAccount.portfolio.positions[DEFAULT_POSITION]?.collateral?.[tokenId]) {
clonedAccount.portfolio.positions[DEFAULT_POSITION].collateral[tokenId].balance =
newCollateralBalance.toFixed();
}
// if (clonedAccount.portfolio.collateral[tokenId]) {
// clonedAccount.portfolio.collateral[tokenId].balance = newCollateralBalance.toFixed();
// } else {
// const updatedToken = {
// balance: newCollateralBalance.toFixed(),
// shares: newCollateralBalance.toFixed(),
// apr: "0",
// };
// clonedAccount.portfolio.collateral[tokenId] = updatedToken;
// }
}
const adjustedCollateralSum = getAdjustedSum(
"collateral",
account.portfolio,
assets.data,
DEFAULT_POSITION,
position,
);
const adjustedBorrowedSum = getAdjustedSum(
"borrowed",
clonedAccount.portfolio,
assets.data,
DEFAULT_POSITION,
position,
);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
Expand Down
58 changes: 0 additions & 58 deletions redux/selectors/recomputeHealthFactorRepayFromDepositsLp.ts

This file was deleted.

0 comments on commit 8d2401e

Please sign in to comment.