Skip to content

Commit

Permalink
fix occasional hr skips in pnl computation
Browse files Browse the repository at this point in the history
  • Loading branch information
dydxwill committed Jan 10, 2024
1 parent 4f2d72a commit 39a35f0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getNewPnlTick,
getPnlTicksCreateObjects,
getUsdcTransfersSinceLastPnlTick,
getAccountsToUpdate,
} from '../../src/helpers/pnl-ticks-helper';
import { defaultPnlTickForSubaccounts } from '../../src/helpers/constants';
import Big from 'big.js';
Expand All @@ -35,6 +36,7 @@ import { ZERO } from '../../src/lib/constants';
import { SubaccountUsdcTransferMap } from '../../src/helpers/types';
import config from '../../src/config';
import _ from 'lodash';
import { ONE_HOUR_IN_MILLISECONDS } from '@dydxprotocol-indexer/base';

describe('pnl-ticks-helper', () => {
const positions: PerpetualPositionFromDatabase[] = [
Expand Down Expand Up @@ -228,6 +230,25 @@ describe('pnl-ticks-helper', () => {
}));
});

it('getAccountsToUpdate', () => {
const accountToLastUpdatedBlockTime: _.Dictionary<IsoString> = {
account1: '2024-01-01T10:00:00Z',
account2: '2024-01-01T11:00:00Z',
account3: '2024-01-01T11:01:00Z',
account4: '2024-01-01T11:10:00Z',
account5: '2024-01-01T12:00:00Z',
};
const blockTime: IsoString = '2024-01-01T12:00:00Z';
config.PNL_TICK_UPDATE_INTERVAL_MS = ONE_HOUR_IN_MILLISECONDS;

const expectedAccountsToUpdate: string[] = ['account1', 'account2', 'account3'];
const accountsToUpdate: string[] = getAccountsToUpdate(
accountToLastUpdatedBlockTime,
blockTime,
);
expect(accountsToUpdate).toEqual(expectedAccountsToUpdate);
});

it('calculateEquity', () => {
const usdcPosition: Big = new Big('100');
const equity: Big = calculateEquity(
Expand Down
34 changes: 26 additions & 8 deletions indexer/services/roundtable/src/helpers/pnl-ticks-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AssetPositionTable,
FundingIndexMap,
FundingIndexUpdatesTable,
helpers,
IsoString,
OraclePriceTable,
PerpetualPositionFromDatabase,
Expand All @@ -15,7 +16,6 @@ import {
SubaccountTable,
SubaccountToPerpetualPositionsMap,
TransferTable,
helpers,
} from '@dydxprotocol-indexer/postgres';
import { LatestAccountPnlTicksCache, PnlTickForSubaccounts } from '@dydxprotocol-indexer/redis';
import Big from 'big.js';
Expand Down Expand Up @@ -62,13 +62,7 @@ export async function getPnlTicksCreateObjects(
);
// get accounts to update based on last updated block height
const accountsToUpdate: string[] = [
..._.keys(accountToLastUpdatedBlockTime).filter(
(accountId) => {
const lastUpdatedBlockTime: string = accountToLastUpdatedBlockTime[accountId];
return new Date(blockTime).getTime() - new Date(lastUpdatedBlockTime).getTime() >=
config.PNL_TICK_UPDATE_INTERVAL_MS;
},
),
...getAccountsToUpdate(accountToLastUpdatedBlockTime, blockTime),
...newSubaccountIds,
];
stats.gauge(
Expand Down Expand Up @@ -166,6 +160,30 @@ export async function getPnlTicksCreateObjects(
return newTicksToCreate;
}

/**
* Gets a list of subaccounts that were last updated more than 0.9 *
* PNL_TICK_UPDATE_INTERVAL_MS ago.
*
* @param mostRecentPnlTicks
* @param blockTime
*/
export function getAccountsToUpdate(
accountToLastUpdatedBlockTime: _.Dictionary<IsoString>,
blockTime: IsoString,
): string[] {
// get accounts to update based on last updated block time
const accountsToUpdate: string[] = [
..._.keys(accountToLastUpdatedBlockTime).filter(
(accountId) => {
const lastUpdatedBlockTime: string = accountToLastUpdatedBlockTime[accountId];
return new Date(blockTime).getTime() - new Date(lastUpdatedBlockTime).getTime() >=
0.9 * config.PNL_TICK_UPDATE_INTERVAL_MS;
},
),
];
return accountsToUpdate;
}

/**
* Get a map of block height to funding index state.
* Funding index state represents the most recent funding index value for every perpetual market.
Expand Down

0 comments on commit 39a35f0

Please sign in to comment.