Skip to content
New issue

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

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

Already on GitHub? # to your account

[IND-557] fix occasional missing pnl ticks #951

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about instead of that we get the list of subaccounts that haven't been updated this hour? For example(all pseudocode):

const blockTime = new Date(blockTime);
const normalizedBlockTime = normalizeStartTime(new Date(blockTime)); // 12:00:01 -> 12:00:00
const lastUpdatedBlockTime = accountToLastUpdatedBlockTime[accountId];
const normalizedLastUpdatedBlockTime = normalizeStartTime(lastUpdatedBlockTime);

// process if normalizedBlockTime != normalizedLastUpdatedBlockTime

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's cleaner. done.

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