Skip to content

Commit

Permalink
Merge pull request #1806 from kleros/refactor/min-stake-check
Browse files Browse the repository at this point in the history
refactor(web): min-stake-check
  • Loading branch information
alcercu authored Dec 19, 2024
2 parents 4a84994 + efedc56 commit a744c1f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 17 deletions.
17 changes: 14 additions & 3 deletions web/src/pages/Courts/CourtDetails/StakePanel/InputDisplay.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
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";

Expand All @@ -12,6 +10,10 @@ import { commify, uncommify } from "utils/commify";
import { 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: ${formatPNK(courtDetails?.court?.minStake)} PNK`);
} 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 a744c1f

Please sign in to comment.