Skip to content

Commit

Permalink
update subaccountId to address (dydxprotocol#1947)
Browse files Browse the repository at this point in the history
  • Loading branch information
affanv14 authored Jul 19, 2024
1 parent cdb8a78 commit d725976
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 26 deletions.
14 changes: 10 additions & 4 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export const createdHeight: string = '2';
export const invalidTicker: string = 'INVALID-INVALID';
export const dydxChain: string = 'dydx';
export const defaultAddress: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc565lnf';
export const defaultAddress2: string = 'dydx1n88uc38xhjgxzw9nwre4ep2c8ga4fjxc575lnf';
export const blockedAddress: string = 'dydx1f9k5qldwmqrnwy8hcgp4fw6heuvszt35egvtx2';

// ============== Subaccounts ==============
Expand Down Expand Up @@ -138,6 +139,11 @@ export const defaultWallet2: WalletCreateObject = {
totalTradingRewards: denomToHumanReadableConversion(1),
};

export const defaultWallet3: WalletCreateObject = {
address: defaultAddress2,
totalTradingRewards: denomToHumanReadableConversion(0),
};

// ============== Assets ==============

export const defaultAsset: AssetCreateObject = {
Expand Down Expand Up @@ -860,31 +866,31 @@ export const duplicatedSubaccountUsername: SubaccountUsernamesCreateObject = {
// ============== Leaderboard pnl Data ==============

export const defaultLeaderboardPnlOneDay: LeaderboardPnlCreateObject = {
subaccountId: defaultSubaccountId,
address: defaultAddress,
timeSpan: 'ONE_DAY',
pnl: '10000',
currentEquity: '1000',
rank: 1,
};

export const defaultLeaderboardPnl2OneDay: LeaderboardPnlCreateObject = {
subaccountId: defaultSubaccountId2,
address: defaultAddress2,
timeSpan: 'ONE_DAY',
pnl: '100',
currentEquity: '10000',
rank: 2,
};

export const defaultLeaderboardPnl1AllTime: LeaderboardPnlCreateObject = {
subaccountId: defaultSubaccountId,
address: defaultAddress,
timeSpan: 'ALL_TIME',
pnl: '10000',
currentEquity: '1000',
rank: 1,
};

export const defaultLeaderboardPnlOneDayToUpsert: LeaderboardPnlCreateObject = {
subaccountId: defaultSubaccountId,
address: defaultAddress,
timeSpan: 'ONE_DAY',
pnl: '100000',
currentEquity: '1000',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import {
defaultLeaderboardPnlOneDay,
defaultLeaderboardPnl1AllTime,
defaultLeaderboardPnlOneDayToUpsert,
defaultWallet3,
} from '../helpers/constants';
import { seedData } from '../helpers/mock-generators';
import { WalletTable } from '../../src';

describe('LeaderboardPnl store', () => {
beforeEach(async () => {
await seedData();
await WalletTable.create(defaultWallet3);
});

beforeAll(async () => {
Expand Down Expand Up @@ -45,7 +48,7 @@ describe('LeaderboardPnl store', () => {
expect(leaderboardPnls.length).toEqual(3);
});

it('Successfully finds LeaderboardPnl with subaccountId and timespan', async () => {
it('Successfully finds LeaderboardPnl with address and timespan', async () => {
await Promise.all([
LeaderboardPnlTable.create(defaultLeaderboardPnlOneDay),
LeaderboardPnlTable.create(defaultLeaderboardPnl2OneDay),
Expand All @@ -54,7 +57,7 @@ describe('LeaderboardPnl store', () => {

const leaderboardPnl: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll(
{
subaccountId: [defaultLeaderboardPnlOneDay.subaccountId],
address: [defaultLeaderboardPnlOneDay.address],
timeSpan: [defaultLeaderboardPnlOneDay.timeSpan],
},
[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as Knex from 'knex';

export async function up(knex: Knex): Promise<void> {
return knex.schema.createTable('leaderboard_pnl', (table) => {
table.uuid('subaccountId').notNullable().references('id').inTable('subaccounts');
table.string('address').notNullable().references('address').inTable('wallets');
table.enum(
'timeSpan',
[
Expand All @@ -16,7 +16,7 @@ export async function up(knex: Knex): Promise<void> {
table.string('pnl').notNullable();
table.string('currentEquity').notNullable();
table.integer('rank').notNullable();
table.primary(['subaccountId', 'timeSpan']);
table.primary(['address', 'timeSpan']);
});
}

Expand Down
16 changes: 8 additions & 8 deletions indexer/packages/postgres/src/models/leaderboard-pnl-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,16 @@ export default class LeaderboardPnlModel extends BaseModel {
}

static get idColumn() {
return ['subaccountId', 'timeSpan'];
return ['address', 'timeSpan'];
}

static relationMappings = {
subaccount: {
wallets: {
relation: Model.BelongsToOneRelation,
modelClass: path.join(__dirname, 'subaccount-model'),
modelClass: path.join(__dirname, 'wallet-model'),
join: {
from: 'leaderboard_pnl.subaccountId',
to: 'subaccounts.id',
from: 'leaderboard_pnl.address',
to: 'wallets.address',
},
},
};
Expand All @@ -31,14 +31,14 @@ export default class LeaderboardPnlModel extends BaseModel {
return {
type: 'object',
required: [
'subaccountId',
'address',
'timeSpan',
'pnl',
'currentEquity',
'rank',
],
properties: {
subaccountId: { type: 'string' },
address: { type: 'string' },
timeSpan: { type: 'string' },
pnl: { type: 'string', pattern: NumericPattern },
currentEquity: { type: 'string', pattern: NumericPattern },
Expand All @@ -47,7 +47,7 @@ export default class LeaderboardPnlModel extends BaseModel {
};
}

subaccountId!: string;
address!: string;

timeSpan!: string;

Expand Down
12 changes: 6 additions & 6 deletions indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {

export async function findAll(
{
subaccountId,
address,
timeSpan,
rank,
limit,
Expand All @@ -36,7 +36,7 @@ export async function findAll(
): Promise<LeaderboardPnlFromDatabase[]> {
verifyAllRequiredFields(
{
subaccountId,
address,
timeSpan,
rank,
limit,
Expand All @@ -49,8 +49,8 @@ export async function findAll(
options,
);

if (subaccountId) {
baseQuery = baseQuery.whereIn(LeaderboardPnlColumns.subaccountId, subaccountId);
if (address) {
baseQuery = baseQuery.whereIn(LeaderboardPnlColumns.address, address);
}

if (timeSpan) {
Expand Down Expand Up @@ -124,7 +124,7 @@ export async function bulkUpsert(
LeaderboardPnlColumns.rank,
],
stringColumns: [
LeaderboardPnlColumns.subaccountId,
LeaderboardPnlColumns.address,
LeaderboardPnlColumns.timeSpan,
LeaderboardPnlColumns.currentEquity,
LeaderboardPnlColumns.pnl,
Expand All @@ -135,7 +135,7 @@ export async function bulkUpsert(
table: LeaderboardPnlModel.tableName,
objectRows: rows,
columns,
uniqueIdentifiers: [LeaderboardPnlColumns.subaccountId, LeaderboardPnlColumns.timeSpan],
uniqueIdentifiers: [LeaderboardPnlColumns.address, LeaderboardPnlColumns.timeSpan],
});

const transaction: Knex.Transaction | undefined = Transaction.get(options.txId);
Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/types/db-model-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ export interface SubaccountUsernamesFromDatabase {
}

export interface LeaderboardPnlFromDatabase {
subaccountId: string;
address: string;
timeSpan: string;
pnl: string;
currentEquity: string;
Expand Down
4 changes: 2 additions & 2 deletions indexer/packages/postgres/src/types/leaderboard-pnl-types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* ------- LEADERBOARD PNL TYPES ------- */

export interface LeaderboardPnlCreateObject {
subaccountId: string;
address: string;
pnl: string;
timeSpan: string;
currentEquity: string;
rank: number;
}

export enum LeaderboardPnlColumns {
subaccountId = 'subaccountId',
address = 'address',
timeSpan = 'timeSpan',
pnl = 'pnl',
currentEquity = 'currentEquity',
Expand Down
2 changes: 1 addition & 1 deletion indexer/packages/postgres/src/types/query-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ export interface TradingRewardAggregationQueryConfig extends QueryConfig {
}

export interface LeaderboardPnlQueryConfig extends QueryConfig {
[QueryableField.SUBACCOUNT_ID]?: string[];
[QueryableField.ADDRESS]?: string[];
[QueryableField.TIMESPAN]?: string[];
[QueryableField.RANK]?: number[];
}

0 comments on commit d725976

Please sign in to comment.