From d7874b5a463725ffe4633b49c8857b760c5c4b61 Mon Sep 17 00:00:00 2001 From: alexandermendes Date: Thu, 14 Nov 2024 22:55:02 +0000 Subject: [PATCH] refactor: split up and test a couple more functions --- src/provider.tsx | 2 +- src/utils/progress.ts | 16 +++++++++ src/utils/schedule.ts | 8 ++--- src/utils/staking.ts | 14 -------- tests/fixtures/streams-schedule.ts | 3 +- .../utils/__snapshots__/progress.test.ts.snap | 12 +++++++ tests/specs/utils/progress.test.ts | 12 +++++++ tests/specs/utils/schedule.test.ts | 33 +++++++++++++++++++ tests/specs/utils/supply.test.ts | 2 +- 9 files changed, 79 insertions(+), 23 deletions(-) create mode 100644 src/utils/progress.ts create mode 100644 tests/specs/utils/__snapshots__/progress.test.ts.snap create mode 100644 tests/specs/utils/progress.test.ts create mode 100644 tests/specs/utils/schedule.test.ts diff --git a/src/provider.tsx b/src/provider.tsx index 4ac11a6..8c91d1c 100644 --- a/src/provider.tsx +++ b/src/provider.tsx @@ -17,7 +17,6 @@ import { getIsPaused, getPendingWithdrawals, getStreamedAmounts, - getStreamsProgress, getStreamsSchedule, getTotalShares, getTotalStaked, @@ -40,6 +39,7 @@ import { AuroraNetwork } from './types/network.js'; import { config } from './config.js'; import { logger } from './logger.js'; import { calculateStakedPctOfSupply } from './utils/supply.js'; +import { getStreamsProgress } from './utils/progress.js'; type StakingProviderProps = { network: AuroraNetwork; diff --git a/src/utils/progress.ts b/src/utils/progress.ts new file mode 100644 index 0000000..13a1206 --- /dev/null +++ b/src/utils/progress.ts @@ -0,0 +1,16 @@ +import { StreamSchedule } from '../types/stream.js'; +import { getScheduleStartAndEndTimes } from './schedule.js'; + +export const getStreamsProgress = ( + streamsSchedule: StreamSchedule[], +): number[] => { + const streamsProgress = streamsSchedule.map((schedule) => { + const { startTime, endTime } = getScheduleStartAndEndTimes(schedule); + + const progress = ((Date.now() - startTime) / (endTime - startTime)) * 100; + + return progress > 10 ? progress : 10; + }); + + return streamsProgress; +}; diff --git a/src/utils/schedule.ts b/src/utils/schedule.ts index 6a761ff..2760633 100644 --- a/src/utils/schedule.ts +++ b/src/utils/schedule.ts @@ -5,12 +5,8 @@ export const getScheduleStartAndEndTimes = (schedule: StreamSchedule) => { const startTime = schedule.scheduleTimes[0]; const endTime = schedule.scheduleTimes[schedule.scheduleTimes.length - 1]; - if (!isDefined(startTime)) { - throw new Error('Invalid schedule: start time not found'); - } - - if (!isDefined(endTime)) { - throw new Error('Invalid schedule: end time not found'); + if (!isDefined(startTime) || !isDefined(endTime)) { + throw new Error('Invalid schedule: no schedule times found'); } return { diff --git a/src/utils/staking.ts b/src/utils/staking.ts index cf462c7..62b91f2 100644 --- a/src/utils/staking.ts +++ b/src/utils/staking.ts @@ -195,20 +195,6 @@ export const getStreamsSchedule = async ( }); }; -export const getStreamsProgress = ( - streamsSchedule: StreamSchedule[], -): number[] => { - const streamsProgress = streamsSchedule.map((schedule) => { - const { startTime, endTime } = getScheduleStartAndEndTimes(schedule); - - const progress = ((Date.now() - startTime) / (endTime - startTime)) * 100; - - return progress > 10 ? progress : 10; - }); - - return streamsProgress; -}; - export const getVoteSupply = (voteSchedule: StreamSchedule): BigNumber => { const { startTime, endTime } = getScheduleStartAndEndTimes(voteSchedule); diff --git a/tests/fixtures/streams-schedule.ts b/tests/fixtures/streams-schedule.ts index 90b4cf5..bd9aa7b 100644 --- a/tests/fixtures/streams-schedule.ts +++ b/tests/fixtures/streams-schedule.ts @@ -1,7 +1,8 @@ import { BigNumber } from 'ethers'; +import { StreamSchedule } from '../../src/types/stream'; // A snapshot of data logged from Aurora Plus -export const streamsSchedule = [ +export const streamsSchedule: StreamSchedule[] = [ { scheduleTimes: [ BigNumber.from('0x6283b870'), diff --git a/tests/specs/utils/__snapshots__/progress.test.ts.snap b/tests/specs/utils/__snapshots__/progress.test.ts.snap new file mode 100644 index 0000000..7b380a4 --- /dev/null +++ b/tests/specs/utils/__snapshots__/progress.test.ts.snap @@ -0,0 +1,12 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`Progress getStreamsProgress returns the progress of each stream 1`] = ` +[ + 49.82118399026023, + 248.95437262357416, + 248.95437262357416, + 248.95437262357416, + 995.8174904942966, + 159.90424325672453, +] +`; diff --git a/tests/specs/utils/progress.test.ts b/tests/specs/utils/progress.test.ts new file mode 100644 index 0000000..49e81d7 --- /dev/null +++ b/tests/specs/utils/progress.test.ts @@ -0,0 +1,12 @@ +import { getStreamsProgress } from '../../../src/utils/progress'; +import { streamsSchedule } from '../../fixtures/streams-schedule'; + +jest.useFakeTimers().setSystemTime(new Date('2024-11-12T00:00:00Z')); + +describe('Progress', () => { + describe('getStreamsProgress', () => { + it('returns the progress of each stream', () => { + expect(getStreamsProgress(streamsSchedule)).toMatchSnapshot(); + }); + }); +}); diff --git a/tests/specs/utils/schedule.test.ts b/tests/specs/utils/schedule.test.ts new file mode 100644 index 0000000..8414595 --- /dev/null +++ b/tests/specs/utils/schedule.test.ts @@ -0,0 +1,33 @@ +import { BigNumber } from 'ethers'; +import { StreamSchedule } from '../../../src/types/stream'; +import { getScheduleStartAndEndTimes } from '../../../src/utils/schedule'; + +const startTime = new Date('2024-01-01T00:00:00Z').getTime(); +const endTime = new Date('2024-02-01T00:00:00Z').getTime(); + +describe('Schedule', () => { + describe('getScheduleStartAndEndTimes', () => { + it('throws if no times found', () => { + const schedule: StreamSchedule = { + scheduleTimes: [], + scheduleRewards: [], + }; + + expect(() => getScheduleStartAndEndTimes(schedule)).toThrow( + 'Invalid schedule: no schedule times found', + ); + }); + + it('returns the start and end times', () => { + const schedule: StreamSchedule = { + scheduleTimes: [BigNumber.from(startTime), BigNumber.from(endTime)], + scheduleRewards: [], + }; + + expect(getScheduleStartAndEndTimes(schedule)).toEqual({ + startTime: startTime * 1000, + endTime: endTime * 1000, + }); + }); + }); +}); diff --git a/tests/specs/utils/supply.test.ts b/tests/specs/utils/supply.test.ts index 8ce12e8..a7b1a0c 100644 --- a/tests/specs/utils/supply.test.ts +++ b/tests/specs/utils/supply.test.ts @@ -1,7 +1,7 @@ import { calculateStakedPctOfSupply } from '../../../src/utils/supply'; import { getNumberTo18Decimals } from '../../utils/get-number-to-18-decimals'; -describe('APR', () => { +describe('Supply', () => { describe('calculateStakedPctOfSupply', () => { it('returns the expected result with 1% staked', () => { const result = calculateStakedPctOfSupply({