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

[OTE-544][OTE-545] Add rountable task for leaderboard table #1965

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
30 changes: 30 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +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
export const vaultAddress: string = 'dydx1c0m5x87llaunl5sgv3q5vd7j5uha26d2q2r2q0';
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: also add that this is for vault id 0


// ============== Subaccounts ==============

Expand All @@ -91,6 +93,20 @@ export const defaultSubaccount3: SubaccountCreateObject = {
updatedAtHeight: createdHeight,
};

export const defaultSubaccountWithAlternateAddress: SubaccountCreateObject = {
address: defaultAddress2,
subaccountNumber: 0,
updatedAt: createdDateTime.toISO(),
updatedAtHeight: createdHeight,
};

export const vaultSubaccount: SubaccountCreateObject = {
address: vaultAddress,
subaccountNumber: 0,
updatedAt: createdDateTime.toISO(),
updatedAtHeight: createdHeight,
};

export const isolatedSubaccount: SubaccountCreateObject = {
address: defaultAddress,
subaccountNumber: 128,
Expand Down Expand Up @@ -119,6 +135,10 @@ export const defaultSubaccountId3: string = SubaccountTable.uuid(
defaultAddress,
defaultSubaccount3.subaccountNumber,
);
export const defaultSubaccountIdWithAlternateAddress: string = SubaccountTable.uuid(
defaultAddress2,
defaultSubaccountWithAlternateAddress.subaccountNumber,
);
export const isolatedSubaccountId: string = SubaccountTable.uuid(
defaultAddress,
isolatedSubaccount.subaccountNumber,
Expand All @@ -128,6 +148,11 @@ export const isolatedSubaccountId2: string = SubaccountTable.uuid(
isolatedSubaccount2.subaccountNumber,
);

export const vaultSubaccountId: string = SubaccountTable.uuid(
vaultAddress,
vaultSubaccount.subaccountNumber,
);

// ============== Wallets ==============
export const defaultWallet: WalletCreateObject = {
address: defaultAddress,
Expand All @@ -139,6 +164,11 @@ export const defaultWallet2: WalletCreateObject = {
totalTradingRewards: denomToHumanReadableConversion(1),
};

export const vaultWallet: WalletCreateObject = {
address: vaultAddress,
totalTradingRewards: denomToHumanReadableConversion(0),
};

export const defaultWallet3: WalletCreateObject = {
address: defaultAddress2,
totalTradingRewards: denomToHumanReadableConversion(0),
Expand Down
266 changes: 263 additions & 3 deletions indexer/packages/postgres/__tests__/stores/pnl-ticks-table.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
IsoString,
LeaderboardPnlCreateObject,
Ordering,
PnlTicksColumns,
PnlTicksCreateObject,
Expand All @@ -8,19 +9,31 @@ import * as PnlTicksTable from '../../src/stores/pnl-ticks-table';
import * as BlockTable from '../../src/stores/block-table';
import { clearData, migrate, teardown } from '../../src/helpers/db-helpers';
import { seedData } from '../helpers/mock-generators';
import * as WalletTable from '../../src/stores/wallet-table';
import * as SubaccountTable from '../../src/stores/subaccount-table';
import {
defaultAddress,
defaultAddress2,
defaultBlock,
defaultBlock2,
defaultPnlTick,
defaultSubaccountId,
defaultSubaccountId2,
defaultSubaccountIdWithAlternateAddress,
defaultSubaccountWithAlternateAddress,
defaultWallet2,
vaultSubaccount,
vaultSubaccountId,
vaultWallet,
} from '../helpers/constants';
import { DateTime } from 'luxon';
import { ZERO_TIME_ISO_8601 } from '../../src/constants';

describe('PnlTicks store', () => {
beforeEach(async () => {
await seedData();
await WalletTable.create(defaultWallet2);
await SubaccountTable.create(defaultSubaccountWithAlternateAddress);
});

beforeAll(async () => {
Expand Down Expand Up @@ -275,12 +288,259 @@ describe('PnlTicks store', () => {
},
]);

const mostRecent: {
const leaderboardRankedData: {
[accountId: string]: PnlTicksCreateObject
} = await PnlTicksTable.findMostRecentPnlTickForEachAccount(
'3',
);
expect(mostRecent[defaultSubaccountId].equity).toEqual('1014');
expect(mostRecent[defaultSubaccountId2].equity).toEqual('200');
expect(leaderboardRankedData[defaultSubaccountId].equity).toEqual('1014');
expect(leaderboardRankedData[defaultSubaccountId2].equity).toEqual('200');
});

it('Get all time ranked pnl ticks', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add a test case for if you're trying to get a month of data and there is < 1 month worth of data?

Copy link
Contributor Author

@affanv14 affanv14 Aug 9, 2024

Choose a reason for hiding this comment

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

there is already a test case to get a year of data and there is < 1 year worth of data. Should translate to month the same way

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',
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',
timeSpan: 'ONE_YEAR',
rank: '2',
}));
});

it('Get thirty days ranked pnl ticks', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to do an it.each for these tests?

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',
timeSpan: 'THIRTY_DAYS',
rank: '2',
}));
});

it('Get seven days ranked pnl ticks', async () => {
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',
}));
});

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

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',
}));
});

it('Ensure that vault addresses are not included in the leaderboard', async () => {
await setupRankedPnlTicksData();

await WalletTable.create(vaultWallet);
await SubaccountTable.create(vaultSubaccount);
await PnlTicksTable.create({
subaccountId: vaultSubaccountId,
equity: '100',
createdAt: DateTime.utc().toISO(),
totalPnl: '100',
netTransfers: '50',
blockHeight: '9',
blockTime: defaultBlock.time,
});

const leaderboardRankedData: LeaderboardPnlCreateObject[] = await
PnlTicksTable.getRankedPnlTicks(
'ALL_TIME',
);
expect(leaderboardRankedData.length).toEqual(2);
});

});

async function setupRankedPnlTicksData() {
await Promise.all([
BlockTable.create({
blockHeight: '3',
time: defaultBlock.time,
}),
BlockTable.create({
blockHeight: '5',
time: defaultBlock.time,
}),
BlockTable.create({
blockHeight: '7',
time: defaultBlock.time,
}),
BlockTable.create({
blockHeight: '9',
time: defaultBlock.time,
}),
]);
await PnlTicksTable.createMany([
{
subaccountId: defaultSubaccountId,
equity: '1100',
createdAt: DateTime.utc().toISO(),
totalPnl: '1200',
netTransfers: '50',
blockHeight: '9',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountId,
equity: '1090',
createdAt: DateTime.utc().minus({ day: 1 }).toISO(),
totalPnl: '1190',
netTransfers: '50',
blockHeight: '7',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountId,
equity: '1080',
createdAt: DateTime.utc().minus({ day: 7 }).toISO(),
totalPnl: '1180',
netTransfers: '50',
blockHeight: '5',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountId,
equity: '1070',
createdAt: DateTime.utc().minus({ day: 30 }).toISO(),
totalPnl: '1170',
netTransfers: '50',
blockHeight: '3',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountId,
equity: '1060',
createdAt: DateTime.utc().minus({ day: 365 }).toISO(),
totalPnl: '1160',
netTransfers: '50',
blockHeight: '1',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountIdWithAlternateAddress,
equity: '200',
createdAt: DateTime.utc().toISO(),
totalPnl: '300',
netTransfers: '50',
blockHeight: '9',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountIdWithAlternateAddress,
equity: '210',
createdAt: DateTime.utc().minus({ day: 1 }).toISO(),
totalPnl: '310',
netTransfers: '50',
blockHeight: '7',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountIdWithAlternateAddress,
equity: '220',
createdAt: DateTime.utc().minus({ week: 1 }).toISO(),
totalPnl: '320',
netTransfers: '50',
blockHeight: '5',
blockTime: defaultBlock.time,
},
{
subaccountId: defaultSubaccountIdWithAlternateAddress,
equity: '230',
createdAt: DateTime.utc().minus({ month: 1 }).toISO(),
totalPnl: '330',
netTransfers: '50',
blockHeight: '3',
blockTime: defaultBlock.time,
},
]);
}
5 changes: 5 additions & 0 deletions indexer/packages/postgres/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,13 @@
import { DateTime } from 'luxon';

import { IsoString } from '../types';
import vaultAddresses from './vault-addresses.json';

export function blockTimeFromIsoString(isoString: IsoString): number {
const dateTime: DateTime = DateTime.fromISO(isoString, { zone: 'utc' });
return Math.floor(dateTime.toMillis() / 1000);
}

export function getVaultAddresses(): string[] {
affanv14 marked this conversation as resolved.
Show resolved Hide resolved
return vaultAddresses;
}
Loading
Loading