Skip to content

Commit

Permalink
Merge pull request #34 from burrowHQ/usn-repay-limit
Browse files Browse the repository at this point in the history
Usn repay limit
  • Loading branch information
aidai524 authored Jan 3, 2024
2 parents 1e80ffc + 1cc6365 commit 097296d
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 31 deletions.
42 changes: 30 additions & 12 deletions components/Modal/Action.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { useState, useMemo, useEffect } from "react";
import { Box, Typography, Switch, Tooltip, Alert, useTheme } from "@mui/material";
import LoadingButton from "@mui/lab/LoadingButton";

import { FcInfo } from "@react-icons/all-files/fc/FcInfo";
import Decimal from "decimal.js";
import { nearTokenId } from "../../utils";
import { toggleUseAsCollateral, hideModal } from "../../redux/appSlice";
import { getModalData } from "./utils";
Expand All @@ -18,22 +15,24 @@ import { getSelectedValues, getAssetData } from "../../redux/appSelectors";
import { trackActionButton, trackUseAsCollateral } from "../../utils/telemetry";
import { useDegenMode } from "../../hooks/hooks";
import { SubmitButton, AlertWarning } from "./components";
import { expandToken } from "../../store";

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

const { available, canUseAsCollateral, extraDecimals, collateral, disabled } = getModalData({
...asset,
maxBorrowAmount,
healthFactor,
amount,
isRepayFromDeposits,
});
const { available, canUseAsCollateral, extraDecimals, collateral, disabled, decimals } =
getModalData({
...asset,
maxBorrowAmount,
healthFactor,
amount,
isRepayFromDeposits,
});

useEffect(() => {
if (!canUseAsCollateral) {
Expand Down Expand Up @@ -96,11 +95,30 @@ export default function Action({ maxBorrowAmount, healthFactor }) {
extraDecimals,
});
} else {
let usnMinRepay = "0";
const isUsn = tokenId === "usn";
if (isUsn && poolAsset?.supplied?.shares) {
// usnMinRepay = new Decimal(
// expandToken(
// new Decimal(poolAsset?.supplied?.balance)
// .div(poolAsset?.supplied?.shares)
// .mul(2)
// .toFixed(0, 2),
// decimals,
// ),
// ).toFixed(0);
usnMinRepay = new Decimal(poolAsset?.supplied?.balance)
.div(poolAsset?.supplied?.shares)
.mul(2)
.toFixed(0, 2);
}
await repay({
tokenId,
amount,
extraDecimals,
isMax,
isUsn,
usnMinRepay,
});
}
break;
Expand Down
9 changes: 7 additions & 2 deletions components/Modal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ 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();

const { action = "Deposit", tokenId } = asset;

const healthFactor = useAppSelector(
Expand Down Expand Up @@ -78,6 +78,7 @@ const Modal = () => {
isRepayFromDeposits,
healthFactor,
amount,
poolAsset: assets[tokenId],
});

const total = (price * +amount).toLocaleString(undefined, USD_FORMAT);
Expand Down Expand Up @@ -129,7 +130,11 @@ const Modal = () => {
)}
</div>
<Alerts data={alerts} />
<Action maxBorrowAmount={maxBorrowAmount} healthFactor={healthFactor} />
<Action
maxBorrowAmount={maxBorrowAmount}
healthFactor={healthFactor}
poolAsset={assets[tokenId]}
/>
</Box>
</Wrapper>
</MUIModal>
Expand Down
25 changes: 21 additions & 4 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,8 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
maxWithdrawAmount,
isRepayFromDeposits,
canUseAsCollateral,
tokenId,
poolAsset,
} = asset;
const data: any = {
apy: borrowApy,
Expand Down Expand Up @@ -117,7 +119,22 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
data.available = getAvailableWithdrawOrAdjust;
data.rates = [];
break;
case "Repay":
case "Repay": {
let minRepay = "0";
const isUsn = tokenId === "usn";
if (isUsn && poolAsset?.supplied?.shares) {
// minRepay = new Decimal(poolAsset?.supplied?.balance)
// .div(poolAsset?.supplied?.shares)
// .mul(2)
// .toNumber();
minRepay = shrinkToken(
new Decimal(poolAsset?.supplied?.balance)
.div(poolAsset?.supplied?.shares)
.mul(2)
.toFixed(0, 2),
18,
);
}
data.totalTitle = `Repay Borrow Amount`;
data.available = toDecimal(
isRepayFromDeposits
Expand All @@ -126,7 +143,7 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
isWrappedNear
? Number(Math.max(0, available + availableNEAR - NEAR_STORAGE_DEPOSIT))
: available,
borrowed,
isUsn ? Math.max(borrowed, +minRepay) : borrowed,
),
);
data.alerts = {};
Expand All @@ -146,7 +163,7 @@ export const getModalData = (asset): UIAsset & Props & { disabled: boolean } =>
});
}
break;

}
default:
}
if (
Expand Down
10 changes: 7 additions & 3 deletions store/actions/repay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export async function repay({
amount,
extraDecimals,
isMax,
usnMinRepay,
isUsn,
}: {
tokenId: string;
amount: string;
extraDecimals: number;
isMax: boolean;
usnMinRepay: string;
isUsn: boolean;
}) {
const { account, logicContract } = await getBurrow();
const tokenContract = await getTokenContract(tokenId);
Expand All @@ -32,8 +36,9 @@ export async function repay({
);

const extraDecimalMultiplier = expandTokenDecimal(1, extraDecimals);

const tokenBorrowedBalance = borrowedBalance.divToInt(extraDecimalMultiplier);
const tokenBorrowedBalance = isUsn
? Decimal.max(borrowedBalance.divToInt(extraDecimalMultiplier), usnMinRepay)
: borrowedBalance.divToInt(extraDecimalMultiplier);

const tokenBalance = new Decimal(await getBalance(tokenId, account.accountId));
const accountBalance = decimalMax(
Expand All @@ -43,7 +48,6 @@ export async function repay({

const maxAvailableBalance = isNEAR ? tokenBalance.add(accountBalance) : tokenBalance;
const maxAmount = decimalMin(tokenBorrowedBalance, maxAvailableBalance);

const expandedAmountToken = isMax
? maxAmount
: decimalMin(maxAmount, expandTokenDecimal(amount, decimals));
Expand Down
10 changes: 3 additions & 7 deletions styles/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ body {
color: #fff;
}

#__next{
#__next {
background: #14162b;
height: 100%;
}
Expand Down Expand Up @@ -740,11 +740,7 @@ options-list::-webkit-scrollbar {
.nws-form-control .account br {
display: none;
}
.nws-modal-wrapper
.nws-modal
.choose-ledger-account-form-wrapper
.nws-form-control
.account {
.nws-modal-wrapper .nws-modal .choose-ledger-account-form-wrapper .nws-form-control .account {
display: flex;
align-items: center;
gap: 4px;
Expand All @@ -755,7 +751,7 @@ options-list::-webkit-scrollbar {
.choose-ledger-account-form-wrapper
.nws-form-control
.account
input[type='checkbox'] {
input[type="checkbox"] {
transform: scale(1.2);
}
.nws-modal-wrapper .nws-modal .overview-wrapper .account {
Expand Down
6 changes: 3 additions & 3 deletions utils/wallet-selector-compat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ export const getWalletSelector = async ({ onAccountChange }: GetWalletSelectorAr
debug: !!isTestnet,
optimizeWalletOrder: false,
});

const subscription = selector.store.observable
const { observable }: { observable: any } = selector.store;
const subscription = observable
.pipe(
map((s) => s.accounts),
map((s: any) => s.accounts),
distinctUntilChanged(),
)
.subscribe((nextAccounts) => {
Expand Down

0 comments on commit 097296d

Please sign in to comment.