From 7db0ebf0e4f0f5cd13fa61a86de532f1cc07f542 Mon Sep 17 00:00:00 2001 From: Christopher-Li Date: Thu, 9 Nov 2023 16:31:25 -0500 Subject: [PATCH] [IND-485]: Query for PerpetualPositions by openEventId (#777) * [IND-485]: Query for PerpetualPositions by openEventId * fix bugs --- .../postgres/__tests__/helpers/constants.ts | 10 +++++ .../__tests__/helpers/mock-generators.ts | 2 + .../stores/perpetual-position-table.test.ts | 38 +++++++++++++++++++ .../src/stores/perpetual-position-table.ts | 2 +- .../order-fills/liquidation-handler.test.ts | 9 ++--- .../order-fills/order-handler.test.ts | 17 +++------ ...ydx_liquidation_fill_handler_per_order.sql | 2 +- ...te_perpetual_position_aggregate_fields.sql | 3 +- 8 files changed, 63 insertions(+), 20 deletions(-) diff --git a/indexer/packages/postgres/__tests__/helpers/constants.ts b/indexer/packages/postgres/__tests__/helpers/constants.ts index f873c46d64..b9c5361743 100644 --- a/indexer/packages/postgres/__tests__/helpers/constants.ts +++ b/indexer/packages/postgres/__tests__/helpers/constants.ts @@ -287,6 +287,11 @@ export const defaultTendermintEvent3: TendermintEventCreateObject = { transactionIndex: 0, eventIndex: 0, }; +export const defaultTendermintEvent4: TendermintEventCreateObject = { + blockHeight: '2', + transactionIndex: 1, + eventIndex: 1, +}; export const defaultTendermintEventId: Buffer = TendermintEventTable.createEventId( defaultTendermintEvent.blockHeight, defaultTendermintEvent.transactionIndex, @@ -302,6 +307,11 @@ export const defaultTendermintEventId3: Buffer = TendermintEventTable.createEven defaultTendermintEvent3.transactionIndex, defaultTendermintEvent3.eventIndex, ); +export const defaultTendermintEventId4: Buffer = TendermintEventTable.createEventId( + defaultTendermintEvent4.blockHeight, + defaultTendermintEvent4.transactionIndex, + defaultTendermintEvent4.eventIndex, +); // ============== Transactions ============== diff --git a/indexer/packages/postgres/__tests__/helpers/mock-generators.ts b/indexer/packages/postgres/__tests__/helpers/mock-generators.ts index e8526066e1..e5ca093b32 100644 --- a/indexer/packages/postgres/__tests__/helpers/mock-generators.ts +++ b/indexer/packages/postgres/__tests__/helpers/mock-generators.ts @@ -24,6 +24,7 @@ import { defaultTendermintEvent, defaultTendermintEvent2, defaultTendermintEvent3, + defaultTendermintEvent4, } from './constants'; export async function seedData() { @@ -53,6 +54,7 @@ export async function seedData() { TendermintEventTable.create(defaultTendermintEvent), TendermintEventTable.create(defaultTendermintEvent2), TendermintEventTable.create(defaultTendermintEvent3), + TendermintEventTable.create(defaultTendermintEvent4), ]); await Promise.all([ AssetTable.create(defaultAsset), diff --git a/indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts b/indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts index a08f2f606a..68c2b3153d 100644 --- a/indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts +++ b/indexer/packages/postgres/__tests__/stores/perpetual-position-table.test.ts @@ -12,6 +12,7 @@ import { PerpetualPositionSubaccountUpdateObject, PositionSide, SubaccountToPerpetualPositionsMap, + TendermintEventCreateObject, } from '../../src/types'; import * as PerpetualPositionTable from '../../src/stores/perpetual-position-table'; import * as PerpetualMarketTable from '../../src/stores/perpetual-market-table'; @@ -26,12 +27,14 @@ import { defaultPerpetualPosition, defaultSubaccountId, defaultSubaccountId2, + defaultTendermintEvent3, defaultTendermintEventId, defaultTendermintEventId2, defaultTendermintEventId3, } from '../helpers/constants'; import { checkLengthAndContains } from './helpers'; import _ from 'lodash'; +import { TendermintEventTable } from '../../src'; describe('PerpetualPosition store', () => { beforeAll(async () => { @@ -185,6 +188,41 @@ describe('PerpetualPosition store', () => { checkLengthAndContains(perpetualPositions, expectedLength, expectedPosition); }); + it('Successfully finds PerpetualPositions sorted by openEventId', async () => { + const earlierPosition: PerpetualPositionCreateObject = { + ...defaultPerpetualPosition, + openEventId: defaultTendermintEventId3, + lastEventId: defaultTendermintEventId3, + }; + const nextTendermintEvent: TendermintEventCreateObject = { + blockHeight: defaultTendermintEvent3.blockHeight, + transactionIndex: defaultTendermintEvent3.transactionIndex, + eventIndex: defaultTendermintEvent3.eventIndex + 1, + }; + const nextTendermintEventId: Buffer = TendermintEventTable.createEventId( + nextTendermintEvent.blockHeight, + nextTendermintEvent.transactionIndex, + nextTendermintEvent.eventIndex, + ); + const laterPosition: PerpetualPositionCreateObject = { + ...defaultPerpetualPosition, + openEventId: nextTendermintEventId, + lastEventId: nextTendermintEventId, + }; + await TendermintEventTable.create(nextTendermintEvent); + await Promise.all([ + await PerpetualPositionTable.create(earlierPosition), + await PerpetualPositionTable.create(laterPosition), + ]); + + const perpetualPositions: PerpetualPositionFromDatabase[] = await + PerpetualPositionTable.findAll({}, [], { readReplica: true }); + + expect(perpetualPositions.length).toEqual(2); + expect(perpetualPositions[0]).toEqual(expect.objectContaining(laterPosition)); + expect(perpetualPositions[1]).toEqual(expect.objectContaining(earlierPosition)); + }); + it.each([ [1, 1, defaultPerpetualPosition], [-1, 0, undefined], diff --git a/indexer/packages/postgres/src/stores/perpetual-position-table.ts b/indexer/packages/postgres/src/stores/perpetual-position-table.ts index 29657a10d9..98268e4f9a 100644 --- a/indexer/packages/postgres/src/stores/perpetual-position-table.ts +++ b/indexer/packages/postgres/src/stores/perpetual-position-table.ts @@ -124,7 +124,7 @@ export async function findAll( PerpetualPositionColumns.subaccountId, Ordering.ASC, ).orderBy( - PerpetualPositionColumns.createdAtHeight, + PerpetualPositionColumns.openEventId, Ordering.DESC, ); } diff --git a/indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts b/indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts index b7ab1b37bf..ba14a7d10e 100644 --- a/indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/order-fills/liquidation-handler.test.ts @@ -141,8 +141,8 @@ describe('LiquidationHandler', () => { entryPrice: '15000', createdAt: DateTime.utc().toISO(), createdAtHeight: '1', - openEventId: testConstants.defaultTendermintEventId, - lastEventId: testConstants.defaultTendermintEventId, + openEventId: testConstants.defaultTendermintEventId4, + lastEventId: testConstants.defaultTendermintEventId4, settledFunding: '200000', }; @@ -234,7 +234,7 @@ describe('LiquidationHandler', () => { ], ])( 'creates fills and orders (with %s), sends vulcan message for maker order update and updates ' + - ' perpetualPosition', + 'perpetualPosition', async ( _name: string, goodTilOneof: Partial, @@ -297,8 +297,7 @@ describe('LiquidationHandler', () => { // older perpetual position to ensure that the correct perpetual position is being updated PerpetualPositionTable.create({ ...defaultPerpetualPosition, - createdAtHeight: '0', - openEventId: testConstants.defaultTendermintEventId2, + openEventId: testConstants.defaultTendermintEventId, }), ]); diff --git a/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts b/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts index e8f49aeab4..415fad9855 100644 --- a/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts +++ b/indexer/services/ender/__tests__/handlers/order-fills/order-handler.test.ts @@ -143,8 +143,8 @@ describe('OrderHandler', () => { entryPrice: '15000', createdAt: DateTime.utc().toISO(), createdAtHeight: '10', - openEventId: testConstants.defaultTendermintEventId, - lastEventId: testConstants.defaultTendermintEventId, + openEventId: testConstants.defaultTendermintEventId4, + lastEventId: testConstants.defaultTendermintEventId4, settledFunding: '200000', }; @@ -314,7 +314,6 @@ describe('OrderHandler', () => { // older perpetual position to ensure that the correct perpetual position is being updated PerpetualPositionTable.create({ ...defaultPerpetualPosition, - createdAtHeight: '0', openEventId: testConstants.defaultTendermintEventId2, }), ]); @@ -915,19 +914,17 @@ describe('OrderHandler', () => { // previous position for subaccount 1 PerpetualPositionTable.create({ ...defaultPerpetualPosition, - createdAtHeight: '1', size: '0', status: PerpetualPositionStatus.CLOSED, - openEventId: testConstants.defaultTendermintEventId2, + openEventId: testConstants.defaultTendermintEventId, }), // previous position for subaccount 2 PerpetualPositionTable.create({ ...defaultPerpetualPosition, subaccountId: testConstants.defaultSubaccountId2, - createdAtHeight: '1', size: '0', status: PerpetualPositionStatus.CLOSED, - openEventId: testConstants.defaultTendermintEventId2, + openEventId: testConstants.defaultTendermintEventId, }), // initial position for subaccount 2 PerpetualPositionTable.create(defaultPerpetualPosition), @@ -1129,20 +1126,18 @@ describe('OrderHandler', () => { PerpetualPositionTable.create({ ...defaultPerpetualPosition, perpetualId: testConstants.defaultPerpetualMarket3.id, - createdAtHeight: '1', size: '0', status: PerpetualPositionStatus.CLOSED, - openEventId: testConstants.defaultTendermintEventId2, + openEventId: testConstants.defaultTendermintEventId, }), // previous position for subaccount 2 PerpetualPositionTable.create({ ...defaultPerpetualPosition, perpetualId: testConstants.defaultPerpetualMarket3.id, subaccountId: testConstants.defaultSubaccountId2, - createdAtHeight: '1', size: '0', status: PerpetualPositionStatus.CLOSED, - openEventId: testConstants.defaultTendermintEventId2, + openEventId: testConstants.defaultTendermintEventId, }), // initial position for subaccount 2 PerpetualPositionTable.create({ diff --git a/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql b/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql index 745edda0b0..dff6e0e518 100644 --- a/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql +++ b/indexer/services/ender/src/scripts/dydx_liquidation_fill_handler_per_order.sql @@ -182,7 +182,7 @@ BEGIN /* Upsert the perpetual_position record for this order_fill event. */ SELECT * INTO perpetual_position_record FROM perpetual_positions WHERE "subaccountId" = subaccount_uuid AND "perpetualId" = perpetual_market_record."id" - ORDER BY "createdAtHeight" DESC; + ORDER BY "openEventId" DESC; IF NOT FOUND THEN RAISE EXCEPTION 'Unable to find existing perpetual position, subaccountId: %, perpetualId: %', subaccount_uuid, perpetual_market_record."id"; END IF; diff --git a/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql b/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql index 3265c73d89..6f314eeae8 100644 --- a/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql +++ b/indexer/services/ender/src/scripts/dydx_update_perpetual_position_aggregate_fields.sql @@ -22,12 +22,11 @@ DECLARE exit_price numeric; BEGIN -- Retrieve the latest perpetual position record. - -- TODO(IND-485): Order by openEventId instead of createdAtHeight. SELECT * INTO perpetual_position_record FROM perpetual_positions WHERE "subaccountId" = subaccount_uuid AND "perpetualId" = perpetual_id - ORDER BY "createdAtHeight" DESC + ORDER BY "openEventId" DESC LIMIT 1; IF NOT FOUND THEN