From 466903c9a0220df01919d354294e39039fe218ed Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 18 Jul 2024 17:23:10 -0400 Subject: [PATCH 1/4] update subaccountId to address --- .../postgres/__tests__/helpers/constants.ts | 9 +++++---- .../stores/leaderboard-pnl-table.test.ts | 4 ++-- ...0717160024_create_leaderboard_pnl_table.ts | 4 ++-- .../src/models/leaderboard-pnl-model.ts | 19 +++++-------------- .../src/stores/leaderboard-pnl-table.ts | 12 ++++++------ .../postgres/src/types/db-model-types.ts | 2 +- .../src/types/leaderboard-pnl-types.ts | 4 ++-- .../postgres/src/types/query-types.ts | 2 +- 8 files changed, 24 insertions(+), 32 deletions(-) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 84ca02bfdc..91cd0e31e5 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -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 ============== @@ -860,7 +861,7 @@ export const duplicatedSubaccountUsername: SubaccountUsernamesCreateObject = { // ============== Leaderboard pnl Data ============== export const defaultLeaderboardPnlOneDay: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId, + address: defaultAddress, timeSpan: 'ONE_DAY', pnl: '10000', currentEquity: '1000', @@ -868,7 +869,7 @@ export const defaultLeaderboardPnlOneDay: LeaderboardPnlCreateObject = { }; export const defaultLeaderboardPnl2OneDay: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId2, + address: defaultAddress2, timeSpan: 'ONE_DAY', pnl: '100', currentEquity: '10000', @@ -876,7 +877,7 @@ export const defaultLeaderboardPnl2OneDay: LeaderboardPnlCreateObject = { }; export const defaultLeaderboardPnl1AllTime: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId, + address: defaultAddress, timeSpan: 'ALL_TIME', pnl: '10000', currentEquity: '1000', @@ -884,7 +885,7 @@ export const defaultLeaderboardPnl1AllTime: LeaderboardPnlCreateObject = { }; export const defaultLeaderboardPnlOneDayToUpsert: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId, + address: defaultAddress, timeSpan: 'ONE_DAY', pnl: '100000', currentEquity: '1000', diff --git a/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts b/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts index 85d1eb6f97..2d7d0b6692 100644 --- a/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts @@ -45,7 +45,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), @@ -54,7 +54,7 @@ describe('LeaderboardPnl store', () => { const leaderboardPnl: LeaderboardPnlFromDatabase[] = await LeaderboardPnlTable.findAll( { - subaccountId: [defaultLeaderboardPnlOneDay.subaccountId], + address: [defaultLeaderboardPnlOneDay.address], timeSpan: [defaultLeaderboardPnlOneDay.timeSpan], }, [], diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts index 47fb10f476..8d7d4748b0 100644 --- a/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts @@ -2,7 +2,7 @@ import * as Knex from 'knex'; export async function up(knex: Knex): Promise { return knex.schema.createTable('leaderboard_pnl', (table) => { - table.uuid('subaccountId').notNullable().references('id').inTable('subaccounts'); + table.string('address').notNullable(); table.enum( 'timeSpan', [ @@ -16,7 +16,7 @@ export async function up(knex: Knex): Promise { table.string('pnl').notNullable(); table.string('currentEquity').notNullable(); table.integer('rank').notNullable(); - table.primary(['subaccountId', 'timeSpan']); + table.primary(['address', 'timeSpan']); }); } diff --git a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts index 7c83f6872e..e232835133 100644 --- a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts +++ b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts @@ -13,32 +13,23 @@ export default class LeaderboardPnlModel extends BaseModel { } static get idColumn() { - return ['subaccountId', 'timeSpan']; + return ['address', 'timeSpan']; } - static relationMappings = { - subaccount: { - relation: Model.BelongsToOneRelation, - modelClass: path.join(__dirname, 'subaccount-model'), - join: { - from: 'leaderboard_pnl.subaccountId', - to: 'subaccounts.id', - }, - }, - }; + static relationMappings = {}; static get jsonSchema() { 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 }, @@ -47,7 +38,7 @@ export default class LeaderboardPnlModel extends BaseModel { }; } - subaccountId!: string; + address!: string; timeSpan!: string; diff --git a/indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts b/indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts index 6dbf33752d..15de08f02c 100644 --- a/indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts +++ b/indexer/packages/postgres/src/stores/leaderboard-pnl-table.ts @@ -26,7 +26,7 @@ import { export async function findAll( { - subaccountId, + address, timeSpan, rank, limit, @@ -36,7 +36,7 @@ export async function findAll( ): Promise { verifyAllRequiredFields( { - subaccountId, + address, timeSpan, rank, limit, @@ -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) { @@ -124,7 +124,7 @@ export async function bulkUpsert( LeaderboardPnlColumns.rank, ], stringColumns: [ - LeaderboardPnlColumns.subaccountId, + LeaderboardPnlColumns.address, LeaderboardPnlColumns.timeSpan, LeaderboardPnlColumns.currentEquity, LeaderboardPnlColumns.pnl, @@ -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); diff --git a/indexer/packages/postgres/src/types/db-model-types.ts b/indexer/packages/postgres/src/types/db-model-types.ts index bee5287c24..a6f4e1192e 100644 --- a/indexer/packages/postgres/src/types/db-model-types.ts +++ b/indexer/packages/postgres/src/types/db-model-types.ts @@ -261,7 +261,7 @@ export interface SubaccountUsernamesFromDatabase { } export interface LeaderboardPnlFromDatabase { - subaccountId: string; + address: string; timeSpan: string; pnl: string; currentEquity: string; diff --git a/indexer/packages/postgres/src/types/leaderboard-pnl-types.ts b/indexer/packages/postgres/src/types/leaderboard-pnl-types.ts index 5441b3d01b..f248922302 100644 --- a/indexer/packages/postgres/src/types/leaderboard-pnl-types.ts +++ b/indexer/packages/postgres/src/types/leaderboard-pnl-types.ts @@ -1,7 +1,7 @@ /* ------- LEADERBOARD PNL TYPES ------- */ export interface LeaderboardPnlCreateObject { - subaccountId: string; + address: string; pnl: string; timeSpan: string; currentEquity: string; @@ -9,7 +9,7 @@ export interface LeaderboardPnlCreateObject { } export enum LeaderboardPnlColumns { - subaccountId = 'subaccountId', + address = 'address', timeSpan = 'timeSpan', pnl = 'pnl', currentEquity = 'currentEquity', diff --git a/indexer/packages/postgres/src/types/query-types.ts b/indexer/packages/postgres/src/types/query-types.ts index b7271d34c1..75417a6d8b 100644 --- a/indexer/packages/postgres/src/types/query-types.ts +++ b/indexer/packages/postgres/src/types/query-types.ts @@ -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[]; } From 8f76deaf8827836467f8841d758402b7a5174cde Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 18 Jul 2024 17:30:31 -0400 Subject: [PATCH 2/4] lint --- indexer/packages/postgres/src/models/leaderboard-pnl-model.ts | 4 ---- 1 file changed, 4 deletions(-) diff --git a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts index e232835133..838dda9749 100644 --- a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts +++ b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts @@ -1,7 +1,3 @@ -import path from 'path'; - -import { Model } from 'objection'; - import { NumericPattern } from '../lib/validators'; import UpsertQueryBuilder from '../query-builders/upsert'; import BaseModel from './base-model'; From 76333e7ada1ea5605e7ed9f1b45ec24db3b741a0 Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 18 Jul 2024 18:06:07 -0400 Subject: [PATCH 3/4] address comments --- ...20240717160024_create_leaderboard_pnl_table.ts | 2 +- .../postgres/src/models/leaderboard-pnl-model.ts | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts b/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts index 8d7d4748b0..0db3a7f8c7 100644 --- a/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts +++ b/indexer/packages/postgres/src/db/migrations/migration_files/20240717160024_create_leaderboard_pnl_table.ts @@ -2,7 +2,7 @@ import * as Knex from 'knex'; export async function up(knex: Knex): Promise { return knex.schema.createTable('leaderboard_pnl', (table) => { - table.string('address').notNullable(); + table.string('address').notNullable().references('address').inTable('wallets'); table.enum( 'timeSpan', [ diff --git a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts index 838dda9749..60e54aaa02 100644 --- a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts +++ b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts @@ -1,3 +1,7 @@ +import path from 'path'; + +import { Model } from 'objection'; + import { NumericPattern } from '../lib/validators'; import UpsertQueryBuilder from '../query-builders/upsert'; import BaseModel from './base-model'; @@ -12,7 +16,16 @@ export default class LeaderboardPnlModel extends BaseModel { return ['address', 'timeSpan']; } - static relationMappings = {}; + static relationMappings = { + wallets: { + relation: Model.BelongsToOneRelation, + modelClass: path.join(__dirname, 'wallet-model'), + join: { + from: 'leaderboard_pnl.address', + to: 'wallets.address', + }, + }, + }; static get jsonSchema() { return { From 81f56e7db5e5156a12662a51edb42af6f2d92414 Mon Sep 17 00:00:00 2001 From: affan Date: Thu, 18 Jul 2024 19:14:57 -0400 Subject: [PATCH 4/4] add new wallet --- indexer/packages/postgres/__tests__/helpers/constants.ts | 5 +++++ .../postgres/__tests__/stores/leaderboard-pnl-table.test.ts | 3 +++ 2 files changed, 8 insertions(+) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 91cd0e31e5..28e4524a4c 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -139,6 +139,11 @@ export const defaultWallet2: WalletCreateObject = { totalTradingRewards: denomToHumanReadableConversion(1), }; +export const defaultWallet3: WalletCreateObject = { + address: defaultAddress2, + totalTradingRewards: denomToHumanReadableConversion(0), +}; + // ============== Assets ============== export const defaultAsset: AssetCreateObject = { diff --git a/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts b/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts index 2d7d0b6692..30680c73e1 100644 --- a/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/leaderboard-pnl-table.test.ts @@ -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 () => {