Skip to content

Commit

Permalink
[CT-1307] Make pnl ticks computation consistent within a transaction. (
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwschau authored Oct 30, 2024
1 parent 2198a3b commit f6101a3
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ describe('pnl-ticks-helper', () => {

it('getNewPnlTicks with prior pnl ticks', async () => {
config.PNL_TICK_UPDATE_INTERVAL_MS = 3_600_000;
const blockHeight: string = '5';
const blockTime: IsoString = DateTime.utc(2022, 6, 2, 0, 30).toISO();
const ticksForSubaccounts: PnlTickForSubaccounts = {
[testConstants.defaultSubaccountId]: {
...testConstants.defaultPnlTick,
Expand All @@ -487,8 +489,6 @@ describe('pnl-ticks-helper', () => {
ticksForSubaccounts,
redisClient,
);
const blockHeight: string = '5';
const blockTime: IsoString = DateTime.utc(2022, 6, 2, 0, 30).toISO();
await BlockTable.create({
blockHeight,
time: blockTime,
Expand All @@ -497,7 +497,7 @@ describe('pnl-ticks-helper', () => {
const txId: number = await Transaction.start();
jest.spyOn(DateTime, 'utc').mockImplementation(() => dateTime);
const newTicksToCreate: PnlTicksCreateObject[] = await
getPnlTicksCreateObjects(blockHeight, blockTime, txId);
getPnlTicksCreateObjects(txId);
await Transaction.rollback(txId);
expect(newTicksToCreate.length).toEqual(1);
expect(newTicksToCreate).toEqual(
Expand All @@ -517,12 +517,16 @@ describe('pnl-ticks-helper', () => {

it('getNewPnlTicks without prior pnl ticks', async () => {
jest.spyOn(DateTime, 'utc').mockImplementation(() => dateTime);
await TransferTable.create(testConstants.defaultTransfer);
const txId: number = await Transaction.start();
const blockHeight: string = '5';
const blockTime: IsoString = DateTime.utc(2022, 6, 2, 0, 30).toISO();
await TransferTable.create(testConstants.defaultTransfer);
await BlockTable.create({
blockHeight,
time: blockTime,
});
const txId: number = await Transaction.start();
const newTicksToCreate: PnlTicksCreateObject[] = await
getPnlTicksCreateObjects(blockHeight, blockTime, txId);
getPnlTicksCreateObjects(txId);
await Transaction.rollback(txId);
expect(newTicksToCreate.length).toEqual(2);
expect(newTicksToCreate).toEqual(
Expand Down
10 changes: 8 additions & 2 deletions indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { logger, stats } from '@dydxprotocol-indexer/base';
import {
AssetPositionTable,
BlockFromDatabase,
BlockTable,
FundingIndexMap,
FundingIndexUpdatesTable,
helpers,
Expand Down Expand Up @@ -50,11 +52,15 @@ export function normalizeStartTime(
* @param blockHeight: consider transfers up until this block height.
*/
export async function getPnlTicksCreateObjects(
blockHeight: string,
blockTime: IsoString,
txId: number,
): Promise<PnlTicksCreateObject[]> {
const startGetPnlTicksCreateObjects: number = Date.now();
const latestBlock: BlockFromDatabase = await BlockTable.getLatest({
readReplica: true,
txId,
});
const blockHeight: string = latestBlock.blockHeight;
const blockTime: string = latestBlock.time;
const pnlTicksToBeCreatedAt: DateTime = DateTime.utc();
const [
mostRecentPnlTicks,
Expand Down
2 changes: 1 addition & 1 deletion indexer/services/roundtable/src/tasks/create-pnl-ticks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default async function runTask(): Promise<void> {
let newTicksToCreate: PnlTicksCreateObject[] = [];
try {
await perpetualMarketRefresher.updatePerpetualMarkets();
newTicksToCreate = await getPnlTicksCreateObjects(latestBlockHeight, latestBlockTime, txId);
newTicksToCreate = await getPnlTicksCreateObjects(txId);
} catch (error) {
logger.error({
at: 'create-pnl-ticks#runTask',
Expand Down

0 comments on commit f6101a3

Please sign in to comment.