Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqiancaosissi committed Dec 31, 2023
1 parent 8d2401e commit 08d0ec1
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 38 deletions.
11 changes: 8 additions & 3 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { recomputeHealthFactorSupply } from "../../redux/selectors/recomputeHeal
import { recomputeHealthFactorRepay } from "../../redux/selectors/recomputeHealthFactorRepay";
import { recomputeHealthFactorRepayFromDeposits } from "../../redux/selectors/recomputeHealthFactorRepayFromDeposits";
import { formatWithCommas_number } from "../../utils/uiNumber";
import { DEFAULT_POSITION } from "../../utils/config";
import { DEFAULT_POSITION, lpTokenPrefix } from "../../utils/config";
import { Wrapper } from "./style";
import { getModalData } from "./utils";
import {
Expand Down Expand Up @@ -48,7 +48,6 @@ const Modal = () => {
const dispatch = useAppDispatch();
const { isRepayFromDeposits } = useDegenMode();
const theme = useTheme();
const portfolioAssets = usePortfolioAssets();
const [selectedCollateralType, setSelectedCollateralType] = useState(DEFAULT_POSITION);

const { action = "Deposit", tokenId, position } = asset;
Expand All @@ -72,8 +71,14 @@ const Modal = () => {
const maxBorrowAmountPositions = useAppSelector(getBorrowMaxAmount(tokenId));
const maxWithdrawAmount = useAppSelector(getWithdrawMaxAmount(tokenId));
const repayPositions = useAppSelector(getRepayPositions(tokenId));
const activePosition =
action === "Repay" || action === "Borrow"
? selectedCollateralType
: tokenId.indexOf(lpTokenPrefix) > -1
? tokenId
: DEFAULT_POSITION;
const { maxBorrowAmount = 0, maxBorrowValue = 0 } =
maxBorrowAmountPositions[selectedCollateralType] || {};
maxBorrowAmountPositions[activePosition] || {};
const repayAmount = repayPositions[selectedCollateralType];
const {
symbol,
Expand Down
18 changes: 1 addition & 17 deletions redux/selectors/getBorrowMaxAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const computeBorrowMaxAmount = (tokenId: string, assets: Assets, account,
Math.max(0, maxBorrowAmountTemp.toNumber()),
uiAsset.availableLiquidity || 0,
);
const maxBorrowPriced = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowPriced = adjustedCollateralSum.sub(adjustedBorrowedSum);
return {
[position]: {
maxBorrowAmount: Math.max(maxBorrowAmount.toNumber(), 0),
Expand All @@ -41,22 +41,6 @@ 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
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export const recomputeHealthFactor = (tokenId: string, amount: number, position:
);
const adjustedBorrowedSum = getAdjustedSum("borrowed", portfolio, assets.data, position);

const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactorAdjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const recomputeHealthFactorAdjust = (tokenId: string, amount: number) =>
position,
);

const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactorRepay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const recomputeHealthFactorRepay = (tokenId: string, amount: number, posi
assets.data,
position,
);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactorRepayFromDeposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const recomputeHealthFactorRepayFromDeposits = (
assets.data,
position,
);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactorSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export const recomputeHealthFactorSupply = (tokenId: string, amount: number) =>
position,
);

const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
2 changes: 1 addition & 1 deletion redux/selectors/recomputeHealthFactorWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const recomputeHealthFactorWithdraw = (tokenId: string, amount: number) =
assets.data,
position,
);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum).mul(95).div(100);
const maxBorrowValue = adjustedCollateralSum.sub(adjustedBorrowedSum);
const healthFactorTemp = adjustedCollateralSum.div(adjustedBorrowedSum).mul(100).toNumber();
const healthFactor = healthFactorTemp < MAX_RATIO ? healthFactorTemp : MAX_RATIO;
return { healthFactor, maxBorrowValue };
Expand Down
9 changes: 3 additions & 6 deletions store/actions/shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,17 @@ export async function shadow_action_supply({
}
export async function shadow_action_withdraw({
tokenId,
decimals,
amount,
expandAmount,
isMax,
decreaseCollateralAmount,
}: {
tokenId: string;
decimals: number;
amount: string;
expandAmount: string;
isMax: boolean;
decreaseCollateralAmount: Decimal;
}): Promise<void> {
const transactions: Transaction[] = [];
const { refv1Contract, logicContract, oracleContract } = await getBurrow();
const expandAmount = expandTokenDecimal(amount, decimals).toFixed(0);
const pool_id = +tokenId.split("-")[1];
if (decreaseCollateralAmount.gt(0)) {
transactions.push({
Expand Down Expand Up @@ -105,7 +102,7 @@ export async function shadow_action_withdraw({
args: {
action: "FromBurrowland",
pool_id,
...(isMax ? {} : { amount: expandAmount }),
amount: expandAmount,
msg: "",
},
},
Expand Down
8 changes: 2 additions & 6 deletions store/actions/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,11 @@ export async function withdraw({ tokenId, extraDecimals, amount, isMax }: Props)
: decimalMin(maxAmount, expandTokenDecimal(amount, decimals + extraDecimals));

const transactions: Transaction[] = [];
const decreaseCollateralAmount = decimalMax(expandedAmount.sub(suppliedBalance), 0);
if (isLpToken) {
const decreaseCollateralAmount = decimalMax(expandedAmount.sub(suppliedBalance), 0);
shadow_action_withdraw({
tokenId,
decimals: decimals + extraDecimals,
amount,
expandAmount: expandedAmount.toFixed(0),
isMax,
decreaseCollateralAmount,
});
Expand Down Expand Up @@ -103,9 +102,6 @@ export async function withdraw({ tokenId, extraDecimals, amount, isMax }: Props)
max_amount: !isMax ? expandedAmount.toFixed(0) : undefined,
},
};

const decreaseCollateralAmount = decimalMax(expandedAmount.sub(suppliedBalance), 0);

if (decreaseCollateralAmount.gt(0)) {
transactions.push({
receiverId: oracleContract.contractId,
Expand Down

0 comments on commit 08d0ec1

Please sign in to comment.