Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqiancaosissi committed Dec 26, 2023
1 parent 76b3811 commit 43b1a02
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 19 deletions.
7 changes: 5 additions & 2 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Modal as MUIModal, Typography, Box, Stack, useTheme } from "@mui/materi

import Decimal from "decimal.js";
import { useAppSelector, useAppDispatch } from "../../redux/hooks";
import { hideModal, updateAmount } from "../../redux/appSlice";
import { hideModal, updateAmount, updatePosition } from "../../redux/appSlice";
import { getModalStatus, getAssetData, getSelectedValues } from "../../redux/appSelectors";
import { getWithdrawMaxAmount } from "../../redux/selectors/getWithdrawMaxAmount";
import { getRepayPositions } from "../../redux/selectors/getRepayPositions";
Expand Down Expand Up @@ -106,10 +106,13 @@ const Modal = () => {
}
}, [isOpen]);
useEffect(() => {
setSelectedCollateralType(position || DEFAULT_POSITION);
if (position) {
setSelectedCollateralType(position);
}
}, [position]);
useEffect(() => {
dispatch(updateAmount({ isMax: false, amount: "0" }));
dispatch(updatePosition({ position: selectedCollateralType }));
}, [selectedCollateralType]);
if (action === "Adjust") {
rates.push({
Expand Down
6 changes: 5 additions & 1 deletion redux/appSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export const initialState: AppState = {
useAsCollateral: false,
amount: "0",
isMax: false,
position: undefined,
},
staking: {
amount: 0,
Expand Down Expand Up @@ -148,7 +149,6 @@ export const appSlice = createSlice({
action: TokenAction;
amount: string;
tokenId: string;
position?: string;
}>,
) {
state.selected = { ...state.selected, isMax: false, ...action.payload };
Expand All @@ -158,6 +158,9 @@ export const appSlice = createSlice({
state.selected.amount = action.payload.amount;
state.selected.isMax = action.payload.isMax;
},
updatePosition(state, action: PayloadAction<{ position: string }>) {
state.selected.position = action.payload.position;
},
toggleUseAsCollateral(state, action: PayloadAction<{ useAsCollateral: boolean }>) {
state.selected.useAsCollateral = action.payload.useAsCollateral;
},
Expand Down Expand Up @@ -251,6 +254,7 @@ export const {
setTheme,
setUnreadLiquidation,
setToastMessage,
updatePosition,
} = appSlice.actions;

export default appSlice.reducer;
6 changes: 2 additions & 4 deletions redux/selectors/getCollateralAmount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ export const getCollateralAmount = (tokenId: string) =>
(state: RootState) => state.account,
(assets, account) => {
if (!hasAssets(assets)) return "0";
const { metadata, config, isLpToken } = assets.data[tokenId];
const collateral = isLpToken
? account.portfolio.positions[tokenId].collateral[tokenId]
: account.portfolio.collateral[tokenId];
const { metadata, config } = assets.data[tokenId];
const collateral = account.portfolio.positions[tokenId]?.collateral?.[tokenId];
if (!collateral) return "0";
return toDecimal(
shrinkToken(
Expand Down
10 changes: 5 additions & 5 deletions redux/selectors/recomputeHealthFactorAdjust.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export const recomputeHealthFactorAdjust = (tokenId: string, amount: number) =>
// apr: "0",
// };
// }

// clonedAccount.portfolio.collateral[tokenId] = {
// ...clonedAccount.portfolio.collateral[tokenId],
// balance: newBalance,
// };
clonedAccount.portfolio.positions[position].collateral[tokenId] = {
...clonedAccount.portfolio.positions[position].collateral[tokenId],
balance: newBalance,
shares: newBalance,
};

const adjustedCollateralSum = getAdjustedSum(
"collateral",
Expand Down
9 changes: 7 additions & 2 deletions redux/selectors/recomputeHealthFactorSupply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ export const recomputeHealthFactorSupply = (tokenId: string, amount: number) =>
clonedAccount.portfolio.positions[position].collateral[tokenId].balance,
);
const newBalance = collateralBalance.plus(app.selected.useAsCollateral ? amountDecimal : 0);
clonedAccount.portfolio.positions[position].collateral[tokenId].balance =
newBalance.toFixed();
clonedAccount.portfolio.positions[position].collateral[tokenId] = {
...clonedAccount.portfolio.positions[position].collateral[tokenId],
shares: newBalance.toFixed(),
balance: newBalance.toFixed(),
};
// clonedAccount.portfolio.positions[position].collateral[tokenId].balance =
// newBalance.toFixed();
// if (!clonedAccount.portfolio.collateral[tokenId]) {
// clonedAccount.portfolio.collateral[tokenId] = {
// balance: "0",
Expand Down
9 changes: 7 additions & 2 deletions redux/selectors/recomputeHealthFactorWithdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ export const recomputeHealthFactorWithdraw = (tokenId: string, amount: number) =
decimalMin(collateralBalance, collateralBalance.plus(suppliedBalance).minus(amountDecimal)),
);

clonedAccount.portfolio.positions[position].collateral[tokenId].balance =
newCollateralBalance.toFixed();
clonedAccount.portfolio.positions[position].collateral[tokenId] = {
...clonedAccount.portfolio.positions[position].collateral[tokenId],
shares: newCollateralBalance.toFixed(),
balance: newCollateralBalance.toFixed(),
};
// clonedAccount.portfolio.positions[position].collateral[tokenId].balance =
// newCollateralBalance.toFixed();

const adjustedCollateralSum = getAdjustedSum(
"collateral",
Expand Down
10 changes: 7 additions & 3 deletions screens/TokenDetail/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useEffect, useState, createContext, useContext, useMemo } from "react";
import { Modal as MUIModal } from "@mui/material";
import { twMerge } from "tailwind-merge";
import { LayoutBox } from "../../components/LayoutContainer/LayoutContainer";
import { showModal } from "../../redux/appSlice";
import { updatePosition } from "../../redux/appSlice";
import {
ArrowLeft,
SuppliedEmptyIcon,
Expand Down Expand Up @@ -742,6 +742,7 @@ function TokenUserInfo() {
const isWrappedNear = tokenRow.symbol === "NEAR";
const { supplyBalance, maxBorrowAmountPositions } = useUserBalance(tokenId, isWrappedNear);
const handleSupplyClick = useSupplyTrigger(tokenId);
const handleBorrowClick = useBorrowTrigger(tokenId);
const dispatch = useAppDispatch();
function getIcons() {
return (
Expand Down Expand Up @@ -833,7 +834,8 @@ function TokenUserInfo() {
disabled={!+totalBorrowAmount}
className="w-1 flex-grow"
onClick={() => {
dispatch(showModal(getBorrowTemplate(tokenId, DEFAULT_POSITION)));
handleBorrowClick();
dispatch(updatePosition({ position: DEFAULT_POSITION }));
}}
>
Borrow
Expand Down Expand Up @@ -966,6 +968,7 @@ function YouBorrowed() {
[[], 0],
) || [[], 0];
const dispatch = useAppDispatch();
const handleRepayClick = useRepayTrigger(tokenId);
const is_empty = !borrowed && !Object.keys(borrowedLp).length;
const borrowedList = { ...borrowedLp };
if (borrowed) {
Expand Down Expand Up @@ -1071,7 +1074,8 @@ function YouBorrowed() {
<RedLineButton
className="w-1 flex-grow"
onClick={() => {
dispatch(showModal(getRepayTemplate(tokenId, position)));
handleRepayClick();
dispatch(updatePosition({ position }));
}}
>
Repay
Expand Down
6 changes: 6 additions & 0 deletions store/actions/borrow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ export async function borrow({
},
},
},
{
Withdraw: {
token_id: tokenId,
max_amount: expandedAmount.toFixed(0),
},
},
],
},
};
Expand Down

0 comments on commit 43b1a02

Please sign in to comment.