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

De-duplicate and filter out invalid pnl ticks for megavault. #2540

Merged
merged 42 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 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
daddb13
update migration to stop excessive consumption of computation (backpo…
mergify[bot] Oct 18, 2024
bb809c2
Get funding index maps for vault positions in chunks. (backport #2525…
mergify[bot] Oct 22, 2024
32cbd9d
Vulcan topic to 210 partitions (backport #2528) (#2530)
mergify[bot] Oct 22, 2024
38a2d36
Fix typo and add test for invalid vaults. (backport #2535) (#2536)
mergify[bot] Oct 22, 2024
a207ab1
De-dupe and filter out aggregated ticks with missing data.
vincentwschau Oct 25, 2024
d72898d
Add tests.
vincentwschau Oct 25, 2024
4b12b78
Merge branch 'main' into vincentc/dedup-filter-ticks
vincentwschau Oct 25, 2024
7af7388
Update gh workflow.
vincentwschau Oct 25, 2024
eefff3e
Logging.
vincentwschau Oct 25, 2024
18f5b73
Off-by-one
vincentwschau Oct 25, 2024
8ee7ab5
Add correct logic to account for main vault subaccount.
vincentwschau Oct 26, 2024
0a9cdcf
Remove gh workflow changes.
vincentwschau Oct 28, 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
6 changes: 6 additions & 0 deletions indexer/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
VaultTable,
MEGAVAULT_MODULE_ADDRESS,
MEGAVAULT_SUBACCOUNT_ID,
TransferTable,
} from '@dydxprotocol-indexer/postgres';
import { RequestMethod, VaultHistoricalPnl } from '../../../../src/types';
import request from 'supertest';
Expand Down Expand Up @@ -181,11 +182,12 @@ describe('vault-controller#V4', () => {
[
'hourly resolution',
'?resolution=hour',
[1, undefined, 2, 3, 4],
[undefined, 6, 7, 8, 9],
[11, undefined, 12, 13, 14],
[1, 2, 3, 4],
[undefined, 7, 8, 9],
[11, 12, 13, 14],
],
])('Get /megavault/historicalPnl with 2 vault subaccounts and main subaccount (%s)', async (
])('Get /megavault/historicalPnl with 2 vault subaccounts and main subaccount (%s), ' +
'excludes tick with missing vault ticks', async (
_name: string,
queryParam: string,
expectedTicksIndex1: (number | undefined)[],
Expand All @@ -202,16 +204,24 @@ describe('vault-controller#V4', () => {
...testConstants.defaultVault,
address: testConstants.defaultAddress,
clobPairId: testConstants.defaultPerpetualMarket.clobPairId,
createdAt: twoDaysAgo.toISO(),
}),
// Single tick for this vault will be excluded from result.
VaultTable.create({
...testConstants.defaultVault,
address: testConstants.vaultAddress,
clobPairId: testConstants.defaultPerpetualMarket2.clobPairId,
createdAt: almostTwoDaysAgo.toISO(),
}),
AssetPositionTable.upsert({
...testConstants.defaultAssetPosition,
subaccountId: MEGAVAULT_SUBACCOUNT_ID,
}),
TransferTable.create({
...testConstants.defaultTransfer,
recipientSubaccountId: MEGAVAULT_SUBACCOUNT_ID,
createdAt: twoDaysAgo.toISO(),
}),
]);

const createdPnlTicks: PnlTicksFromDatabase[] = await createPnlTicks(
Expand Down Expand Up @@ -559,6 +569,7 @@ describe('vault-controller#V4', () => {
...testConstants.defaultPnlTick,
subaccountId: testConstants.vaultSubaccountId,
}),
// Invalid pnl tick to be excluded as only a single pnl tick but 2 pnl ticks should exist.
PnlTicksTable.create({
...testConstants.defaultPnlTick,
subaccountId: testConstants.vaultSubaccountId,
Expand Down
74 changes: 53 additions & 21 deletions indexer/services/comlink/__tests__/lib/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ import {
defaultTendermintEventId2,
defaultTendermintEventId3,
} from '@dydxprotocol-indexer/postgres/build/__tests__/helpers/constants';
import { AssetPositionsMap, PerpetualPositionWithFunding, SubaccountResponseObject } from '../../src/types';
import {
AggregatedPnlTick, AssetPositionsMap, PerpetualPositionWithFunding, SubaccountResponseObject,
} from '../../src/types';
import { ZERO, ZERO_USDC_POSITION } from '../../src/lib/constants';
import { DateTime } from 'luxon';

Expand Down Expand Up @@ -844,15 +846,20 @@ describe('helpers', () => {
),
};

const aggregatedPnlTicks: PnlTicksFromDatabase[] = aggregateHourlyPnlTicks([pnlTick]);
const aggregatedPnlTicks: AggregatedPnlTick[] = aggregateHourlyPnlTicks([pnlTick]);
expect(
aggregatedPnlTicks,
).toEqual(
[expect.objectContaining({ ...testConstants.defaultPnlTick })],
[expect.objectContaining(
{
pnlTick: expect.objectContaining(testConstants.defaultPnlTick),
numTicks: 1,
},
)],
);
});

it('aggregates multiple pnl ticks same height', () => {
it('aggregates multiple pnl ticks same height and de-dupes ticks', () => {
const pnlTick: PnlTicksFromDatabase = {
...testConstants.defaultPnlTick,
id: PnlTicksTable.uuid(
Expand All @@ -862,13 +869,14 @@ describe('helpers', () => {
};
const pnlTick2: PnlTicksFromDatabase = {
...testConstants.defaultPnlTick,
subaccountId: testConstants.defaultSubaccountId2,
id: PnlTicksTable.uuid(
testConstants.defaultSubaccountId2,
testConstants.defaultPnlTick.createdAt,
),
};
const blockHeight2: string = '80';
const blockTime2: string = DateTime.fromISO(pnlTick.createdAt).startOf('hour').plus({ minute: 61 }).toISO();
const blockTime2: string = DateTime.fromISO(pnlTick.createdAt).plus({ hour: 1 }).toISO();
const pnlTick3: PnlTicksFromDatabase = {
...testConstants.defaultPnlTick,
id: PnlTicksTable.uuid(
Expand All @@ -880,11 +888,12 @@ describe('helpers', () => {
createdAt: blockTime2,
};
const blockHeight3: string = '81';
const blockTime3: string = DateTime.fromISO(pnlTick.createdAt).startOf('hour').plus({ minute: 62 }).toISO();
const blockTime3: string = DateTime.fromISO(pnlTick.createdAt).plus({ minute: 61 }).toISO();
const pnlTick4: PnlTicksFromDatabase = {
...testConstants.defaultPnlTick,
subaccountId: testConstants.defaultSubaccountId2,
id: PnlTicksTable.uuid(
testConstants.defaultPnlTick.subaccountId,
testConstants.defaultSubaccountId2,
blockTime3,
),
equity: '1',
Expand All @@ -894,29 +903,52 @@ describe('helpers', () => {
blockTime: blockTime3,
createdAt: blockTime3,
};
const blockHeight4: string = '82';
const blockTime4: string = DateTime.fromISO(pnlTick.createdAt).startOf('hour').plus({ minute: 63 }).toISO();
// should be de-duped
const pnlTick5: PnlTicksFromDatabase = {
...testConstants.defaultPnlTick,
subaccountId: testConstants.defaultSubaccountId2,
id: PnlTicksTable.uuid(
testConstants.defaultSubaccountId2,
blockTime4,
),
equity: '1',
totalPnl: '2',
netTransfers: '3',
blockHeight: blockHeight4,
blockTime: blockTime4,
createdAt: blockTime4,
};

const aggregatedPnlTicks: PnlTicksFromDatabase[] = aggregateHourlyPnlTicks(
[pnlTick, pnlTick2, pnlTick3, pnlTick4],
const aggregatedPnlTicks: AggregatedPnlTick[] = aggregateHourlyPnlTicks(
[pnlTick, pnlTick2, pnlTick3, pnlTick4, pnlTick5],
);
expect(aggregatedPnlTicks).toEqual(
expect.arrayContaining([
// Combined pnl tick at initial hour
expect.objectContaining({
equity: (parseFloat(testConstants.defaultPnlTick.equity) +
parseFloat(pnlTick2.equity)).toString(),
totalPnl: (parseFloat(testConstants.defaultPnlTick.totalPnl) +
parseFloat(pnlTick2.totalPnl)).toString(),
netTransfers: (parseFloat(testConstants.defaultPnlTick.netTransfers) +
parseFloat(pnlTick2.netTransfers)).toString(),
pnlTick: expect.objectContaining({
equity: (parseFloat(testConstants.defaultPnlTick.equity) +
parseFloat(pnlTick2.equity)).toString(),
totalPnl: (parseFloat(testConstants.defaultPnlTick.totalPnl) +
parseFloat(pnlTick2.totalPnl)).toString(),
netTransfers: (parseFloat(testConstants.defaultPnlTick.netTransfers) +
parseFloat(pnlTick2.netTransfers)).toString(),
}),
numTicks: 2,
}),
// Combined pnl tick at initial hour + 1 hour and initial hour + 1 hour, 1 minute
expect.objectContaining({
equity: (parseFloat(pnlTick3.equity) +
parseFloat(pnlTick4.equity)).toString(),
totalPnl: (parseFloat(pnlTick3.totalPnl) +
parseFloat(pnlTick4.totalPnl)).toString(),
netTransfers: (parseFloat(pnlTick3.netTransfers) +
parseFloat(pnlTick4.netTransfers)).toString(),
pnlTick: expect.objectContaining({
equity: (parseFloat(pnlTick3.equity) +
parseFloat(pnlTick4.equity)).toString(),
totalPnl: (parseFloat(pnlTick3.totalPnl) +
parseFloat(pnlTick4.totalPnl)).toString(),
netTransfers: (parseFloat(pnlTick3.netTransfers) +
parseFloat(pnlTick4.netTransfers)).toString(),
}),
numTicks: 2,
}),
]),
);
Expand Down
3 changes: 2 additions & 1 deletion indexer/services/comlink/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@
"@cosmjs/encoding": "^0.32.3",
"@dydxprotocol-indexer/base": "workspace:^0.0.1",
"@dydxprotocol-indexer/compliance": "workspace:^0.0.1",
"@dydxprotocol-indexer/notifications": "workspace:^0.0.1",
"@dydxprotocol-indexer/postgres": "workspace:^0.0.1",
"@dydxprotocol-indexer/redis": "workspace:^0.0.1",
"@dydxprotocol-indexer/v4-proto-parser": "workspace:^0.0.1",
"@dydxprotocol-indexer/v4-protos": "workspace:^0.0.1",
"@keplr-wallet/cosmos": "^0.12.122",
"@dydxprotocol-indexer/notifications": "workspace:^0.0.1",
"@tsoa/runtime": "^5.0.0",
"big.js": "^6.2.1",
"binary-searching": "^2.0.5",
"body-parser": "^1.20.0",
"cors": "^2.8.5",
"dd-trace": "^3.32.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
} from '@dydxprotocol-indexer/postgres';
import express from 'express';
import { matchedData } from 'express-validator';
import _ from 'lodash';
import {
Controller, Get, Query, Route,
} from 'tsoa';
Expand Down Expand Up @@ -156,7 +157,10 @@ class HistoricalPnlController extends Controller {
}

// aggregate pnlTicks for all subaccounts grouped by blockHeight
const aggregatedPnlTicks: PnlTicksFromDatabase[] = aggregateHourlyPnlTicks(pnlTicks);
const aggregatedPnlTicks: PnlTicksFromDatabase[] = _.map(
aggregateHourlyPnlTicks(pnlTicks),
'pnlTick',
);

return {
historicalPnl: aggregatedPnlTicks.map(
Expand Down
Loading
Loading