Skip to content

Commit

Permalink
Add subaccountNumber to PerpetualPositionResponseObject (#1274)
Browse files Browse the repository at this point in the history
Signed-off-by: Shrenuj Bansal <[email protected]>
  • Loading branch information
shrenujb authored Mar 28, 2024
1 parent 24259f0 commit e0b5c29
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ describe('addresses-controller#V4', () => {
createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight,
exitPrice: null,
closedAt: null,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
},
},
assetPositions: {
Expand Down Expand Up @@ -266,6 +267,7 @@ describe('addresses-controller#V4', () => {
createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight,
exitPrice: null,
closedAt: null,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
},
},
assetPositions: {
Expand Down Expand Up @@ -453,6 +455,7 @@ describe('addresses-controller#V4', () => {
createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight,
exitPrice: null,
closedAt: null,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
},
},
assetPositions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('perpetual-positions-controller#V4', () => {
createdAt: testConstants.createdDateTime.toISO(),
closedAt: null,
createdAtHeight: testConstants.createdHeight,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
};

expect(response.body.positions).toEqual(
Expand Down Expand Up @@ -140,6 +141,7 @@ describe('perpetual-positions-controller#V4', () => {
createdAt: testConstants.createdDateTime.toISO(),
closedAt: null,
createdAtHeight: testConstants.createdHeight,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
};

expect(response.body.positions).toEqual(
Expand Down Expand Up @@ -189,6 +191,7 @@ describe('perpetual-positions-controller#V4', () => {
createdAt: testConstants.createdDateTime.toISO(),
closedAt: null,
createdAtHeight: testConstants.createdHeight,
subaccountNumber: testConstants.defaultSubaccount.subaccountNumber,
};

expect(response.body.positions).toEqual(
Expand Down
52 changes: 35 additions & 17 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}',
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -102,7 +103,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}',
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -211,7 +213,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}/subaccountNumber
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -228,7 +231,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}/subaccountNumber
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -340,7 +344,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}/parentSubaccount
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -357,7 +362,8 @@ fetch('https://dydx-testnet.imperator.co/v4/addresses/{address}/parentSubaccount
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -1840,7 +1846,8 @@ fetch('https://dydx-testnet.imperator.co/v4/perpetualPositions?address=string&su
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
]
}
Expand Down Expand Up @@ -2350,7 +2357,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}

```
Expand All @@ -2374,6 +2382,7 @@ This operation does not require authentication
|unrealizedPnl|string|true|none|none|
|closedAt|[IsoString](#schemaisostring)¦null|false|none|none|
|exitPrice|string¦null|false|none|none|
|subaccountNumber|number(double)|true|none|none|

## PerpetualPositionsMap

Expand All @@ -2399,7 +2408,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -2416,7 +2426,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
}

Expand Down Expand Up @@ -2518,7 +2529,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -2535,7 +2547,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -2602,7 +2615,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -2619,7 +2633,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -2688,7 +2703,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
},
"property2": {
"market": "string",
Expand All @@ -2705,7 +2721,8 @@ This operation does not require authentication
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": null,
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
},
"assetPositions": {
Expand Down Expand Up @@ -3887,7 +3904,8 @@ or
"netFunding": "string",
"unrealizedPnl": "string",
"closedAt": "string",
"exitPrice": "string"
"exitPrice": "string",
"subaccountNumber": 0
}
]
}
Expand Down
7 changes: 6 additions & 1 deletion indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@
"exitPrice": {
"type": "string",
"nullable": true
},
"subaccountNumber": {
"type": "number",
"format": "double"
}
},
"required": [
Expand All @@ -91,7 +95,8 @@
"sumOpen",
"sumClose",
"netFunding",
"unrealizedPnl"
"unrealizedPnl",
"subaccountNumber"
],
"type": "object",
"additionalProperties": false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,7 @@ async function getSubaccountResponse(
perpetualPosition,
perpetualMarketsMap,
marketIdToMarket,
subaccount.subaccountNumber,
);
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,12 @@ class PerpetualPositionsController extends Controller {

return {
positions: updatedPerpetualPositions.map((position: PerpetualPositionWithFunding) => {
return perpetualPositionToResponseObject(position, perpetualMarketsMap, marketIdToMarket);
return perpetualPositionToResponseObject(
position,
perpetualMarketsMap,
marketIdToMarket,
subaccountNumber,
);
}),
};
}
Expand Down Expand Up @@ -179,11 +184,14 @@ router.get(
createdBeforeOrAt,
}: PerpetualPositionRequest = matchedData(req) as PerpetualPositionRequest;

// The schema checks allow subaccountNumber to be a string, but we know it's a number here.
const subaccountNum: number = +subaccountNumber;

try {
const controller: PerpetualPositionsController = new PerpetualPositionsController();
const response: PerpetualPositionResponse = await controller.listPositions(
address,
subaccountNumber,
subaccountNum,
status,
limit,
createdBeforeOrAtHeight,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export function perpetualPositionToResponseObject(
position: PerpetualPositionWithFunding,
perpetualMarketsMap: PerpetualMarketsMap,
marketsMap: MarketsMap,
subaccountNumber: number,
): PerpetualPositionResponseObject {
// Realized pnl is calculated from the difference in price between the average entry/exit price
// (order depending on side of the position) multiplied by amount of the position that was closed
Expand Down Expand Up @@ -104,6 +105,7 @@ export function perpetualPositionToResponseObject(
sumOpen: position.sumOpen,
sumClose: position.sumClose,
netFunding: netFunding.toFixed(),
subaccountNumber,
};
}

Expand Down
1 change: 1 addition & 0 deletions indexer/services/comlink/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export interface PerpetualPositionResponseObject {
unrealizedPnl: string;
closedAt?: IsoString | null;
exitPrice?: string | null;
subaccountNumber: number;
}

export type PerpetualPositionsMap = { [market: string]: PerpetualPositionResponseObject };
Expand Down

0 comments on commit e0b5c29

Please sign in to comment.