From 1bde4f6d6ba2ed155c73a5c34df5a744d54e4a3f Mon Sep 17 00:00:00 2001 From: Vincent Chau <99756290+vincentwschau@users.noreply.github.com> Date: Fri, 22 Nov 2024 13:47:43 -0500 Subject: [PATCH] New lines, add missing test. --- .../stores/vault-pnl-ticks-view.test.ts | 2 +- .../__tests__/tasks/refresh-vault-pnl.test.ts | 131 ++++++++++++++++++ .../roundtable/src/tasks/refresh-vault-pnl.ts | 2 +- 3 files changed, 133 insertions(+), 2 deletions(-) create mode 100644 indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts diff --git a/indexer/packages/postgres/__tests__/stores/vault-pnl-ticks-view.test.ts b/indexer/packages/postgres/__tests__/stores/vault-pnl-ticks-view.test.ts index 926c10d917..eaf69d671f 100644 --- a/indexer/packages/postgres/__tests__/stores/vault-pnl-ticks-view.test.ts +++ b/indexer/packages/postgres/__tests__/stores/vault-pnl-ticks-view.test.ts @@ -206,4 +206,4 @@ describe('PnlTicks store', () => { ]); return createdTicks; } -}); \ No newline at end of file +}); diff --git a/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts b/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts new file mode 100644 index 0000000000..595bef1d6d --- /dev/null +++ b/indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.ts @@ -0,0 +1,131 @@ +import config from '../../src/config'; +import refreshVaulPnlTask from '../../src/tasks/refresh-vault-pnl'; +import { Settings, DateTime } from 'luxon'; +import { + BlockTable, + PnlTickInterval, + PnlTicksFromDatabase, + PnlTicksTable, + VaultPnlTicksView, + VaultTable, + dbHelpers, + testConstants, + testMocks, +} from '@dydxprotocol-indexer/postgres'; + +jest.mock('../../src/helpers/aws'); + +describe('refresh-vault-pnl', () => { + const currentTime: DateTime = DateTime.utc(); + + beforeAll(async () => { + await dbHelpers.migrate(); + await dbHelpers.clearData(); + }); + + beforeEach(async () => { + await testMocks.seedData(); + await Promise.all([ + VaultTable.create({ + ...testConstants.defaultVault, + address: testConstants.defaultSubaccount.address, + }), + ]); + }); + + afterAll(async () => { + await dbHelpers.teardown(); + jest.resetAllMocks(); + }); + + afterEach(async () => { + await dbHelpers.clearData(); + await VaultPnlTicksView.refreshDailyView(); + await VaultPnlTicksView.refreshHourlyView(); + jest.clearAllMocks(); + Settings.now = () => new Date().valueOf(); + }); + + it('refreshes hourly view if within time window of an hour', async() => { + Settings.now = () => currentTime.startOf('hour').plus( + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS - 1}, + ).valueOf() + const pnlTick: PnlTicksFromDatabase = await setupPnlTick(); + await refreshVaulPnlTask(); + + const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( + PnlTickInterval.hour, + 86400, + currentTime.minus({ day: 1}), + ); + expect(pnlTicks).toEqual([pnlTick]); + }); + + it('refreshes daily view if within time window of a day', async() => { + Settings.now = () => currentTime.startOf('day').plus( + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS - 1 }, + ).valueOf() + const pnlTick: PnlTicksFromDatabase = await setupPnlTick(); + await refreshVaulPnlTask(); + + const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( + PnlTickInterval.day, + 608400, + currentTime.minus({ day: 7}), + ); + expect(pnlTicks).toEqual([pnlTick]); + }); + + it('does not refresh hourly view if outside of time window of an hour', async() => { + Settings.now = () => currentTime.startOf('hour').plus( + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS + 1}, + ).valueOf() + await setupPnlTick(); + await refreshVaulPnlTask(); + + const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( + PnlTickInterval.hour, + 86400, + currentTime.minus({ day: 1}), + ); + expect(pnlTicks).toEqual([]); + }); + + it('does not refresh hourly view if outside time window of a day', async() => { + Settings.now = () => currentTime.startOf('day').plus( + { milliseconds: config.TIME_WINDOW_FOR_REFRESH_MS + 1}, + ).valueOf() + await setupPnlTick(); + await refreshVaulPnlTask(); + + const pnlTicks: PnlTicksFromDatabase[] = await VaultPnlTicksView.getVaultsPnl( + PnlTickInterval.day, + 608400, + currentTime.minus({ day: 7}), + ); + expect(pnlTicks).toEqual([]); + }); + + async function setupPnlTick(): Promise { + const twoHoursAgo: string = currentTime.minus({ hour: 2 }).toISO(); + await Promise.all([ + BlockTable.create({ + blockHeight: '6', + time: twoHoursAgo, + }), + ]); + const createdTick: PnlTicksFromDatabase = await PnlTicksTable.create( + { + subaccountId: testConstants.defaultSubaccountId, + equity: '1080', + createdAt: twoHoursAgo, + totalPnl: '1180', + netTransfers: '50', + blockHeight: '6', + blockTime: twoHoursAgo, + }, + ); + return createdTick; + } +}); + diff --git a/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts b/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts index 8a63b93f69..b74b08e5ec 100644 --- a/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts +++ b/indexer/services/roundtable/src/tasks/refresh-vault-pnl.ts @@ -42,4 +42,4 @@ export default async function runTask(): Promise { Date.now() - refreshDailyStart, ); } -} \ No newline at end of file +}