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 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
11 changes: 11 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ export const defaultSubaccount3: SubaccountCreateObject = {
updatedAtHeight: createdHeight,
};

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

export const isolatedSubaccount: SubaccountCreateObject = {
address: defaultAddress,
subaccountNumber: 128,
Expand Down Expand Up @@ -119,6 +126,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 Down
231 changes: 231 additions & 0 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,28 @@ 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,
} 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 @@ -283,4 +293,225 @@ describe('PnlTicks store', () => {
expect(mostRecent[defaultSubaccountId].equity).toEqual('1014');
expect(mostRecent[defaultSubaccountId2].equity).toEqual('200');
});

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

const mostRecent: LeaderboardPnlCreateObject[] = await PnlTicksTable.getRankedPnlTicks(
'ALL_TIME',
)
expect(mostRecent.length).toEqual(2);
expect(mostRecent[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '1200',
currentEquity: '1100',
timeSpan: 'ALL_TIME',
rank: '1'
}));
expect(mostRecent[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 mostRecent: LeaderboardPnlCreateObject[] = await PnlTicksTable.getRankedPnlTicks(
'ONE_YEAR',
)
expect(mostRecent.length).toEqual(2);
expect(mostRecent[0]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '300',
currentEquity: '200',
timeSpan: 'ONE_YEAR',
rank: '1'
}));
expect(mostRecent[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 mostRecent: LeaderboardPnlCreateObject[] = await PnlTicksTable.getRankedPnlTicks(
'THIRTY_DAYS',
)
expect(mostRecent.length).toEqual(2);
expect(mostRecent[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '30',
currentEquity: '1100',
timeSpan: 'THIRTY_DAYS',
rank: '1'
}));
expect(mostRecent[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 mostRecent: LeaderboardPnlCreateObject[] = await PnlTicksTable.getRankedPnlTicks(
'SEVEN_DAYS',
)
expect(mostRecent.length).toEqual(2);
expect(mostRecent[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '20',
currentEquity: '1100',
timeSpan: 'SEVEN_DAYS',
rank: '1'
}));
expect(mostRecent[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 mostRecent: LeaderboardPnlCreateObject[] = await PnlTicksTable.getRankedPnlTicks(
'ONE_DAY',
)
expect(mostRecent.length).toEqual(2);
expect(mostRecent[0]).toEqual(expect.objectContaining({
address: defaultAddress,
pnl: '10',
currentEquity: '1100',
timeSpan: 'ONE_DAY',
rank: '1'
}));
expect(mostRecent[1]).toEqual(expect.objectContaining({
address: defaultAddress2,
pnl: '-10',
currentEquity: '200',
timeSpan: 'ONE_DAY',
rank: '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,
},
]);
}

8 changes: 8 additions & 0 deletions indexer/packages/postgres/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ export const DEFAULT_POSTGRES_OPTIONS : Options = config.USE_READ_REPLICA
export const MAX_PARENT_SUBACCOUNTS: number = 128;

export const CHILD_SUBACCOUNT_MULTIPLIER: number = 1000;

export enum LEADERBOARD_TIMESPAN {
ONE_DAY = 'ONE_DAY',
SEVEN_DAYS = 'SEVEN_DAYS',
THIRTY_DAYS = 'THIRTY_DAYS',
ONE_YEAR = 'ONE_YEAR',
ALL_TIME = 'ALL_TIME',
}
Loading
Loading