Skip to content

Commit

Permalink
secondsUntilLosingReward util (#5875)
Browse files Browse the repository at this point in the history
# Motivation

For the periodic confirmation feature, the time until the neuron starts
losing rewards should be displayed. A function to retrieve this value in
seconds has been added here.

# Changes

- New util `secondsUntilLosingReward`.
- Refactor relater utils to use it.

# Tests

- Added.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
mstrasinskis authored Nov 28, 2024
1 parent 7476708 commit 558bd78
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
17 changes: 10 additions & 7 deletions frontend/src/lib/utils/neuron.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1186,16 +1186,19 @@ const getVotingPowerRefreshedTimestampSeconds = ({
// to avoid unnecessary notifications.
fullNeuron?.votingPowerRefreshedTimestampSeconds ?? BigInt(nowInSeconds());

export const secondsUntilLosingRewards = (neuron: NeuronInfo): number => {
const rewardLossStart =
Number(getVotingPowerRefreshedTimestampSeconds(neuron)) +
START_REDUCING_VOTING_POWER_AFTER_SECONDS;
return rewardLossStart - nowInSeconds();
};

export const isNeuronLosingRewards = (neuron: NeuronInfo): boolean =>
nowInSeconds() >=
getVotingPowerRefreshedTimestampSeconds(neuron) +
BigInt(START_REDUCING_VOTING_POWER_AFTER_SECONDS);
secondsUntilLosingRewards(neuron) <= 0;

// e.g. "Neuron will start losing rewards in 30 days"
export const shouldDisplayRewardLossNotification = (
neuron: NeuronInfo
): boolean =>
nowInSeconds() >=
getVotingPowerRefreshedTimestampSeconds(neuron) +
BigInt(START_REDUCING_VOTING_POWER_AFTER_SECONDS) -
BigInt(daysToSeconds(NOTIFICATION_PERIOD_BEFORE_REWARD_LOSS_STARTS_DAYS));
secondsUntilLosingRewards(neuron) <=
daysToSeconds(NOTIFICATION_PERIOD_BEFORE_REWARD_LOSS_STARTS_DAYS);
29 changes: 29 additions & 0 deletions frontend/src/tests/lib/utils/neuron.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ import {
neuronStakedMaturity,
neuronVotingPower,
neuronsVotingPower,
secondsUntilLosingRewards,
shouldDisplayRewardLossNotification,
sortNeuronsByStake,
sortNeuronsByVotingPowerRefreshedTimeout,
Expand Down Expand Up @@ -3294,6 +3295,34 @@ describe("neuron-utils", () => {
const losingRewardsPeriod = SECONDS_IN_HALF_YEAR;
const notificationPeriod = 30 * SECONDS_IN_DAY;

describe("secondsUntilLosingRewards", () => {
it("should return future date when no fullNeuron", () => {
expect(
secondsUntilLosingRewards({
...mockNeuron,
fullNeuron: undefined,
})
).toEqual(SECONDS_IN_HALF_YEAR);
});

it("should return seconds until losing rewards", () => {
expect(
secondsUntilLosingRewards(
neuronWithRefreshedTimestamp({
votingPowerRefreshedTimestampAgeSecs: 0,
})
)
).toBe(SECONDS_IN_HALF_YEAR);
expect(
secondsUntilLosingRewards(
neuronWithRefreshedTimestamp({
votingPowerRefreshedTimestampAgeSecs: losingRewardsPeriod,
})
)
).toBe(0);
});
});

describe("isNeuronLosingRewards", () => {
it("should return false by default", () => {
expect(
Expand Down

0 comments on commit 558bd78

Please sign in to comment.