Skip to content

Commit

Permalink
Merge branch 'dev' into fix/reserves_stakes_fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aerilym authored Feb 10, 2025
2 parents 284fb1d + ac35e06 commit b9abca0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 53 deletions.
3 changes: 1 addition & 2 deletions apps/staking/app/mystakes/modules/useTotalStaked.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ export function useTotalStaked(addressOverride?: Address) {
const stakedStakes = stakes.filter((stake) => {
const eventState = parseStakeEventState(stake);
return (
!(eventState === STAKE_EVENT_STATE.EXITED || eventState === STAKE_EVENT_STATE.LIQUIDATED) &&
currentContractIds?.has(stake.contract_id)
!(eventState === STAKE_EVENT_STATE.EXITED) && currentContractIds?.has(stake.contract_id)
);
});

Expand Down
18 changes: 9 additions & 9 deletions apps/staking/components/StakedNode/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import {
} from '@session/staking-api-js/client';

enum EVENT {
/** Emitted when a new service node is added to the network */
NewServiceNodeV2 = 'NewServiceNodeV2',
/** Emitted when a service node exit request is initiated */
ServiceNodeExitRequest = 'ServiceNodeExitRequest',
/** Emitted when a service node exits (by request or liquidation) */
ServiceNodeExit = 'ServiceNodeExit',
/** Emitted when a service node is liquidated NOTE: Always followed by ServiceNodeExit */
ServiceNodeLiquidated = 'ServiceNodeLiquidated',
}

Expand All @@ -17,7 +21,6 @@ export enum STAKE_EVENT_STATE {
ACTIVE,
EXIT_REQUESTED,
EXITED,
LIQUIDATED,
}

export function parseStakeEventState(stake: Stake) {
Expand All @@ -32,18 +35,15 @@ export function parseStakeEventState(stake: Stake) {
case EVENT.ServiceNodeExitRequest:
return STAKE_EVENT_STATE.EXIT_REQUESTED;
case EVENT.ServiceNodeExit:
return STAKE_EVENT_STATE.EXITED;
case EVENT.ServiceNodeLiquidated:
return STAKE_EVENT_STATE.LIQUIDATED;
return STAKE_EVENT_STATE.EXITED;
default:
return STAKE_EVENT_STATE.UNKNOWN;
}
}

export enum STAKE_STATE {
/** @see {STAKE_EVENT_STATE.EXITED}
* OR
* @see {STAKE_EVENT_STATE.LIQUIDATED}
*
* To determine if the stake was exited by `unlock` or `deregistration`, use {@link isStakeDeregistered}
* */
Expand Down Expand Up @@ -89,14 +89,14 @@ export function isStakeReadyToExit(stake: Stake, blockHeight: number) {
export function parseStakeState(stake: Stake, blockHeight: number) {
const eventState = parseStakeEventState(stake);

if (eventState === STAKE_EVENT_STATE.EXITED || eventState === STAKE_EVENT_STATE.LIQUIDATED) {
return STAKE_STATE.EXITED;
}

if (isStakeDeregistered(stake)) {
return STAKE_STATE.DEREGISTERED;
}

if (eventState === STAKE_EVENT_STATE.EXITED) {
return STAKE_STATE.EXITED;
}

if (eventState === STAKE_EVENT_STATE.EXIT_REQUESTED) {
return isStakeReadyToExit(stake, blockHeight) ? STAKE_STATE.AWAITING_EXIT : STAKE_STATE.RUNNING;
}
Expand Down
64 changes: 22 additions & 42 deletions apps/staking/components/StakedNodeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,11 @@ export const isReadyToExitByUnlock = (
!!(
state === STAKE_STATE.AWAITING_EXIT &&
eventState !== STAKE_EVENT_STATE.EXITED &&
eventState !== STAKE_EVENT_STATE.LIQUIDATED &&
unlockHeight &&
blockHeight &&
unlockHeight <= blockHeight
);

export const useIsReadyToExitByDeregistrationUnlock = (
state: STAKE_STATE,
eventState: STAKE_EVENT_STATE,
deregistrationHeight?: number | null,
blockHeight?: number
) => {
return !!(
state === STAKE_STATE.DEREGISTERED &&
eventState !== STAKE_EVENT_STATE.EXITED &&
eventState !== STAKE_EVENT_STATE.LIQUIDATED &&
deregistrationHeight &&
blockHeight &&
deregistrationHeight <= blockHeight
);
};

function getNodeStatus(state: STAKE_STATE): VariantProps<typeof statusVariants>['status'] {
switch (state) {
case STAKE_STATE.RUNNING:
Expand Down Expand Up @@ -236,12 +219,20 @@ const ExitUnlockTimerNotification = ({
const notFoundString = dictionaryGeneral('notFound');
const soonString = dictionaryGeneral('soon');

const isExitableSoon = useMemo(() => isDateSoonOrPast(date), [date]);
const [isExitableSoon, isPastTime] = useMemo(
() => [isDateSoonOrPast(date), date && Date.now() > date.getTime()],
[date]
);

const relativeTime = useMemo(
() => (!isExitableSoon ? timeString : soonString),
[isExitableSoon, timeString, soonString, notFoundString]
);

if (isPastTime) {
return null;
}

return (
<Tooltip
tooltipContent={dictionary(
Expand Down Expand Up @@ -335,15 +326,7 @@ const NodeSummary = ({
isInContractIdList,
}: NodeSummaryProps) => {
const eventState = parseStakeEventState(node);
const isExited =
eventState === STAKE_EVENT_STATE.EXITED || eventState === STAKE_EVENT_STATE.LIQUIDATED;

const isReadyToUnlockByDeregistration = useIsReadyToExitByDeregistrationUnlock(
state,
eventState,
node.deregistration_height,
blockHeight
);
const isExited = eventState === STAKE_EVENT_STATE.EXITED;

const contributors = (
<NodeContributorList
Expand All @@ -357,20 +340,18 @@ const NodeSummary = ({
<>
{contributors}
{!isExited && isInContractIdList ? (
isReadyToUnlockByDeregistration ? (
<ReadyForExitNotification
timeString={liquidationTime}
date={liquidationDate}
isDeregistered
/>
) : (
<ExitUnlockTimerNotification
timeString={deregistrationUnlockTime}
date={deregistrationUnlockDate}
isDeregistered
/>
)
) : null}
<ReadyForExitNotification
timeString={liquidationTime}
date={liquidationDate}
isDeregistered
/>
) : (
<ExitUnlockTimerNotification
timeString={deregistrationUnlockTime}
date={deregistrationUnlockDate}
isDeregistered
/>
)}
</>
);
}
Expand Down Expand Up @@ -708,7 +689,6 @@ function StakeNodeCardButton({
if (
state === STAKE_STATE.EXITED ||
eventState === STAKE_EVENT_STATE.EXITED ||
eventState === STAKE_EVENT_STATE.LIQUIDATED ||
state === STAKE_STATE.DEREGISTERED
) {
return null;
Expand Down

0 comments on commit b9abca0

Please sign in to comment.