Skip to content

Commit

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

This reverts commit c2e0a36.
  • Loading branch information
urnotsam committed Nov 15, 2024
1 parent 20ad8e7 commit 853e9a0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
18 changes: 9 additions & 9 deletions components/molecules/RewardsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,17 @@ export const RewardsCard = () => {
const { chain } = useNetwork();
const [canRedeem, setCanRedeem] = useState(
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?.unstakable?.canUnstake
);
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 @@ -91,9 +92,8 @@ 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: 10 additions & 18 deletions components/molecules/StakeDisplay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ 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 @@ -31,7 +29,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 @@ -44,14 +42,6 @@ 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 @@ -103,22 +93,24 @@ 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
? isStoppedForLongerThan15Minutes
? nodeStatus?.unstakable?.canUnstake
? "text-primary"
: "text-yellow-500"
: "text-gray-400"
}
${!isStoppedForLongerThan15Minutes && parseFloat(nodeStatus?.lockedStake || "0") > 0 ? "tooltip tooltip-bottom" : ""}
${!nodeStatus?.unstakable?.canUnstake && parseFloat(nodeStatus?.lockedStake || "0") > 0 ? "tooltip tooltip-bottom" : ""}
`}
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.`
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.`
: "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 ||
!isStoppedForLongerThan15Minutes
!nodeStatus?.unstakable
}
onClick={() => {
resetModal();
Expand Down Expand Up @@ -182,4 +174,4 @@ export const StakeDisplay = () => {
</div>
</Card>
);
};
};
21 changes: 13 additions & 8 deletions model/node-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,24 @@ 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 853e9a0

Please sign in to comment.