Skip to content

Commit

Permalink
[OTE-544][OTE-545] Add rountable task for leaderboard table (#1965)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Aug 12, 2024
1 parent 3930d14 commit 82a4b77
Show file tree
Hide file tree
Showing 12 changed files with 1,845 additions and 5 deletions.
31 changes: 31 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,9 @@ export const dydxChain: string = 'dydx';
export const defaultAddress: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc565lnf';
export const defaultAddress2: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc575lnf';
export const blockedAddress: string = 'dydx1f9k5qldwmqrnwy8hcgp4fw6heuvszt35egvtx2';
// Vault address for vault id 0 was generated using
// script protocol/scripts/vault/get_vault.go
export const vaultAddress: string = 'dydx1c0m5x87llaunl5sgv3q5vd7j5uha26d2q2r2q0';

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

Expand All @@ -91,6 +94,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 +136,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 +149,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 +165,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');
});

const testCases = [
{
description: 'Get all time ranked pnl ticks',
timeSpan: 'ALL_TIME',
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',
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',
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.each(testCases)('$description', async ({ timeSpan, expectedLength, expectedResults }) => {
await setupRankedPnlTicksData();

const leaderboardRankedData = await PnlTicksTable.getRankedPnlTicks(timeSpan);

expect(leaderboardRankedData.length).toEqual(expectedLength);

expectedResults.forEach((expectedResult, index) => {
expect(leaderboardRankedData[index]).toEqual(expect.objectContaining(expectedResult));
});
});

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[] {
return vaultAddresses;
}
Loading

0 comments on commit 82a4b77

Please sign in to comment.