Skip to content

Commit

Permalink
[CT-629] Fix entryPrice calc (#2455)
Browse files Browse the repository at this point in the history
(cherry picked from commit c8f8a4a)
  • Loading branch information
dydxwill authored and mergify[bot] committed Oct 16, 2024
1 parent 8d165a9 commit fbf2021
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ describe('LiquidationHandler', () => {
perpetualId: testConstants.defaultPerpetualMarket.id,
side: PositionSide.LONG,
status: PerpetualPositionStatus.OPEN,
size: '10',
size: '5',
maxSize: '25',
sumOpen: '10',
entryPrice: '15000',
Expand Down Expand Up @@ -385,7 +385,7 @@ describe('LiquidationHandler', () => {
defaultPerpetualPosition.openEventId,
),
{
sumOpen: Big(defaultPerpetualPosition.size).plus(totalFilled).toFixed(),
sumOpen: Big(defaultPerpetualPosition.sumOpen!).plus(totalFilled).toFixed(),
entryPrice: getWeightedAverage(
defaultPerpetualPosition.entryPrice!,
defaultPerpetualPosition.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ describe('OrderHandler', () => {
perpetualId: testConstants.defaultPerpetualMarket.id,
side: PositionSide.LONG,
status: PerpetualPositionStatus.OPEN,
size: '10',
size: '5',
maxSize: '25',
sumOpen: '10',
entryPrice: '15000',
Expand Down Expand Up @@ -207,6 +207,7 @@ describe('OrderHandler', () => {
{
goodTilBlock: 15,
},
false,
],
[
'goodTilBlockTime',
Expand All @@ -216,6 +217,17 @@ describe('OrderHandler', () => {
{
goodTilBlockTime: 1_000_005_000,
},
false,
],
[
'goodTilBlock',
{
goodTilBlock: 10,
},
{
goodTilBlock: 15,
},
true,
],
])(
'creates fills and orders (with %s), sends vulcan messages for order updates and order ' +
Expand All @@ -224,6 +236,7 @@ describe('OrderHandler', () => {
_name: string,
makerGoodTilOneof: Partial<IndexerOrder>,
takerGoodTilOneof: Partial<IndexerOrder>,
useNegativeSize: boolean,
) => {
const transactionIndex: number = 0;
const eventIndex: number = 0;
Expand Down Expand Up @@ -279,7 +292,10 @@ describe('OrderHandler', () => {

// create PerpetualPositions
await Promise.all([
PerpetualPositionTable.create(defaultPerpetualPosition),
PerpetualPositionTable.create({
...defaultPerpetualPosition,
size: useNegativeSize ? '-5' : defaultPerpetualPosition.size,
}),
PerpetualPositionTable.create({
...defaultPerpetualPosition,
subaccountId: testConstants.defaultSubaccountId2,
Expand Down Expand Up @@ -432,7 +448,7 @@ describe('OrderHandler', () => {
defaultPerpetualPosition.openEventId,
),
{
sumOpen: Big(defaultPerpetualPosition.size).plus(totalFilled).toFixed(),
sumOpen: Big(defaultPerpetualPosition.sumOpen!).plus(totalFilled).toFixed(),
entryPrice: getWeightedAverage(
defaultPerpetualPosition.entryPrice!,
defaultPerpetualPosition.size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ BEGIN
perpetual_position_record."side", order_side) THEN
sum_open = dydx_trim_scale(perpetual_position_record."sumOpen" + fill_amount);
entry_price = dydx_get_weighted_average(
perpetual_position_record."entryPrice", perpetual_position_record."sumOpen",
perpetual_position_record."entryPrice", ABS(perpetual_position_record."size"),
maker_price, fill_amount);
perpetual_position_record."sumOpen" = sum_open;
perpetual_position_record."entryPrice" = entry_price;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ BEGIN
IF dydx_perpetual_position_and_order_side_matching(perpetual_position_record."side", side) THEN
sum_open := dydx_trim_scale(perpetual_position_record."sumOpen" + size);
entry_price := dydx_get_weighted_average(
perpetual_position_record."entryPrice", perpetual_position_record."sumOpen", price, size
perpetual_position_record."entryPrice", ABS(perpetual_position_record."size"), price, size
);
perpetual_position_record."sumOpen" = sum_open;
perpetual_position_record."entryPrice" = entry_price;
Expand Down

0 comments on commit fbf2021

Please sign in to comment.