Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IND-485]: Query for PerpetualPositions by openEventId #777

Merged
merged 2 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions indexer/packages/postgres/__tests__/helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 ==============

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
defaultTendermintEvent,
defaultTendermintEvent2,
defaultTendermintEvent3,
defaultTendermintEvent4,
} from './constants';

export async function seedData() {
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export async function findAll(
PerpetualPositionColumns.subaccountId,
Ordering.ASC,
).orderBy(
PerpetualPositionColumns.createdAtHeight,
PerpetualPositionColumns.openEventId,
Ordering.DESC,
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

Expand Down Expand Up @@ -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<IndexerOrder>,
Expand Down Expand Up @@ -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,
}),
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
};

Expand Down Expand Up @@ -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,
}),
]);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down