Skip to content

Commit

Permalink
Revert "feat: use new cli field canUnstake for disabling unstaking (#…
Browse files Browse the repository at this point in the history
…60)"

This reverts commit bd59c29.
  • Loading branch information
chrypnotoad committed Nov 12, 2024
1 parent bd59c29 commit c2e0a36
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 32 deletions.
18 changes: 9 additions & 9 deletions components/molecules/RewardsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,16 @@ export const RewardsCard = () => {
const { chain } = useNetwork();
const [canRedeem, setCanRedeem] = useState(
isConnected &&
chain?.id === CHAIN_ID &&
nodeStatus?.state === "stopped" &&
parseFloat(nodeStatus?.lockedStake || "0") > 0 &&
nodeStatus?.unstakable?.canUnstake
chain?.id === CHAIN_ID &&
nodeStatus?.state === "stopped" &&
parseFloat(nodeStatus?.lockedStake || "0") > 0
);
useEffect(() => {
setCanRedeem(
isConnected &&
chain?.id === CHAIN_ID &&
nodeStatus?.state === "stopped" &&
parseFloat(nodeStatus?.lockedStake || "0") > 0
chain?.id === CHAIN_ID &&
nodeStatus?.state === "stopped" &&
parseFloat(nodeStatus?.lockedStake || "0") > 0
);
}, [nodeStatus?.state, nodeStatus?.lockedStake, isConnected, chain?.id]);

Expand Down Expand Up @@ -92,8 +91,9 @@ export const RewardsCard = () => {
`border border-gray-400 mt-3 px-3 py-1 text-sm rounded ` +
(canRedeem
? "text-primary"
: `text-gray-400 ${nodeStatus?.state === "active" ? "tooltip" : ""
}`)
: `text-gray-400 ${
nodeStatus?.state === "active" ? "tooltip" : ""
}`)
}
data-tip="It is not possible to redeem rewards while you are validating.
If absolutely necessary, use the force stop option in settings (Not Recommended)."
Expand Down
28 changes: 18 additions & 10 deletions components/molecules/StakeDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import { ConfirmUnstakeModal } from "./ConfirmUnstakeModal";
import { ClipboardIcon } from "../atoms/ClipboardIcon";
import { MobileModalWrapper } from "../layouts/MobileModalWrapper";
import { useAccountStakeInfo } from "../../hooks/useAccountStakeInfo";
import { useSettings } from "../../hooks/useSettings";

export const StakeDisplay = () => {
const addressRef = useRef<HTMLSpanElement>(null);
const { address, isConnected } = useAccount();
const { stakeInfo } = useAccountStakeInfo(address);
const { chain } = useNetwork();
const { nodeStatus } = useNodeStatus();
const { settings } = useSettings();
const { setShowModal, setContent, resetModal } = useModalStore(
(state: any) => ({
setShowModal: state.setShowModal,
Expand All @@ -29,7 +31,7 @@ export const StakeDisplay = () => {
const minimumStakeRequirement = useMemo(() => {
return Math.max(
parseFloat(nodeStatus?.stakeRequirement || "10") -
parseFloat(nodeStatus?.lockedStake || "0"),
parseFloat(nodeStatus?.lockedStake || "0"),
0
);
}, [nodeStatus?.stakeRequirement, nodeStatus?.lockedStake]);
Expand All @@ -42,6 +44,14 @@ export const StakeDisplay = () => {
}
}, [nodeStatus?.state]);

const { isStoppedForLongerThan15Minutes, remainingWaitTime } = useMemo(() => {
if (!settings?.lastStopped) return { isStoppedForLongerThan15Minutes: false, remainingWaitTime: 0 };
const timeDifference = Date.now() - settings.lastStopped;
const isStoppedForLongerThan15Minutes = timeDifference > 15 * 60 * 1000;
const remainingWaitTime = Math.max(15 * 60 * 1000 - timeDifference, 0);
return { isStoppedForLongerThan15Minutes, remainingWaitTime };
}, [settings?.lastStopped]);

const formatRemainingTime = (ms: number) => {
const minutes = Math.floor(ms / 60000);
const seconds = Math.floor((ms % 60000) / 1000);
Expand Down Expand Up @@ -93,24 +103,22 @@ export const StakeDisplay = () => {
className={`
bg-white border border-bodyFg text-sm px-3 py-2 rounded basis-0 grow
${hasNodeStopped && parseFloat(nodeStatus?.lockedStake || "0") > 0
? nodeStatus?.unstakable?.canUnstake
? isStoppedForLongerThan15Minutes
? "text-primary"
: "text-yellow-500"
: "text-gray-400"
}
${!nodeStatus?.unstakable?.canUnstake && parseFloat(nodeStatus?.lockedStake || "0") > 0 ? "tooltip tooltip-bottom" : ""}
${!isStoppedForLongerThan15Minutes && parseFloat(nodeStatus?.lockedStake || "0") > 0 ? "tooltip tooltip-bottom" : ""}
`}
data-tip={(
hasNodeStopped &&
nodeStatus?.unstakable?.canUnstake === false &&
nodeStatus?.unstakable?.remainingTime > 0)
? `Node is currently stopped and is being removed from the active validator list. Please wait for another ${formatRemainingTime(nodeStatus.unstakable.remainingTime)} before you can remove your stake.`
data-tip={
hasNodeStopped && !isStoppedForLongerThan15Minutes
? `Node is currently stopped and is being removed from the active validator list. Please wait for another ${formatRemainingTime(remainingWaitTime)} before you can remove your stake.`
: "It is not recommended to unstake while validating. If absolutely necessary, use the force remove option in settings to remove stake (Not Recommended)."
}
disabled={
!hasNodeStopped ||
parseFloat(nodeStatus?.lockedStake || "0") === 0 ||
!nodeStatus?.unstakable
!isStoppedForLongerThan15Minutes
}
onClick={() => {
resetModal();
Expand Down Expand Up @@ -174,4 +182,4 @@ export const StakeDisplay = () => {
</div>
</Card>
);
};
};
21 changes: 8 additions & 13 deletions model/node-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,19 @@ import { NodePerformance } from "./node-performance";
export interface NodeStatus {
performance: NodePerformance;
state:
| "active"
| "standby"
| "stopped"
| "syncing"
| "need-stake"
| "waiting-for-network"
| "ready"
| "selected";
| "active"
| "standby"
| "stopped"
| "syncing"
| "need-stake"
| "waiting-for-network"
| "ready"
| "selected";
exitStatus: "Exit with warning" | "Exited cleanly" | "Exit with error";
exitMessage: string;
totalTimeValidating: number;
lastActive: string;
lockedStake: string;
unstakable: {
canUnstake: boolean;
reason: string,
remainingTime: number,
};
stakeAddress: string;
stakeRequirement: string;
nominatorAddress: string;
Expand Down

0 comments on commit c2e0a36

Please sign in to comment.