Skip to content

Commit

Permalink
refactor(web): min-stake-check
Browse files Browse the repository at this point in the history
  • Loading branch information
Harman-singh-waraich committed Dec 19, 2024
1 parent f9cb5a6 commit c4916c2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
19 changes: 15 additions & 4 deletions web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import React, { useState, useMemo, useEffect } from "react";
import styled from "styled-components";

import { hoverShortTransitionTiming } from "styles/commonStyles";

import { useParams } from "react-router-dom";
import { useDebounce } from "react-use";

import { useParsedAmount } from "hooks/useParsedAmount";
import { usePnkData } from "hooks/usePNKData";
import { commify, uncommify } from "utils/commify";
import { formatPNK, roundNumberDown } from "utils/format";
import { formatETH, formatPNK, roundNumberDown } from "utils/format";
import { isUndefined } from "utils/index";

import { useCourtDetails } from "queries/useCourtDetails";

import { hoverShortTransitionTiming } from "styles/commonStyles";

import { NumberInputField } from "components/NumberInputField";

import StakeWithdrawButton, { ActionType } from "./StakeWithdrawButton";
Expand Down Expand Up @@ -75,6 +77,7 @@ const InputDisplay: React.FC<IInputDisplay> = ({ action, amount, setAmount }) =>

const { id } = useParams();
const { balance, jurorBalance } = usePnkData({ courtId: id });
const { data: courtDetails } = useCourtDetails(id);

const parsedBalance = formatPNK(balance ?? 0n, 0, true);

Expand All @@ -88,10 +91,18 @@ const InputDisplay: React.FC<IInputDisplay> = ({ action, amount, setAmount }) =>
setErrorMsg("Insufficient balance to stake this amount");
} else if (!isStaking && jurorBalance && parsedAmount > jurorBalance[2]) {
setErrorMsg("Insufficient staked amount to withdraw this amount");
} else if (
action === ActionType.stake &&
courtDetails &&
jurorBalance &&
parsedAmount !== 0n &&
jurorBalance[2] + parsedAmount < BigInt(courtDetails?.court?.minStake)
) {
setErrorMsg(`Min Stake in court is: ${formatETH(courtDetails?.court?.minStake)}`);
} else {
setErrorMsg(undefined);
}
}, [parsedAmount, isStaking, balance, jurorBalance]);
}, [parsedAmount, isStaking, balance, jurorBalance, action, courtDetails]);

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import {
useSimulatePnkIncreaseAllowance,
useWritePnkIncreaseAllowance,
} from "hooks/contracts/generated";
import { useCourtDetails } from "hooks/queries/useCourtDetails";
import { useLockOverlayScroll } from "hooks/useLockOverlayScroll";
import { usePnkData } from "hooks/usePNKData";
import { formatETH } from "utils/format";
import { isUndefined } from "utils/index";
import { parseWagmiError } from "utils/parseWagmiError";
import { refetchWithRetry } from "utils/refecthWithRetry";

import { useCourtDetails } from "queries/useCourtDetails";

import { EnsureChain } from "components/EnsureChain";

import StakeWithdrawPopup from "./StakeWithdrawPopup";
Expand Down Expand Up @@ -67,9 +67,8 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
const controllerRef = useRef<AbortController | null>(null);
useLockOverlayScroll(isPopupOpen);

const { data: courtDetails } = useCourtDetails(id);
const { balance, jurorBalance, allowance, refetchAllowance } = usePnkData({ courtId: id });

const { data: courtDetails } = useCourtDetails(id);
const publicClient = usePublicClient();

const isStaking = action === ActionType.stake;
Expand Down Expand Up @@ -181,6 +180,16 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
)
);
});
} else {
updatePopupState(
signal,
getStakeSteps(
StakeSteps.StakeFailed,
...commonArgs,
undefined,
new Error("Simulation Failed. Please restart the process.")
)
);
}
},
[setStake, setStakeConfig, publicClient, amount, theme, action]
Expand Down Expand Up @@ -248,20 +257,20 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({

useEffect(() => {
if (isPopupOpen) return;
if (
action === ActionType.stake &&
targetStake !== 0n &&
courtDetails &&
targetStake < BigInt(courtDetails.court?.minStake)
) {
setErrorMsg(`Min Stake in court is: ${formatETH(courtDetails?.court?.minStake)}`);
} else if (setStakeError || allowanceError) {
if (setStakeError || allowanceError) {
setErrorMsg(parseWagmiError(setStakeError || allowanceError));
}
}, [setStakeError, setErrorMsg, targetStake, courtDetails, allowanceError, isPopupOpen, action]);
}, [setStakeError, setErrorMsg, targetStake, allowanceError, isPopupOpen]);

const isDisabled = useMemo(() => {
if (parsedAmount == 0n) return true;
if (
parsedAmount == 0n ||
(action === ActionType.stake &&
targetStake !== 0n &&
courtDetails &&
targetStake < BigInt(courtDetails?.court?.minStake))
)
return true;
if (isAllowance) {
return isUndefined(increaseAllowanceConfig) || isSimulatingAllowance || !isUndefined(allowanceError);
}
Expand All @@ -275,6 +284,9 @@ const StakeWithdrawButton: React.FC<IActionButton> = ({
setStakeError,
allowanceError,
isAllowance,
targetStake,
action,
courtDetails,
]);

const closePopup = () => {
Expand Down

0 comments on commit c4916c2

Please sign in to comment.