-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
TOK-512: last cycle rewards timestamps (#433)
* refactor: last cycle rewards timestamps * refactor: pr comments
- Loading branch information
1 parent
0be8800
commit 85854e0
Showing
13 changed files
with
247 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/app/collective-rewards/rewards/hooks/useGetLastCycleDistribution.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
useGetRewardDistributionFinishedLogs, | ||
useGetLastCycleDistribution, | ||
RewardDistributionFinishedEventLog, | ||
} from '@/app/collective-rewards/rewards' | ||
import { describe, expect, it, vi } from 'vitest' | ||
import { Cycle } from '@/app/collective-rewards/metrics' | ||
import { DateTime, Duration } from 'luxon' | ||
|
||
vi.mock('@/app/collective-rewards/rewards/hooks/useGetRewardDistributionFinishedLogs', () => { | ||
return { | ||
useGetRewardDistributionFinishedLogs: vi.fn(), | ||
} | ||
}) | ||
|
||
describe('useGetLastCycleRewardsTimestamps', () => { | ||
type Log = RewardDistributionFinishedEventLog[number] & { timeStamp: number } | ||
const startTimestamp = 1733011200 // 2024-12-01 00:00:00 | ||
const duration = 1209600 // 14 days | ||
const distributionWindow = 3600 // 1 hour | ||
const cycle: Cycle = { | ||
cycleStart: DateTime.fromSeconds(startTimestamp), | ||
cycleNext: DateTime.fromSeconds(startTimestamp + duration), | ||
cycleDuration: Duration.fromObject({ seconds: duration }), | ||
fistCycleStart: DateTime.fromSeconds(startTimestamp), | ||
endDistributionWindow: DateTime.fromSeconds(distributionWindow), | ||
} | ||
const { cycleStart, cycleNext, cycleDuration } = cycle | ||
|
||
it('should return (from,to) = to next cycle start if there are no events', () => { | ||
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => { | ||
return { | ||
data: [], | ||
isLoading: false, | ||
error: null, | ||
} | ||
}) | ||
|
||
const { data } = useGetLastCycleDistribution(cycle) | ||
|
||
expect(data).toEqual({ | ||
fromTimestamp: cycleNext.toSeconds(), | ||
toTimestamp: cycleNext.toSeconds(), | ||
}) | ||
}) | ||
|
||
it('should return (from,to) = to next cycle start if there were not distributions in the current cycle', () => { | ||
const lastCycleStart = cycle.cycleStart.minus({ seconds: cycleDuration.as('seconds') }) | ||
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => { | ||
return { | ||
data: [ | ||
{ | ||
timeStamp: lastCycleStart.toSeconds(), | ||
}, | ||
] as Log[], | ||
isLoading: false, | ||
error: null, | ||
} | ||
}) | ||
|
||
const { data } = useGetLastCycleDistribution(cycle) | ||
|
||
expect(data).toEqual({ | ||
fromTimestamp: cycleNext.toSeconds(), | ||
toTimestamp: cycleNext.toSeconds(), | ||
}) | ||
}) | ||
|
||
it('should return (from = current cycle start, to = last event) if there were distributions in the current cycle', () => { | ||
const endDistributionTime = cycleStart.plus({ seconds: 100 }) | ||
vi.mocked(useGetRewardDistributionFinishedLogs).mockImplementation(() => { | ||
return { | ||
data: [ | ||
{ | ||
timeStamp: endDistributionTime.toSeconds(), | ||
}, | ||
] as Log[], | ||
isLoading: false, | ||
error: null, | ||
} | ||
}) | ||
|
||
const { data } = useGetLastCycleDistribution(cycle) | ||
|
||
expect(data).toEqual({ | ||
fromTimestamp: cycleStart.toSeconds(), | ||
toTimestamp: endDistributionTime.toSeconds(), | ||
}) | ||
}) | ||
}) |
29 changes: 29 additions & 0 deletions
29
src/app/collective-rewards/rewards/hooks/useGetLastCycleDistribution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Cycle } from '@/app/collective-rewards/metrics' | ||
import { | ||
RewardDistributionFinishedEventLog, | ||
useGetRewardDistributionFinishedLogs, | ||
} from '@/app/collective-rewards/rewards' | ||
|
||
type Log = RewardDistributionFinishedEventLog[number] & { timeStamp: number } | ||
export const useGetLastCycleDistribution = ({ cycleStart, cycleNext }: Cycle) => { | ||
const { data: rewardDistributionFinished, isLoading, error } = useGetRewardDistributionFinishedLogs() | ||
|
||
const [lastEvent] = rewardDistributionFinished.slice(-1) as Log[] | ||
|
||
let fromTimestamp = cycleNext.toSeconds() | ||
let toTimestamp = cycleNext.toSeconds() | ||
|
||
if (lastEvent && lastEvent.timeStamp >= cycleStart.toSeconds()) { | ||
fromTimestamp = cycleStart.toSeconds() | ||
toTimestamp = lastEvent.timeStamp | ||
} | ||
|
||
return { | ||
data: { | ||
fromTimestamp, | ||
toTimestamp, | ||
}, | ||
isLoading, | ||
error, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.