Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CT-1307] Make pnl ticks computation consistent within a transaction. #2548

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
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;
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
const pnlTicksToBeCreatedAt: DateTime = DateTime.utc();
const [
mostRecentPnlTicks,
Expand Down
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
Loading