From d7259760f1f1097514ba271c6295ba483f5c0ebc Mon Sep 17 00:00:00 2001 From: Mohammed Affan Date: Thu, 18 Jul 2024 22:39:36 -0400 Subject: [PATCH] update subaccountId to address (#1947) --- .../postgres/__tests__/helpers/constants.ts | 14 ++++++++++---- .../stores/leaderboard-pnl-table.test.ts | 7 +++++-- ...0240717160024_create_leaderboard_pnl_table.ts | 4 ++-- .../postgres/src/models/leaderboard-pnl-model.ts | 16 ++++++++-------- .../postgres/src/stores/leaderboard-pnl-table.ts | 12 ++++++------ .../postgres/src/types/db-model-types.ts | 2 +- .../postgres/src/types/leaderboard-pnl-types.ts | 4 ++-- .../packages/postgres/src/types/query-types.ts | 2 +- 8 files changed, 35 insertions(+), 26 deletions(-) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index 84ca02bfdc..28e4524a4c 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 ============== @@ -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 = { @@ -860,7 +866,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 +874,7 @@ export const defaultLeaderboardPnlOneDay: LeaderboardPnlCreateObject = { }; export const defaultLeaderboardPnl2OneDay: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId2, + address: defaultAddress2, timeSpan: 'ONE_DAY', pnl: '100', currentEquity: '10000', @@ -876,7 +882,7 @@ export const defaultLeaderboardPnl2OneDay: LeaderboardPnlCreateObject = { }; export const defaultLeaderboardPnl1AllTime: LeaderboardPnlCreateObject = { - subaccountId: defaultSubaccountId, + address: defaultAddress, timeSpan: 'ALL_TIME', pnl: '10000', currentEquity: '1000', @@ -884,7 +890,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..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 () => { @@ -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), @@ -54,7 +57,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..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.uuid('subaccountId').notNullable().references('id').inTable('subaccounts'); + table.string('address').notNullable().references('address').inTable('wallets'); 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..60e54aaa02 100644 --- a/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts +++ b/indexer/packages/postgres/src/models/leaderboard-pnl-model.ts @@ -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', }, }, }; @@ -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 }, @@ -47,7 +47,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[]; }