Skip to content

Commit

Permalink
feat(comlink): ✨ add pagination for getTrades and getFills
Browse files Browse the repository at this point in the history
  • Loading branch information
DavideSegullo committed Mar 26, 2024
1 parent 4dac677 commit 635dcd6
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 5 deletions.
23 changes: 23 additions & 0 deletions indexer/services/comlink/public/api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ fetch('https://dydx-testnet.imperator.co/v4/fills?address=string&subaccountNumbe
|limit|query|number(double)|false|none|
|createdBeforeOrAtHeight|query|number(double)|false|none|
|createdBeforeOrAt|query|[IsoString](#schemaisostring)|false|none|
|page|query|number(double)|false|none|

#### Enumerated Values

Expand All @@ -702,6 +703,9 @@ fetch('https://dydx-testnet.imperator.co/v4/fills?address=string&subaccountNumbe
```json
{
"pageSize": 0,
"totalResults": 0,
"offset": 0,
"fills": [
{
"id": "string",
Expand Down Expand Up @@ -800,6 +804,9 @@ fetch('https://dydx-testnet.imperator.co/v4/fills/parentSubaccount?address=strin
```json
{
"pageSize": 0,
"totalResults": 0,
"offset": 0,
"fills": [
{
"id": "string",
Expand Down Expand Up @@ -1966,13 +1973,17 @@ fetch('https://dydx-testnet.imperator.co/v4/trades/perpetualMarket/{ticker}',
|limit|query|number(double)|false|none|
|createdBeforeOrAtHeight|query|number(double)|false|none|
|createdBeforeOrAt|query|[IsoString](#schemaisostring)|false|none|
|page|query|number(double)|false|none|

> Example responses
> 200 Response
```json
{
"pageSize": 0,
"totalResults": 0,
"offset": 0,
"trades": [
{
"id": "string",
Expand Down Expand Up @@ -2968,6 +2979,9 @@ This operation does not require authentication

```json
{
"pageSize": 0,
"totalResults": 0,
"offset": 0,
"fills": [
{
"id": "string",
Expand All @@ -2994,6 +3008,9 @@ This operation does not require authentication

|Name|Type|Required|Restrictions|Description|
|---|---|---|---|---|
|pageSize|number(double)|false|none|none|
|totalResults|number(double)|false|none|none|
|offset|number(double)|false|none|none|
|fills|[[FillResponseObject](#schemafillresponseobject)]|true|none|none|

## HeightResponse
Expand Down Expand Up @@ -3845,6 +3862,9 @@ or

```json
{
"pageSize": 0,
"totalResults": 0,
"offset": 0,
"trades": [
{
"id": "string",
Expand All @@ -3864,6 +3884,9 @@ or

|Name|Type|Required|Restrictions|Description|
|---|---|---|---|---|
|pageSize|number(double)|false|none|none|
|totalResults|number(double)|false|none|none|
|offset|number(double)|false|none|none|
|trades|[[TradeResponseObject](#schematraderesponseobject)]|true|none|none|

## TransferType
Expand Down
42 changes: 42 additions & 0 deletions indexer/services/comlink/public/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,18 @@
},
"FillResponse": {
"properties": {
"pageSize": {
"type": "number",
"format": "double"
},
"totalResults": {
"type": "number",
"format": "double"
},
"offset": {
"type": "number",
"format": "double"
},
"fills": {
"items": {
"$ref": "#/components/schemas/FillResponseObject"
Expand Down Expand Up @@ -1066,6 +1078,18 @@
},
"TradeResponse": {
"properties": {
"pageSize": {
"type": "number",
"format": "double"
},
"totalResults": {
"type": "number",
"format": "double"
},
"offset": {
"type": "number",
"format": "double"
},
"trades": {
"items": {
"$ref": "#/components/schemas/TradeResponseObject"
Expand Down Expand Up @@ -1523,6 +1547,15 @@
"schema": {
"$ref": "#/components/schemas/IsoString"
}
},
{
"in": "query",
"name": "page",
"required": false,
"schema": {
"format": "double",
"type": "number"
}
}
]
}
Expand Down Expand Up @@ -2246,6 +2279,15 @@
"schema": {
"$ref": "#/components/schemas/IsoString"
}
},
{
"in": "query",
"name": "page",
"required": false,
"schema": {
"format": "double",
"type": "number"
}
}
]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class FillsController extends Controller {
@Query() limit?: number,
@Query() createdBeforeOrAtHeight?: number,
@Query() createdBeforeOrAt?: IsoString,
@Query() page?: number,
): Promise<FillResponse> {
// TODO(DEC-656): Change to using a cache of markets in Redis similar to Librarian instead of
// querying the DB.
Expand All @@ -68,7 +69,12 @@ class FillsController extends Controller {
}

const subaccountId: string = SubaccountTable.uuid(address, subaccountNumber);
const fills: FillFromDatabase[] = await FillTable.findAll(
const {
results: fills,
limit: pageSize,
offset,
total,
} = await FillTable.findAll(
{
subaccountId: [subaccountId],
clobPairId,
Expand All @@ -77,6 +83,7 @@ class FillsController extends Controller {
? createdBeforeOrAtHeight.toString()
: undefined,
createdBeforeOrAt,
page,
},
[QueryableField.LIMIT],
);
Expand All @@ -98,6 +105,9 @@ class FillsController extends Controller {
fills: fills.map((fill: FillFromDatabase): FillResponseObject => {
return fillToResponseObject(fill, clobPairIdToMarket, subaccountNumber);
}),
pageSize,
totalResults: total,
offset,
};
}

Expand Down Expand Up @@ -132,7 +142,7 @@ class FillsController extends Controller {
);
const subaccountIds: string[] = Object.keys(childIdtoSubaccountNumber);

const fills: FillFromDatabase[] = await FillTable.findAll(
const { results: fills } = await FillTable.findAll(
{
subaccountId: subaccountIds,
clobPairId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class TradesController extends Controller {
@Query() limit?: number,
@Query() createdBeforeOrAtHeight?: number,
@Query() createdBeforeOrAt?: IsoString,
@Query() page?: number,
): Promise<TradeResponse> {
const clobPairId: string | undefined = perpetualMarketRefresher
.getClobPairIdFromTicker(ticker);
Expand All @@ -53,7 +54,12 @@ class TradesController extends Controller {
throw new NotFoundError(`${ticker} not found in tickers of type ${MarketType.PERPETUAL}`);
}

const fills: FillFromDatabase[] = await FillTable.findAll(
const {
results: fills,
limit: pageSize,
offset,
total,
} = await FillTable.findAll(
{
clobPairId,
liquidity: Liquidity.TAKER,
Expand All @@ -62,6 +68,7 @@ class TradesController extends Controller {
? createdBeforeOrAtHeight.toString()
: undefined,
createdBeforeOrAt,
page,
},
[QueryableField.LIQUIDITY, QueryableField.CLOB_PAIR_ID, QueryableField.LIMIT],
);
Expand All @@ -70,6 +77,9 @@ class TradesController extends Controller {
trades: fills.map((fill: FillFromDatabase): TradeResponseObject => {
return fillToTradeResponseObject(fill);
}),
pageSize,
totalResults: total,
offset,
};
}
}
Expand Down
4 changes: 2 additions & 2 deletions indexer/services/comlink/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export type AssetPositionsMap = { [symbol: string]: AssetPositionResponseObject

/* ------- FILL TYPES ------- */

export interface FillResponse {
export interface FillResponse extends PaginationResponse {
fills: FillResponseObject[],
}

Expand Down Expand Up @@ -191,7 +191,7 @@ export interface PnlTicksResponseObject {

/* ------- TRADE TYPES ------- */

export interface TradeResponse {
export interface TradeResponse extends PaginationResponse {
trades: TradeResponseObject[],
}

Expand Down

0 comments on commit 635dcd6

Please sign in to comment.