diff --git a/components/Modal/index.tsx b/components/Modal/index.tsx index c32116db..e183030f 100644 --- a/components/Modal/index.tsx +++ b/components/Modal/index.tsx @@ -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 { @@ -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; @@ -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, diff --git a/redux/selectors/getBorrowMaxAmount.ts b/redux/selectors/getBorrowMaxAmount.ts index 17851ff6..cf6ecd57 100644 --- a/redux/selectors/getBorrowMaxAmount.ts +++ b/redux/selectors/getBorrowMaxAmount.ts @@ -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), @@ -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( diff --git a/redux/selectors/recomputeHealthFactor.ts b/redux/selectors/recomputeHealthFactor.ts index 78b16bf3..9833d7ca 100644 --- a/redux/selectors/recomputeHealthFactor.ts +++ b/redux/selectors/recomputeHealthFactor.ts @@ -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 }; diff --git a/redux/selectors/recomputeHealthFactorAdjust.ts b/redux/selectors/recomputeHealthFactorAdjust.ts index e7dbd44e..62617c45 100644 --- a/redux/selectors/recomputeHealthFactorAdjust.ts +++ b/redux/selectors/recomputeHealthFactorAdjust.ts @@ -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 }; diff --git a/redux/selectors/recomputeHealthFactorRepay.ts b/redux/selectors/recomputeHealthFactorRepay.ts index 9fdded99..c3668e11 100644 --- a/redux/selectors/recomputeHealthFactorRepay.ts +++ b/redux/selectors/recomputeHealthFactorRepay.ts @@ -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 }; diff --git a/redux/selectors/recomputeHealthFactorRepayFromDeposits.ts b/redux/selectors/recomputeHealthFactorRepayFromDeposits.ts index 6ae8262b..81aba3a0 100644 --- a/redux/selectors/recomputeHealthFactorRepayFromDeposits.ts +++ b/redux/selectors/recomputeHealthFactorRepayFromDeposits.ts @@ -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 }; diff --git a/redux/selectors/recomputeHealthFactorSupply.ts b/redux/selectors/recomputeHealthFactorSupply.ts index 4bede9b9..af6500c6 100644 --- a/redux/selectors/recomputeHealthFactorSupply.ts +++ b/redux/selectors/recomputeHealthFactorSupply.ts @@ -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 }; diff --git a/redux/selectors/recomputeHealthFactorWithdraw.ts b/redux/selectors/recomputeHealthFactorWithdraw.ts index 5f239d67..19ff9f56 100644 --- a/redux/selectors/recomputeHealthFactorWithdraw.ts +++ b/redux/selectors/recomputeHealthFactorWithdraw.ts @@ -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 }; diff --git a/store/actions/shadow.ts b/store/actions/shadow.ts index 4332b029..bc676b97 100644 --- a/store/actions/shadow.ts +++ b/store/actions/shadow.ts @@ -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 { 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({ @@ -105,7 +102,7 @@ export async function shadow_action_withdraw({ args: { action: "FromBurrowland", pool_id, - ...(isMax ? {} : { amount: expandAmount }), + amount: expandAmount, msg: "", }, }, diff --git a/store/actions/withdraw.ts b/store/actions/withdraw.ts index a35bd27f..8206a34c 100644 --- a/store/actions/withdraw.ts +++ b/store/actions/withdraw.ts @@ -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, }); @@ -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,