-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7f894a5
commit 1bde4f6
Showing
3 changed files
with
133 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,4 +206,4 @@ describe('PnlTicks store', () => { | |
]); | ||
return createdTicks; | ||
} | ||
}); | ||
}); |
131 changes: 131 additions & 0 deletions
131
indexer/services/roundtable/__tests__/tasks/refresh-vault-pnl.test.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,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<PnlTicksFromDatabase> { | ||
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; | ||
} | ||
}); | ||
|
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 |
---|---|---|
|
@@ -42,4 +42,4 @@ export default async function runTask(): Promise<void> { | |
Date.now() - refreshDailyStart, | ||
); | ||
} | ||
} | ||
} |