Skip to content

Commit

Permalink
refactor: split up and test a couple more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandermendes committed Nov 14, 2024
1 parent ecd6041 commit d7874b5
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
getIsPaused,
getPendingWithdrawals,
getStreamedAmounts,
getStreamsProgress,
getStreamsSchedule,
getTotalShares,
getTotalStaked,
Expand All @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions src/utils/progress.ts
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 2 additions & 6 deletions src/utils/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 0 additions & 14 deletions src/utils/staking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/streams-schedule.ts
Original file line number Diff line number Diff line change
@@ -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'),
Expand Down
12 changes: 12 additions & 0 deletions tests/specs/utils/__snapshots__/progress.test.ts.snap
Original file line number Diff line number Diff line change
@@ -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,
]
`;
12 changes: 12 additions & 0 deletions tests/specs/utils/progress.test.ts
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
33 changes: 33 additions & 0 deletions tests/specs/utils/schedule.test.ts
Original file line number Diff line number Diff line change
@@ -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,
});
});
});
});
2 changes: 1 addition & 1 deletion tests/specs/utils/supply.test.ts
Original file line number Diff line number Diff line change
@@ -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({
Expand Down

0 comments on commit d7874b5

Please sign in to comment.