From 301124523e66748b1704bdc7fe4999d5abd20fbf Mon Sep 17 00:00:00 2001 From: Shrenuj Bansal Date: Thu, 28 Mar 2024 14:56:07 -0400 Subject: [PATCH] Add subaccountNumber to PerpetualPositionResponseObject Signed-off-by: Shrenuj Bansal --- .../api/v4/addresses-controller.test.ts | 3 ++ .../v4/perpetual-positions-controller.test.ts | 3 ++ .../comlink/public/api-documentation.md | 52 +++++++++++++------ indexer/services/comlink/public/swagger.json | 7 ++- .../api/v4/addresses-controller.ts | 1 + .../api/v4/perpetual-positions-controller.ts | 12 ++++- .../request-helpers/request-transformer.ts | 2 + indexer/services/comlink/src/types.ts | 1 + 8 files changed, 61 insertions(+), 20 deletions(-) diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/addresses-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/addresses-controller.test.ts index 119d085d40..61961fd461 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/addresses-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/addresses-controller.test.ts @@ -106,6 +106,7 @@ describe('addresses-controller#V4', () => { createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight, exitPrice: null, closedAt: null, + subaccountNumber: testConstants.defaultSubaccount.subaccountNumber, }, }, assetPositions: { @@ -266,6 +267,7 @@ describe('addresses-controller#V4', () => { createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight, exitPrice: null, closedAt: null, + subaccountNumber: testConstants.defaultSubaccount.subaccountNumber, }, }, assetPositions: { @@ -453,6 +455,7 @@ describe('addresses-controller#V4', () => { createdAtHeight: testConstants.defaultPerpetualPosition.createdAtHeight, exitPrice: null, closedAt: null, + subaccountNumber: testConstants.defaultSubaccount.subaccountNumber, }, }, assetPositions: { diff --git a/indexer/services/comlink/__tests__/controllers/api/v4/perpetual-positions-controller.test.ts b/indexer/services/comlink/__tests__/controllers/api/v4/perpetual-positions-controller.test.ts index 22dc791af3..c60f47b979 100644 --- a/indexer/services/comlink/__tests__/controllers/api/v4/perpetual-positions-controller.test.ts +++ b/indexer/services/comlink/__tests__/controllers/api/v4/perpetual-positions-controller.test.ts @@ -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( @@ -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( @@ -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( diff --git a/indexer/services/comlink/public/api-documentation.md b/indexer/services/comlink/public/api-documentation.md index f3dd667640..590e3c9b08 100644 --- a/indexer/services/comlink/public/api-documentation.md +++ b/indexer/services/comlink/public/api-documentation.md @@ -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", @@ -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": { @@ -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", @@ -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": { @@ -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", @@ -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": { @@ -1838,7 +1844,8 @@ fetch('https://dydx-testnet.imperator.co/v4/perpetualPositions?address=string&su "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } ] } @@ -2348,7 +2355,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } ``` @@ -2372,6 +2380,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 @@ -2397,7 +2406,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 }, "property2": { "market": "string", @@ -2414,7 +2424,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } } @@ -2516,7 +2527,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 }, "property2": { "market": "string", @@ -2533,7 +2545,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } }, "assetPositions": { @@ -2600,7 +2613,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": null, - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 }, "property2": { "market": "string", @@ -2617,7 +2631,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": null, - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } }, "assetPositions": { @@ -2686,7 +2701,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": null, - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 }, "property2": { "market": "string", @@ -2703,7 +2719,8 @@ This operation does not require authentication "netFunding": "string", "unrealizedPnl": "string", "closedAt": null, - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } }, "assetPositions": { @@ -3856,7 +3873,8 @@ or "netFunding": "string", "unrealizedPnl": "string", "closedAt": "string", - "exitPrice": "string" + "exitPrice": "string", + "subaccountNumber": 0 } ] } diff --git a/indexer/services/comlink/public/swagger.json b/indexer/services/comlink/public/swagger.json index cf3507be2c..a6747eaedb 100644 --- a/indexer/services/comlink/public/swagger.json +++ b/indexer/services/comlink/public/swagger.json @@ -76,6 +76,10 @@ "exitPrice": { "type": "string", "nullable": true + }, + "subaccountNumber": { + "type": "number", + "format": "double" } }, "required": [ @@ -91,7 +95,8 @@ "sumOpen", "sumClose", "netFunding", - "unrealizedPnl" + "unrealizedPnl", + "subaccountNumber" ], "type": "object", "additionalProperties": false diff --git a/indexer/services/comlink/src/controllers/api/v4/addresses-controller.ts b/indexer/services/comlink/src/controllers/api/v4/addresses-controller.ts index cd5b1f77e9..23ee3f280e 100644 --- a/indexer/services/comlink/src/controllers/api/v4/addresses-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/addresses-controller.ts @@ -509,6 +509,7 @@ async function getSubaccountResponse( perpetualPosition, perpetualMarketsMap, marketIdToMarket, + subaccount.subaccountNumber, ); }, ); diff --git a/indexer/services/comlink/src/controllers/api/v4/perpetual-positions-controller.ts b/indexer/services/comlink/src/controllers/api/v4/perpetual-positions-controller.ts index 8923e2880a..f54c961f92 100644 --- a/indexer/services/comlink/src/controllers/api/v4/perpetual-positions-controller.ts +++ b/indexer/services/comlink/src/controllers/api/v4/perpetual-positions-controller.ts @@ -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, + ); }), }; } @@ -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, diff --git a/indexer/services/comlink/src/request-helpers/request-transformer.ts b/indexer/services/comlink/src/request-helpers/request-transformer.ts index 3eea8dc703..0ca1a79bb7 100644 --- a/indexer/services/comlink/src/request-helpers/request-transformer.ts +++ b/indexer/services/comlink/src/request-helpers/request-transformer.ts @@ -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 @@ -104,6 +105,7 @@ export function perpetualPositionToResponseObject( sumOpen: position.sumOpen, sumClose: position.sumClose, netFunding: netFunding.toFixed(), + subaccountNumber, }; } diff --git a/indexer/services/comlink/src/types.ts b/indexer/services/comlink/src/types.ts index 83afcd18a7..5944257bfa 100644 --- a/indexer/services/comlink/src/types.ts +++ b/indexer/services/comlink/src/types.ts @@ -100,6 +100,7 @@ export interface PerpetualPositionResponseObject { unrealizedPnl: string; closedAt?: IsoString | null; exitPrice?: string | null; + subaccountNumber: number; } export type PerpetualPositionsMap = { [market: string]: PerpetualPositionResponseObject };