diff --git a/components/Modal/index.tsx b/components/Modal/index.tsx index 6c109d5a..e8740b43 100644 --- a/components/Modal/index.tsx +++ b/components/Modal/index.tsx @@ -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"; @@ -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({ diff --git a/redux/appSlice.ts b/redux/appSlice.ts index 33cd2244..df52267d 100644 --- a/redux/appSlice.ts +++ b/redux/appSlice.ts @@ -96,6 +96,7 @@ export const initialState: AppState = { useAsCollateral: false, amount: "0", isMax: false, + position: undefined, }, staking: { amount: 0, @@ -148,7 +149,6 @@ export const appSlice = createSlice({ action: TokenAction; amount: string; tokenId: string; - position?: string; }>, ) { state.selected = { ...state.selected, isMax: false, ...action.payload }; @@ -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; }, @@ -251,6 +254,7 @@ export const { setTheme, setUnreadLiquidation, setToastMessage, + updatePosition, } = appSlice.actions; export default appSlice.reducer; diff --git a/redux/selectors/getCollateralAmount.ts b/redux/selectors/getCollateralAmount.ts index 2726d59f..98d6927b 100644 --- a/redux/selectors/getCollateralAmount.ts +++ b/redux/selectors/getCollateralAmount.ts @@ -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( diff --git a/redux/selectors/recomputeHealthFactorAdjust.ts b/redux/selectors/recomputeHealthFactorAdjust.ts index c8fb10b8..e7dbd44e 100644 --- a/redux/selectors/recomputeHealthFactorAdjust.ts +++ b/redux/selectors/recomputeHealthFactorAdjust.ts @@ -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", diff --git a/redux/selectors/recomputeHealthFactorSupply.ts b/redux/selectors/recomputeHealthFactorSupply.ts index 98248b4b..4bede9b9 100644 --- a/redux/selectors/recomputeHealthFactorSupply.ts +++ b/redux/selectors/recomputeHealthFactorSupply.ts @@ -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", diff --git a/redux/selectors/recomputeHealthFactorWithdraw.ts b/redux/selectors/recomputeHealthFactorWithdraw.ts index 77d3bd09..5f239d67 100644 --- a/redux/selectors/recomputeHealthFactorWithdraw.ts +++ b/redux/selectors/recomputeHealthFactorWithdraw.ts @@ -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", diff --git a/screens/TokenDetail/index.tsx b/screens/TokenDetail/index.tsx index de8e44c0..1a3a3616 100644 --- a/screens/TokenDetail/index.tsx +++ b/screens/TokenDetail/index.tsx @@ -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, @@ -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 ( @@ -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 @@ -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) { @@ -1071,7 +1074,8 @@ function YouBorrowed() { { - dispatch(showModal(getRepayTemplate(tokenId, position))); + handleRepayClick(); + dispatch(updatePosition({ position })); }} > Repay diff --git a/store/actions/borrow.ts b/store/actions/borrow.ts index b2620af5..8edf632a 100644 --- a/store/actions/borrow.ts +++ b/store/actions/borrow.ts @@ -104,6 +104,12 @@ export async function borrow({ }, }, }, + { + Withdraw: { + token_id: tokenId, + max_amount: expandedAmount.toFixed(0), + }, + }, ], }, };