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

Get funding index maps for vault positions in chunks. #2525

Merged
merged 45 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
aaec10f
Revert "Add Orderbook Mid Price Cache (#2289)" (backport #2333) (#2334)
mergify[bot] Sep 24, 2024
ce8f484
Add Orderbook Mid Price Cache (backport #2338) (#2340)
mergify[bot] Sep 25, 2024
8a0a748
[OTE-784] Limit addresses for compliance check to dydx wallets with d…
mergify[bot] Sep 25, 2024
0f214f1
enable username generation roundtable (backport #2361) (#2362)
mergify[bot] Sep 26, 2024
a47c327
Add current equity as a pnl tick. (backport #2335) (#2365)
mergify[bot] Sep 26, 2024
9875783
Use vault table rather than placeholder config flags to fetch vaults.…
mergify[bot] Sep 26, 2024
d034716
Add sql script latency metrics (backport #2356) (#2371)
mergify[bot] Sep 26, 2024
8b933c6
Add oracle prices index on ("marketId", "effectiveAtHeight") (backpor…
mergify[bot] Sep 26, 2024
0502f73
Include getting main subaccount equity / pnl for megavault PnL query.…
mergify[bot] Sep 26, 2024
1389214
Add function to fetch availability zone id (#2326) (backport #2390) (…
mergify[bot] Sep 30, 2024
6dc0501
Create consumer using rack id (#2352) (backport #2393) (#2411)
mergify[bot] Sep 30, 2024
3e94eb4
Filter out to single tick per interval. (backport #2403) (#2418)
mergify[bot] Oct 1, 2024
6005a0d
[CT-629] Fix entryPrice calc (backport #2415) (#2417)
mergify[bot] Oct 1, 2024
e8af710
Revert "[CT-629] Fix entryPrice calc" (backport #2425) (#2426)
mergify[bot] Oct 1, 2024
dbe13a3
Fix flaky vault test. (backport #2422) (#2424)
mergify[bot] Oct 1, 2024
0b9e8a2
Split affiliate info fees by taker and maker (backport #2439) (#2448)
mergify[bot] Oct 3, 2024
c182cd9
Fix bug with PnL aggregation. (backport #2446) (#2451)
mergify[bot] Oct 3, 2024
939cb56
Get latest hourly tick to compute final tick for megavault PnL. (back…
mergify[bot] Oct 3, 2024
6800a36
add afflaiteReferredMakerRebates field to response (backport #2473) (…
mergify[bot] Oct 4, 2024
4b19a59
[OTE-846] Bazooka sequential clear (backport #2423) (#2477)
mergify[bot] Oct 7, 2024
ca6c044
[OTE-863] update username generation query (backport #2482) (#2483)
mergify[bot] Oct 14, 2024
071aa31
Improve vault endpoint performance. (backport #2475) (#2484)
mergify[bot] Oct 15, 2024
7a2200e
Return undefined from getOrderbookMidPriceMap (backport #2441) (#2486)
mergify[bot] Oct 15, 2024
54b3bce
[CT-629] Fix entryPrice calc (backport #2455) (#2496)
mergify[bot] Oct 16, 2024
666898f
Don't increment messageId for custom ping messages (backport #2493) (…
mergify[bot] Oct 16, 2024
5042763
[OTE-880] Emit log in case of collisions (backport #2500) (#2504)
mergify[bot] Oct 16, 2024
3339df5
[OTE-876] update roundtable loop timings for instrumentation and uncr…
mergify[bot] Oct 16, 2024
e2298c4
Remove orderbook cache roundtable job (backport #2510) (#2511)
mergify[bot] Oct 17, 2024
ec8b919
Add config var to exclude specific stateful order ids from being proc…
mergify[bot] Oct 18, 2024
bba2a12
add wallet when transfer to subaccount (backport #2519) (#2520)
mergify[bot] Oct 18, 2024
efe31e8
Chunk fetching of funding indices.
vincentwschau Oct 21, 2024
e96ded9
Filter out invalid vault subaccounts.
vincentwschau Oct 21, 2024
57c1395
instrument.
vincentwschau Oct 21, 2024
50d1bd3
Window funding index heights.
vincentwschau Oct 21, 2024
10b1b35
more logging.
vincentwschau Oct 21, 2024
f8ced66
Use chunked methods.
vincentwschau Oct 21, 2024
6497f7d
Filter out invalid vault subaccounts at source.
vincentwschau Oct 21, 2024
fea557c
Add metrics, clean up logging.
vincentwschau Oct 21, 2024
dfd868e
Merge branch 'main' into vincentc/chunk-vault-equity
vincentwschau Oct 21, 2024
afeb453
Remove branch from github workflow.
vincentwschau Oct 21, 2024
41fe266
Fix lint.
vincentwschau Oct 21, 2024
c3cd754
Remove unsafe delete.
vincentwschau Oct 22, 2024
72c423a
Fix tests.
vincentwschau Oct 22, 2024
60639d9
Parallelize fetching of funding index maps.
vincentwschau Oct 22, 2024
5de986d
Fix lint.
vincentwschau Oct 22, 2024
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 @@ -242,6 +242,7 @@ export async function findFundingIndexMaps(
.sort();
// Get the min height to limit the search to blocks 4 hours or before the min height.
const minHeight: number = heightNumbers[0];
const maxheight: number = heightNumbers[heightNumbers.length - 1];

const result: {
rows: FundingIndexUpdatesFromDatabaseWithSearchHeight[],
Expand All @@ -255,6 +256,7 @@ export async function findFundingIndexMaps(
unnest(ARRAY[${heightNumbers.join(',')}]) AS "searchHeight"
WHERE
"effectiveAtHeight" > ${Big(minHeight).minus(FOUR_HOUR_OF_BLOCKS).toFixed()} AND
"effectiveAtHeight" < ${Big(maxheight)} AND
"effectiveAtHeight" <= "searchHeight"
ORDER BY
"perpetualId",
Expand Down
1 change: 1 addition & 0 deletions indexer/services/comlink/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export const configSchema = {
VAULT_PNL_HISTORY_DAYS: parseInteger({ default: 90 }),
VAULT_PNL_HISTORY_HOURS: parseInteger({ default: 72 }),
VAULT_LATEST_PNL_TICK_WINDOW_HOURS: parseInteger({ default: 1 }),
VAULT_FETCH_FUNDING_INDEX_BLOCK_WINDOWS: parseInteger({default: 250_000 }),
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stats } from '@dydxprotocol-indexer/base';
import { logger, stats } from '@dydxprotocol-indexer/base';
import {
PnlTicksFromDatabase,
PnlTicksTable,
Expand Down Expand Up @@ -71,7 +71,14 @@ class VaultController extends Controller {
async getMegavaultHistoricalPnl(
@Query() resolution?: PnlTickInterval,
): Promise<MegavaultHistoricalPnlResponse> {
const start: number = Date.now();
const vaultSubaccounts: VaultMapping = await getVaultMapping();
stats.timing(
`${config.SERVICE_NAME}.${controllerName}.fetch_vaults.timing`,
Date.now() - start,
);

vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
const startTicksPositions: number = Date.now();
const vaultSubaccountIdsWithMainSubaccount: string[] = _
.keys(vaultSubaccounts)
.concat([MEGAVAULT_SUBACCOUNT_ID]);
Expand All @@ -94,6 +101,10 @@ class VaultController extends Controller {
getMainSubaccountEquity(),
getLatestPnlTick(vaultSubaccountIdsWithMainSubaccount),
]);
stats.timing(
`${config.SERVICE_NAME}.${controllerName}.fetch_ticks_positions_equity.timing`,
Date.now() - startTicksPositions,
);

vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
// aggregate pnlTicks for all vault subaccounts grouped by blockHeight
const aggregatedPnlTicks: PnlTicksFromDatabase[] = aggregateHourlyPnlTicks(vaultPnlTicks);
Expand Down Expand Up @@ -324,6 +335,7 @@ async function getVaultSubaccountPnlTicks(
async function getVaultPositions(
vaultSubaccounts: VaultMapping,
): Promise<Map<string, VaultPosition>> {
const start: number = Date.now();
const vaultSubaccountIds: string[] = _.keys(vaultSubaccounts);
if (vaultSubaccountIds.length === 0) {
return new Map();
Expand Down Expand Up @@ -374,7 +386,12 @@ async function getVaultPositions(
),
BlockTable.getLatest(),
]);
stats.timing(
`${config.SERVICE_NAME}.${controllerName}.positions.fetch_subaccounts_positions.timing`,
Date.now() - start,
);

vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
const startFunding: number = Date.now();
const updatedAtHeights: string[] = _(subaccounts).map('updatedAtHeight').uniq().value();
const [
latestFundingIndexMap,
Expand All @@ -387,11 +404,13 @@ async function getVaultPositions(
.findFundingIndexMap(
latestBlock.blockHeight,
),
FundingIndexUpdatesTable
.findFundingIndexMaps(
updatedAtHeights,
),
getFundingIndexMapsChunked(updatedAtHeights),
]);
stats.timing(
`${config.SERVICE_NAME}.${controllerName}.positions.fetch_funding.timing`,
Date.now() - startFunding,
);

const assetPositionsBySubaccount:
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved
{ [subaccountId: string]: AssetPositionFromDatabase[] } = _.groupBy(
assetPositions,
Expand Down Expand Up @@ -557,20 +576,87 @@ function getResolution(resolution: PnlTickInterval = PnlTickInterval.day): PnlTi
return resolution;
}

/**
* Gets funding index maps in a chunked fashion to reduce database load and aggregates into a
* a map of funding index maps.
* @param updatedAtHeights
* @returns
*/
async function getFundingIndexMapsChunked(
updatedAtHeights: string[]
): Promise<{[blockHeight: string]: FundingIndexMap}> {
const updatedAtHeightsNum: number[] = updatedAtHeights.map((height: string): number => {
return parseInt(height, 10);
}).sort();
const aggregateFundingIndexMaps: {[blockHeight: string]: FundingIndexMap} = {};
for(const chunk of getHeightWindows(updatedAtHeightsNum)) {
const fundingIndexMaps: {[blockHeight: string]: FundingIndexMap} = await FundingIndexUpdatesTable.findFundingIndexMaps(
chunk.map((heightNum: number): string => { return heightNum.toString() }),
);
for (const height of _.keys(fundingIndexMaps)) {
aggregateFundingIndexMaps[height] = fundingIndexMaps[height];
}
}
return aggregateFundingIndexMaps;
}

/**
* Separates an array of heights into a chunks based on a window size. Each chunk should only
* contain heights within a certain number of blocks of each other.
* @param heights
* @returns
*/
function getHeightWindows(
heights: number[],
): number[][] {
if (heights.length == 0) {
return [];
}
const windows: number[][] = [];
let windowStart: number = heights[0];
let currentWindow: number[] = [];
for(const height of heights) {
if (height - windowStart < config.VAULT_FETCH_FUNDING_INDEX_BLOCK_WINDOWS) {
currentWindow.push(height);
} else {
windows.push(currentWindow);
currentWindow = [height];
windowStart = height;
}
}
windows.push(currentWindow);
return windows;
}

async function getVaultMapping(): Promise<VaultMapping> {
const vaults: VaultFromDatabase[] = await VaultTable.findAll(
{},
[],
{},
);
return _.zipObject(
const vaultMapping: VaultMapping = _.zipObject(
vaults.map((vault: VaultFromDatabase): string => {
return SubaccountTable.uuid(vault.address, 0);
}),
vaults.map((vault: VaultFromDatabase): string => {
return vault.clobPairId;
}),
);
for (const subaccountId of _.keys(vaultMapping)) {
const perpetual: PerpetualMarketFromDatabase | undefined = perpetualMarketRefresher.getPerpetualMarketFromClobPairId(
vaultMapping[subaccountId],
);
if (perpetual === undefined) {
logger.warning({
at: 'VaultController#getVaultPositions',
message: `Vault clob pair id ${vaultMapping[subaccountId]} does not correspond to a ` +
'perpetual market.',
subaccountId,
});
delete vaultMapping[subaccountId];
}
}
return vaultMapping;
}
vincentwschau marked this conversation as resolved.
Show resolved Hide resolved

export default router;
Loading