Skip to content

Commit c958927

Browse files
mergify[bot]pp346UnbornAztecKing
authored
Feature/closed pnl fill (backport #3168) (#3289)
Co-authored-by: pp346 <kevin@dydx.exchange> Co-authored-by: unbornaztecking <21340974+UnbornAztecKing@users.noreply.github.com>
1 parent 3bb87aa commit c958927

File tree

20 files changed

+461
-51
lines changed

20 files changed

+461
-51
lines changed

indexer/packages/postgres/__tests__/helpers/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ export const defaultPerpetualPosition: PerpetualPositionCreateObject = {
542542
openEventId: defaultTendermintEventId,
543543
lastEventId: defaultTendermintEventId2,
544544
settledFunding: '200000',
545+
totalRealizedPnl: '100',
545546
};
546547

547548
export const defaultPerpetualPositionId: string = PerpetualPositionTable.uuid(
@@ -564,6 +565,7 @@ export const isolatedPerpetualPosition: PerpetualPositionCreateObject = {
564565
openEventId: defaultTendermintEventId,
565566
lastEventId: defaultTendermintEventId2,
566567
settledFunding: '200000',
568+
totalRealizedPnl: '100',
567569
};
568570

569571
export const isolatedPerpetualPositionId: string = PerpetualPositionTable.uuid(
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import * as Knex from 'knex';
2+
3+
export async function up(knex: Knex): Promise<void> {
4+
await knex.schema.alterTable('perpetual_positions', (table) => {
5+
table.decimal('totalRealizedPnl', null).nullable();
6+
});
7+
8+
await knex.schema.alterTable('fills', (table) => {
9+
table.decimal('positionSizeBefore', null).nullable();
10+
table.decimal('entryPriceBefore', null).nullable();
11+
table.enum('positionSideBefore', [
12+
'LONG',
13+
'SHORT',
14+
]).nullable().defaultTo(null);
15+
});
16+
}
17+
18+
export async function down(knex: Knex): Promise<void> {
19+
await knex.schema.alterTable('perpetual_positions', (table) => {
20+
table.dropColumn('totalRealizedPnl');
21+
});
22+
23+
await knex.schema.alterTable('fills', (table) => {
24+
table.dropColumn('positionSizeBefore');
25+
table.dropColumn('entryPriceBefore');
26+
table.dropColumn('positionSideBefore');
27+
});
28+
}

indexer/packages/postgres/src/models/fill-model.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
IsoString,
1313
Liquidity,
1414
OrderSide,
15+
PositionSide,
1516
} from '../types';
1617

1718
export default class FillModel extends Model {
@@ -92,6 +93,9 @@ export default class FillModel extends Model {
9293
builderAddress: { type: ['string', 'null'], default: null },
9394
orderRouterAddress: { type: ['string', 'null'], default: null },
9495
orderRouterFee: { type: ['string', 'null'], default: null },
96+
positionSizeBefore: { type: ['string', 'null'], pattern: NumericPattern, default: null },
97+
entryPriceBefore: { type: ['string', 'null'], pattern: NonNegativeNumericPattern, default: null },
98+
positionSideBefore: { type: ['string', 'null'], enum: [...Object.values(PositionSide), null], default: null },
9599
},
96100
};
97101
}
@@ -125,6 +129,9 @@ export default class FillModel extends Model {
125129
builderAddress: 'string',
126130
orderRouterAddress: 'string',
127131
orderRouterFee: 'string',
132+
positionSizeBefore: 'string',
133+
entryPriceBefore: 'string',
134+
positionSideBefore: 'string',
128135
};
129136
}
130137

@@ -169,4 +176,10 @@ export default class FillModel extends Model {
169176
orderRouterAddress!: string;
170177

171178
orderRouterFee!: string;
179+
180+
positionSizeBefore?: string;
181+
182+
entryPriceBefore?: string;
183+
184+
positionSideBefore?: PositionSide;
172185
}

indexer/packages/postgres/src/models/perpetual-position-model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ export default class PerpetualPositionModel extends Model {
9797
createdAtHeight: { type: 'string', pattern: IntegerPattern },
9898
closedAtHeight: { type: ['string', 'null'], default: null, pattern: IntegerPattern },
9999
settledFunding: { type: 'string', pattern: NumericPattern },
100+
totalRealizedPnl: { type: ['string', 'null'], default: null, pattern: NumericPattern },
100101
},
101102
};
102103
}
@@ -128,6 +129,7 @@ export default class PerpetualPositionModel extends Model {
128129
closeEventId: 'hex-string',
129130
lastEventId: 'hex-string',
130131
settledFunding: 'string',
132+
totalRealizedPnl: 'string',
131133
};
132134
}
133135

@@ -168,4 +170,6 @@ export default class PerpetualPositionModel extends Model {
168170
lastEventId!: Buffer;
169171

170172
settledFunding!: string;
173+
174+
totalRealizedPnl?: string;
171175
}

indexer/packages/postgres/src/types/db-model-types.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export interface PerpetualPositionFromDatabase extends IdBasedModelFromDatabase
5252
closeEventId?: Buffer,
5353
lastEventId: Buffer,
5454
settledFunding: string,
55+
totalRealizedPnl?: string,
5556
}
5657

5758
export interface OrderFromDatabase extends IdBasedModelFromDatabase {
@@ -127,6 +128,9 @@ export interface FillFromDatabase {
127128
builderFee?: string,
128129
orderRouterAddress?: string,
129130
orderRouterFee?: string,
131+
positionSizeBefore?: string,
132+
entryPriceBefore?: string,
133+
positionSideBefore?: PositionSide,
130134
}
131135

132136
export interface BlockFromDatabase {

indexer/packages/postgres/src/types/perpetual-position-types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ export interface PerpetualPositionCreateObject {
3434
closedAtHeight?: string,
3535
closeEventId?: Buffer,
3636
exitPrice?: string,
37+
totalRealizedPnl?: string,
3738
}
3839

3940
export interface PerpetualPositionUpdateObject {
@@ -53,6 +54,7 @@ export interface PerpetualPositionUpdateObject {
5354
closeEventId?: Buffer | null,
5455
lastEventId?: Buffer,
5556
settledFunding?: string,
57+
totalRealizedPnl?: string,
5658
}
5759

5860
// Object used to update a subaccount's perpetual position in the SubaccountUpdateHandler

indexer/services/comlink/__tests__/controllers/api/v4/addresses-controller.test.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ describe('addresses-controller#V4', () => {
122122
maxSize: testConstants.defaultPerpetualPosition.maxSize,
123123
// 200000 + 10*(10000-10050)=199500
124124
netFunding: getFixedRepresentation('199500'),
125-
// sumClose=0, so realized Pnl is the same as the net funding of the position.
126-
// Unsettled funding is funding payments that already "happened" but not reflected
127-
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
128-
realizedPnl: getFixedRepresentation('199500'),
125+
realizedPnl: getFixedRepresentation('100'),
129126
// size * (index-entry) = 10*(15000-20000) = -50000
130127
unrealizedPnl: getFixedRepresentation(-50000),
131128
status: testConstants.defaultPerpetualPosition.status,
@@ -290,7 +287,7 @@ describe('addresses-controller#V4', () => {
290287
// sumClose=0, so realized Pnl is the same as the net funding of the position.
291288
// Unsettled funding is funding payments that already "happened" but not reflected
292289
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
293-
realizedPnl: getFixedRepresentation('199500'),
290+
realizedPnl: getFixedRepresentation('100'),
294291
// size * (index-entry) = 10*(15000-20000) = -50000
295292
unrealizedPnl: getFixedRepresentation(-50000),
296293
status: testConstants.defaultPerpetualPosition.status,
@@ -483,7 +480,7 @@ describe('addresses-controller#V4', () => {
483480
// sumClose=0, so realized Pnl is the same as the net funding of the position.
484481
// Unsettled funding is funding payments that already "happened" but not reflected
485482
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
486-
realizedPnl: getFixedRepresentation('199500'),
483+
realizedPnl: getFixedRepresentation('100'),
487484
// size * (index-entry) = 10*(15000-20000) = -50000
488485
unrealizedPnl: getFixedRepresentation(-50000),
489486
status: testConstants.defaultPerpetualPosition.status,

indexer/services/comlink/__tests__/controllers/api/v4/perpetual-positions-controller.test.ts

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('perpetual-positions-controller#V4', () => {
8080
// sumClose=0, so realized Pnl is the same as the net funding of the position.
8181
// Unsettled funding is funding payments that already "happened" but not reflected
8282
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
83-
realizedPnl: getFixedRepresentation('199500'),
83+
realizedPnl: getFixedRepresentation('100'),
8484
// For the calculation of the unrealized pnl (long position):
8585
// index price = 15_000, entry price = 20_000, size = 10
8686
// unrealizedPnl = size * (index price - entry price)
@@ -132,7 +132,7 @@ describe('perpetual-positions-controller#V4', () => {
132132
// sumClose=0, so realized Pnl is the same as the net funding of the position.
133133
// Unsettled funding is funding payments that already "happened" but not reflected
134134
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
135-
realizedPnl: getFixedRepresentation('200500'),
135+
realizedPnl: getFixedRepresentation('100'),
136136
// For the calculation of the unrealized pnl (short position):
137137
// index price = 15_000, entry price = 20_000, size = -10
138138
// unrealizedPnl = size * (index price - entry price)
@@ -180,9 +180,7 @@ describe('perpetual-positions-controller#V4', () => {
180180
netFunding: getFixedRepresentation(
181181
testConstants.defaultPerpetualPosition.settledFunding,
182182
),
183-
realizedPnl: getFixedRepresentation(
184-
testConstants.defaultPerpetualPosition.settledFunding,
185-
),
183+
realizedPnl: getFixedRepresentation('100'),
186184
// For the calculation of the unrealized pnl (short position):
187185
// index price = 15_000, entry price = 20_000, size = 10
188186
// unrealizedPnl = size * (index price - entry price)
@@ -293,7 +291,7 @@ describe('perpetual-positions-controller#V4', () => {
293291
// sumClose=0, so realized Pnl is the same as the net funding of the position.
294292
// Unsettled funding is funding payments that already "happened" but not reflected
295293
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
296-
realizedPnl: getFixedRepresentation('199500'),
294+
realizedPnl: getFixedRepresentation('100'),
297295
// For the calculation of the unrealized pnl (long position):
298296
// index price = 15_000, entry price = 20_000, size = 10
299297
// unrealizedPnl = size * (index price - entry price)
@@ -324,7 +322,7 @@ describe('perpetual-positions-controller#V4', () => {
324322
// sumClose=0, so realized Pnl is the same as the net funding of the position.
325323
// Unsettled funding is funding payments that already "happened" but not reflected
326324
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
327-
realizedPnl: getFixedRepresentation('202000'),
325+
realizedPnl: getFixedRepresentation('100'),
328326
// For the calculation of the unrealized pnl (short position):
329327
// index price = 1, entry price = 1.5, size = -10
330328
// unrealizedPnl = size * (index price - entry price)
@@ -382,9 +380,7 @@ describe('perpetual-positions-controller#V4', () => {
382380
netFunding: getFixedRepresentation(
383381
testConstants.defaultPerpetualPosition.settledFunding,
384382
),
385-
realizedPnl: getFixedRepresentation(
386-
testConstants.defaultPerpetualPosition.settledFunding,
387-
),
383+
realizedPnl: getFixedRepresentation('100'),
388384
// For the calculation of the unrealized pnl (short position):
389385
// index price = 15_000, entry price = 20_000, size = 10
390386
// unrealizedPnl = size * (index price - entry price)
@@ -409,9 +405,7 @@ describe('perpetual-positions-controller#V4', () => {
409405
netFunding: getFixedRepresentation(
410406
testConstants.isolatedPerpetualPosition.settledFunding,
411407
),
412-
realizedPnl: getFixedRepresentation(
413-
testConstants.isolatedPerpetualPosition.settledFunding,
414-
),
408+
realizedPnl: getFixedRepresentation('100'),
415409
// For the calculation of the unrealized pnl (short position):
416410
// index price = 1, entry price = 1.5, size = -10
417411
// unrealizedPnl = size * (index price - entry price)

indexer/services/comlink/__tests__/controllers/api/v4/vault-controller.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ describe('vault-controller#V4', () => {
459459
// sumClose=0, so realized Pnl is the same as the net funding of the position.
460460
// Unsettled funding is funding payments that already "happened" but not reflected
461461
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
462-
realizedPnl: getFixedRepresentation('199500'),
462+
realizedPnl: getFixedRepresentation('100'),
463463
// size * (index-entry) = 10*(15000-20000) = -50000
464464
unrealizedPnl: getFixedRepresentation(-50000),
465465
status: testConstants.defaultPerpetualPosition.status,
@@ -521,7 +521,7 @@ describe('vault-controller#V4', () => {
521521
),
522522
maxSize: testConstants.defaultPerpetualPosition.maxSize,
523523
netFunding: getFixedRepresentation('199500'),
524-
realizedPnl: getFixedRepresentation('199500'),
524+
realizedPnl: getFixedRepresentation('100'),
525525
unrealizedPnl: getFixedRepresentation(-50000),
526526
status: testConstants.defaultPerpetualPosition.status,
527527
sumOpen: testConstants.defaultPerpetualPosition.sumOpen,
@@ -543,7 +543,6 @@ describe('vault-controller#V4', () => {
543543
},
544544
{
545545
equity: getFixedRepresentation(10000),
546-
perpetualPosition: undefined,
547546
assetPosition: {
548547
symbol: testConstants.defaultAsset.symbol,
549548
size: testConstants.defaultAssetPosition.size,

indexer/services/comlink/__tests__/lib/helpers.test.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,10 +807,7 @@ describe('helpers', () => {
807807
maxSize: testConstants.defaultPerpetualPosition.maxSize,
808808
// 200000 + 10*(10000-10050)=199500
809809
netFunding: getFixedRepresentation('199500'),
810-
// sumClose=0, so realized Pnl is the same as the net funding of the position.
811-
// Unsettled funding is funding payments that already "happened" but not reflected
812-
// in the subaccount's balance yet, so it's considered a part of realizedPnl.
813-
realizedPnl: getFixedRepresentation('199500'),
810+
realizedPnl: getFixedRepresentation('100'),
814811
// size * (index-entry) = 10*(15000-20000) = -50000
815812
unrealizedPnl: getFixedRepresentation(-50000),
816813
status: testConstants.defaultPerpetualPosition.status,

0 commit comments

Comments
 (0)