From bb9243e898062931e4faed267ca4923abb5cf482 Mon Sep 17 00:00:00 2001 From: Vincent Chau <99756290+vincentwschau@users.noreply.github.com> Date: Tue, 29 Oct 2024 11:20:47 -0400 Subject: [PATCH] Make pnl ticks computation consistent within a transaction. --- .../__tests__/helpers/pnl-ticks-helper.test.ts | 16 ++++++++++------ .../roundtable/src/helpers/pnl-ticks-helper.ts | 10 ++++++++-- .../roundtable/src/tasks/create-pnl-ticks.ts | 2 +- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts index c60cca50bc..e962c0f077 100644 --- a/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts +++ b/indexer/services/roundtable/__tests__/helpers/pnl-ticks-helper.test.ts @@ -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, @@ -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, @@ -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( @@ -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( diff --git a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts index ee0f6d5970..c899082bfc 100644 --- a/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts +++ b/indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts @@ -1,6 +1,8 @@ import { logger, stats } from '@dydxprotocol-indexer/base'; import { AssetPositionTable, + BlockFromDatabase, + BlockTable, FundingIndexMap, FundingIndexUpdatesTable, helpers, @@ -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 { 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, diff --git a/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts b/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts index 36d7f40698..fde4918a18 100644 --- a/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts +++ b/indexer/services/roundtable/src/tasks/create-pnl-ticks.ts @@ -55,7 +55,7 @@ export default async function runTask(): Promise { 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',