Skip to content

Commit

Permalink
fix conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
xieqiancaosissi committed Jan 18, 2024
2 parents 1d58793 + d203357 commit 72b9551
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 14 deletions.
36 changes: 32 additions & 4 deletions components/Modal/Action.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useState, useMemo, useEffect } from "react";
import { Box, Typography, Switch, Tooltip, Alert, useTheme } from "@mui/material";
import LoadingButton from "@mui/lab/LoadingButton";

import Decimal from "decimal.js";
import { FcInfo } from "@react-icons/all-files/fc/FcInfo";
import { nearTokenId } from "../../utils";
import { toggleUseAsCollateral, hideModal } from "../../redux/appSlice";
Expand All @@ -21,13 +21,14 @@ import { useDegenMode } from "../../hooks/hooks";
import { SubmitButton, AlertWarning } from "./components";
import { getAccountPortfolio } from "../../redux/accountSelectors";
import getShadowRecords from "../../api/get-shadows";
import { expandToken } from "../../store";

export default function Action({ maxBorrowAmount, healthFactor, collateralType }) {
export default function Action({ maxBorrowAmount, healthFactor, collateralType, poolAsset }) {
const [loading, setLoading] = useState(false);
const { amount, useAsCollateral, isMax } = useAppSelector(getSelectedValues);
const dispatch = useAppDispatch();
const asset = useAppSelector(getAssetData);
const { action = "Deposit", tokenId, isLpToken, decimals } = asset;
const { action = "Deposit", tokenId, isLpToken, decimals, borrowApy, price, borrowed } = asset;
const { isRepayFromDeposits } = useDegenMode();

const { available, canUseAsCollateral, extraDecimals, collateral, disabled } = getModalData({
Expand Down Expand Up @@ -102,13 +103,37 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
isMax,
});
break;
case "Repay":
case "Repay": {
// TODO
let minRepay = "0";
let interestChargedIn1min = "0";
if (borrowApy && price && borrowed) {
interestChargedIn1min = expandToken(
new Decimal(borrowApy)
.div(365 * 24 * 60)
.div(100)
.mul(borrowed)
.toFixed(),
decimals || 0,
0,
);
if (+interestChargedIn1min === 0) {
interestChargedIn1min = "1";
}
}
if (poolAsset?.supplied?.shares) {
minRepay = new Decimal(poolAsset?.supplied?.balance)
.div(poolAsset?.supplied?.shares)
.mul(2)
.toFixed(0, 2);
}
if (isRepayFromDeposits) {
await repayFromDeposits({
tokenId,
amount,
extraDecimals,
position: collateralType,
isMax,
});
} else {
await repay({
Expand All @@ -117,9 +142,12 @@ export default function Action({ maxBorrowAmount, healthFactor, collateralType }
extraDecimals,
isMax,
position: collateralType,
minRepay,
interestChargedIn1min,
});
}
break;
}
default:
break;
}
Expand Down
2 changes: 1 addition & 1 deletion components/Modal/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { toggleUseAsCollateral, hideModal, showModal } from "../../redux/appSlic
import { isInvalid, formatWithCommas_usd } from "../../utils/uiNumber";
import { YellowSolidSubmitButton, RedSolidSubmitButton } from "./button";
import { getCollateralAmount } from "../../redux/selectors/getCollateralAmount";
import { TipIcon, CloseIcon, WarnIcon, ArrowRight } from "./svg";
import { TipIcon, CloseIcon, WarnIcon, JumpTipIcon, ArrowRight } from "./svg";
import ReactToolTip from "../ToolTip";
import { IToken } from "../../interfaces/asset";

Expand Down
3 changes: 3 additions & 0 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const Modal = () => {
const accountId = useAppSelector(getAccountId);
const asset = useAppSelector(getAssetData);
const { amount } = useAppSelector(getSelectedValues);
const assets = useAppSelector((state) => state.assets?.data || {});
const dispatch = useAppDispatch();
const { isRepayFromDeposits } = useDegenMode();
const theme = useTheme();
Expand Down Expand Up @@ -98,6 +99,7 @@ const Modal = () => {
healthFactor,
amount,
borrowed: repayAmount,
poolAsset: assets[tokenId],
});
const handleClose = () => dispatch(hideModal());
useEffect(() => {
Expand Down Expand Up @@ -186,6 +188,7 @@ const Modal = () => {
maxBorrowAmount={maxBorrowAmount}
healthFactor={healthFactor}
collateralType={selectedCollateralType}
poolAsset={assets[tokenId]}
/>
</Box>
</ModalContext.Provider>
Expand Down
19 changes: 18 additions & 1 deletion components/Modal/svg.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export function WarnIcon(props) {
</svg>
);
}

export function ArrowRight(props) {
return (
<svg
Expand All @@ -84,3 +83,21 @@ export function ArrowRight(props) {
</svg>
);
}
export function JumpTipIcon(props) {
return (
<svg
{...props}
width="12"
height="12"
viewBox="0 0 12 12"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M7 4C7 4.55234 6.55234 5 6 5C5.72422 5 5.47422 4.88828 5.29297 4.70703C5.11172 4.52578 5 4.27578 5 4C5 3.72422 5.11172 3.47422 5.29297 3.29297C5.47422 3.11172 5.72422 3 6 3C6.27578 3 6.52578 3.11172 6.70703 3.29297C6.88828 3.47422 7 3.72422 7 4ZM7 6V9.25C7 9.52578 6.77578 9.75 6.5 9.75H5.5C5.22422 9.75 5 9.52578 5 9.25V6C5 5.72344 5.22422 5.5 5.5 5.5H6.5C6.77578 5.5 7 5.72344 7 6Z"
fill="currentColor"
/>
<circle cx="6" cy="6" r="5.5" stroke="currentColor" />
</svg>
);
}
37 changes: 32 additions & 5 deletions components/Modal/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Decimal from "decimal.js";
import { USD_FORMAT, TOKEN_FORMAT, PERCENT_DIGITS, NEAR_STORAGE_DEPOSIT } from "../../store";
import type { UIAsset } from "../../interfaces";
import { formatWithCommas_number, toDecimal } from "../../utils/uiNumber";
import { expandToken } from "../../store/helper";
import { expandToken, shrinkToken } from "../../store/helper";
import { decimalMax } from "../../utils";

interface Alert {
Expand Down Expand Up @@ -51,6 +51,10 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
maxWithdrawAmount,
isRepayFromDeposits,
canUseAsCollateral,
tokenId,
poolAsset,
decimals,
extraDecimals,
} = asset;
const data: any = {
apy: borrowApy,
Expand Down Expand Up @@ -117,16 +121,39 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
data.available = getAvailableWithdrawOrAdjust;
data.rates = [];
break;
case "Repay":
case "Repay": {
// TODO
let minRepay = "0";
if (poolAsset?.supplied?.shares) {
minRepay = shrinkToken(
new Decimal(poolAsset?.supplied?.balance)
.div(poolAsset?.supplied?.shares)
.mul(2)
.toFixed(0, 2),
decimals,
);
}
let interestChargedIn1min = "0";
if (borrowApy && price && borrowed) {
interestChargedIn1min = new Decimal(borrowApy)
.div(365 * 24 * 60)
.div(100)
.mul(borrowed)
.toFixed(decimals, 2);
}
const repayAmount = Decimal.max(
new Decimal(borrowed).plus(interestChargedIn1min),
minRepay,
).toNumber();
data.totalTitle = `Repay Borrow Amount`;
data.available = toDecimal(
isRepayFromDeposits
? Math.min(maxWithdrawAmount, borrowed)
? Math.min(maxWithdrawAmount, repayAmount)
: Math.min(
isWrappedNear
? Number(Math.max(0, available + availableNEAR - NEAR_STORAGE_DEPOSIT))
: available,
borrowed,
repayAmount,
),
);
data.alerts = {};
Expand All @@ -146,7 +173,7 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
});
}
break;

}
default:
}
if (
Expand Down
10 changes: 9 additions & 1 deletion store/actions/repay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ export async function repay({
extraDecimals,
isMax,
position,
minRepay,
interestChargedIn1min,
}: {
tokenId: string;
amount: string;
extraDecimals: number;
isMax: boolean;
position: string;
minRepay: string;
interestChargedIn1min: string;
}) {
const { account, logicContract } = await getBurrow();
const tokenContract = await getTokenContract(tokenId);
Expand All @@ -34,7 +38,11 @@ export async function repay({
0,
);
const extraDecimalMultiplier = expandTokenDecimal(1, extraDecimals);
const tokenBorrowedBalance = borrowedBalance.divToInt(extraDecimalMultiplier);
// TODO
const tokenBorrowedBalance = Decimal.max(
borrowedBalance.divToInt(extraDecimalMultiplier).plus(interestChargedIn1min),
minRepay,
);

const tokenBalance = new Decimal(await getBalance(tokenId, account.accountId));
const accountBalance = decimalMax(
Expand Down
4 changes: 3 additions & 1 deletion store/actions/repayFromDeposits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export async function repayFromDeposits({
amount,
extraDecimals,
position,
isMax,
}: {
tokenId: string;
amount: string;
extraDecimals: number;
position?: string;
isMax: boolean;
}) {
const { logicContract, oracleContract } = await getBurrow();
const { decimals } = (await getMetadata(tokenId))!;
Expand All @@ -42,7 +44,7 @@ export async function repayFromDeposits({
? {
Repay: {
token_id: tokenId,
amount: expandedAmount.mul(extraDecimalMultiplier).toFixed(0),
amount: isMax ? undefined : expandedAmount.mul(extraDecimalMultiplier).toFixed(0),
},
}
: {
Expand Down
1 change: 1 addition & 0 deletions store/assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
} from "../interfaces";
import { getBurrow } from "../utils";
import { DEFAULT_PRECISION } from "./constants";
import { lpTokenPrefix } from "../utils/config";

Decimal.set({ precision: DEFAULT_PRECISION });

Expand Down
1 change: 1 addition & 0 deletions utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export const getBurrow = async ({
throw err;
}
};

const call = async (
contract: Contract,
methodName: string,
Expand Down
2 changes: 1 addition & 1 deletion utils/wallet-selector-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export const getWalletSelector = async ({ onAccountChange }: GetWalletSelectorAr
debug: !!isTestnet,
optimizeWalletOrder: false,
});
const observable = selector.store.observable as any;
const { observable }: { observable: any } = selector.store;
const subscription = observable
.pipe(
map((s: any) => s.accounts),
Expand Down

0 comments on commit 72b9551

Please sign in to comment.