Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 committed Aug 9, 2024
1 parent 2d3af02 commit 82572aa
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 169 deletions.
3 changes: 2 additions & 1 deletion indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export const dydxChain: string = 'dydx';
export const defaultAddress: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc565lnf';
export const defaultAddress2: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc575lnf';
export const blockedAddress: string = 'dydx1f9k5qldwmqrnwy8hcgp4fw6heuvszt35egvtx2';
// Vault address was generated using script protocol/scripts/vault/get_vault.go
// Vault address for vault id 0 was generated using
// script protocol/scripts/vault/get_vault.go
export const vaultAddress: string = 'dydx1c0m5x87llaunl5sgv3q5vd7j5uha26d2q2r2q0';

// ============== Subaccounts ==============
Expand Down
220 changes: 110 additions & 110 deletions indexer/packages/postgres/__tests__/stores/pnl-ticks-table.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,124 +297,124 @@ describe('PnlTicks store', () => {
expect(leaderboardRankedData[defaultSubaccountId2].equity).toEqual('200');
});

it('Get all time ranked pnl ticks', async () => {
await setupRankedPnlTicksData();

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'ALL_TIME',
);
expect(leaderboardRankedData.length).toEqual(2);
expect(leaderboardRankedData[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '1200',
currentEquity: '1100',
timeSpan: 'ALL_TIME',
rank: '1',
}));
expect(leaderboardRankedData[1]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '300',
currentEquity: '200',
const testCases = [
{
description: 'Get all time ranked pnl ticks',
timeSpan: 'ALL_TIME',
rank: '2',
}));
});

it('Get one year ranked pnl ticks with missing pnl for one subaccount', async () => {
await setupRankedPnlTicksData();

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'ONE_YEAR',
);
expect(leaderboardRankedData.length).toEqual(2);
expect(leaderboardRankedData[0]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '300',
currentEquity: '200',
timeSpan: 'ONE_YEAR',
rank: '1',
}));
expect(leaderboardRankedData[1]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '40',
currentEquity: '1100',
expectedLength: 2,
expectedResults: [
{
address: defaultAddress,
pnl: '1200',
currentEquity: '1100',
timeSpan: 'ALL_TIME',
rank: '1',
},
{
address: defaultAddress2,
pnl: '300',
currentEquity: '200',
timeSpan: 'ALL_TIME',
rank: '2',
},
],
},
{
description: 'Get one year ranked pnl ticks with missing pnl for one subaccount',
timeSpan: 'ONE_YEAR',
rank: '2',
}));
});

it('Get thirty days ranked pnl ticks', async () => {
await setupRankedPnlTicksData();

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'THIRTY_DAYS',
);
expect(leaderboardRankedData.length).toEqual(2);
expect(leaderboardRankedData[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '30',
currentEquity: '1100',
timeSpan: 'THIRTY_DAYS',
rank: '1',
}));
expect(leaderboardRankedData[1]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '-30',
currentEquity: '200',
expectedLength: 2,
expectedResults: [
{
address: defaultAddress2,
pnl: '300',
currentEquity: '200',
timeSpan: 'ONE_YEAR',
rank: '1',
},
{
address: defaultAddress,
pnl: '40',
currentEquity: '1100',
timeSpan: 'ONE_YEAR',
rank: '2',
},
],
},
{
description: 'Get thirty days ranked pnl ticks',
timeSpan: 'THIRTY_DAYS',
rank: '2',
}));
});
expectedLength: 2,
expectedResults: [
{
address: defaultAddress,
pnl: '30',
currentEquity: '1100',
timeSpan: 'THIRTY_DAYS',
rank: '1',
},
{
address: defaultAddress2,
pnl: '-30',
currentEquity: '200',
timeSpan: 'THIRTY_DAYS',
rank: '2',
},
],
},
{
description: 'Get seven days ranked pnl ticks',
timeSpan: 'SEVEN_DAYS',
expectedLength: 2,
expectedResults: [
{
address: defaultAddress,
pnl: '20',
currentEquity: '1100',
timeSpan: 'SEVEN_DAYS',
rank: '1',
},
{
address: defaultAddress2,
pnl: '-20',
currentEquity: '200',
timeSpan: 'SEVEN_DAYS',
rank: '2',
},
],
},
{
description: 'Get one day ranked pnl ticks',
timeSpan: 'ONE_DAY',
expectedLength: 2,
expectedResults: [
{
address: defaultAddress,
pnl: '10',
currentEquity: '1100',
timeSpan: 'ONE_DAY',
rank: '1',
},
{
address: defaultAddress2,
pnl: '-10',
currentEquity: '200',
timeSpan: 'ONE_DAY',
rank: '2',
},
],
},
];

it('Get seven days ranked pnl ticks', async () => {
it.each(testCases)('$description', async ({ timeSpan, expectedLength, expectedResults }) => {
await setupRankedPnlTicksData();

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'SEVEN_DAYS',
);
expect(leaderboardRankedData.length).toEqual(2);
expect(leaderboardRankedData[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '20',
currentEquity: '1100',
timeSpan: 'SEVEN_DAYS',
rank: '1',
}));
expect(leaderboardRankedData[1]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '-20',
currentEquity: '200',
timeSpan: 'SEVEN_DAYS',
rank: '2',
}));
});
const leaderboardRankedData = await PnlTicksTable.getRankedPnlTicks(timeSpan);

it('Get one day ranked pnl ticks', async () => {
await setupRankedPnlTicksData();
expect(leaderboardRankedData.length).toEqual(expectedLength);

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'ONE_DAY',
);
expect(leaderboardRankedData.length).toEqual(2);
expect(leaderboardRankedData[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '10',
currentEquity: '1100',
timeSpan: 'ONE_DAY',
rank: '1',
}));
expect(leaderboardRankedData[1]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '-10',
currentEquity: '200',
timeSpan: 'ONE_DAY',
rank: '2',
}));
expectedResults.forEach((expectedResult, index) => {
expect(leaderboardRankedData[index]).toEqual(expect.objectContaining(expectedResult));
});
});

it('Ensure that vault addresses are not included in the leaderboard', async () => {
Expand Down
6 changes: 3 additions & 3 deletions indexer/packages/postgres/src/stores/pnl-ticks-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -319,8 +319,8 @@ function convertTimespanToSQL(timeSpan: string): string {
* 4. aggregated_results: Aggregates the PnL differences and current equity for
* all subaccounts, grouping by address.
*
* 5. The final SELECT statement then ranks the addresses based on their total PnL
* in descending order, providing a snapshot of subaccount performance over the
* 5. The final SELECT statement then ranks the addresses based on their total PnL
* in descending order, providing a snapshot of subaccount performance over the
* specified time span.
*
*/
Expand Down Expand Up @@ -396,7 +396,7 @@ async function getRankedPnlTicksForTimeSpan(
/**
* Constructs a query to calculate and rank the Profit and Loss (PnL) and current equity of
* subaccounts for the current day. This query is divided into 3 main parts:
* 1. latest_pnl: This selects the most recent PnL tick for each Parent subaccount
* 1. latest_pnl: This selects the most recent PnL tick for each Parent subaccount
* and associated child subaccounts. It filters subaccounts based on the current date.
* Additionally, it excludes any addresses that are vault addresses.
*
Expand Down
Loading

0 comments on commit 82572aa

Please sign in to comment.