Skip to content

Commit

Permalink
feat: add pagination clients support (#159)
Browse files Browse the repository at this point in the history
* feat(v4-client-js): ✨ add pagination support

* feat(v4-client-py): ✨ add pagination support

* test(v4-client-js): ✅ add pagination tests

* fix(v4-client-js): 🚨 fix lint errors

* chore(v4-client-js): 📌 bump package version

* revert(v4-client-py): ⏪ pagination integration

* chore(v4-client-js): 🔖 bump package version
  • Loading branch information
DavideSegullo committed Jun 3, 2024
1 parent 4721c81 commit 484ff81
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 4 deletions.
71 changes: 71 additions & 0 deletions v4-client-js/__tests__/modules/client/AccountEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ describe('IndexerClient', () => {
}
});

it('Transfers Pagination', async () => {
const response = await client.account.getSubaccountTransfers(
DYDX_TEST_ADDRESS,
0,
1,
undefined,
undefined,
1,
);
expect(response).not.toBeNull();
const transfers = response.transfers;

expect(transfers).not.toBeNull();
if (transfers.length > 0) {
const transfer = transfers[0];
expect(transfer).not.toBeNull();

expect(response.totalResults).toBeGreaterThanOrEqual(1);
}

expect(response.pageSize).toStrictEqual(1);
expect(response.offset).toStrictEqual(0);
});

it('Orders', async () => {
const response = await client.account.getSubaccountOrders(DYDX_TEST_ADDRESS, 0);
expect(response).not.toBeNull();
Expand All @@ -78,6 +102,31 @@ describe('IndexerClient', () => {
}
});

it('Fills Pagination', async () => {
const response = await client.account.getSubaccountFills(
DYDX_TEST_ADDRESS,
0,
undefined,
undefined,
1,
undefined,
undefined,
1,
);

expect(response).not.toBeNull();
const fills = response.fills;
expect(fills).not.toBeNull();
if (fills.length > 0) {
const fill = fills[0];
expect(fill).not.toBeNull();
expect(response.totalResults).toBeGreaterThanOrEqual(1);
}

expect(response.pageSize).toStrictEqual(1);
expect(response.offset).toStrictEqual(0);
});

it('Historical PNL', async () => {
const response = await client.account.getSubaccountHistoricalPNLs(DYDX_TEST_ADDRESS, 0);
expect(response).not.toBeNull();
Expand All @@ -88,5 +137,27 @@ describe('IndexerClient', () => {
expect(historicalPnl0).not.toBeNull();
}
});

it('Historical PNL Pagination', async () => {
const response = await client.account.getSubaccountHistoricalPNLs(
DYDX_TEST_ADDRESS,
0,
undefined,
undefined,
1,
1,
);
expect(response).not.toBeNull();
const historicalPnl = response.historicalPnl;
expect(historicalPnl).not.toBeNull();
if (historicalPnl.length > 0) {
const historicalPnl0 = historicalPnl[0];
expect(historicalPnl0).not.toBeNull();
expect(response.totalResults).toBeGreaterThanOrEqual(1);
}

expect(response.pageSize).toStrictEqual(1);
expect(response.offset).toStrictEqual(0);
});
});
});
21 changes: 21 additions & 0 deletions v4-client-js/__tests__/modules/client/MarketsEndpoints.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,27 @@ describe('IndexerClient', () => {
expect(trades).not.toBeUndefined();
});

it('BTC Trades Pagination', async () => {
const response = await client.markets.getPerpetualMarketTrades(
MARKET_BTC_USD,
undefined,
1,
1,
);
const trades = response.trades;
expect(trades).not.toBeUndefined();

if (trades.length > 0) {
const trade = trades[0];
expect(trade).not.toBeNull();

expect(response.totalResults).toBeGreaterThanOrEqual(1);
}

expect(response.pageSize).toStrictEqual(1);
expect(response.offset).toStrictEqual(0);
});

it('BTC Orderbook', async () => {
const response = await client.markets.getPerpetualMarketOrderbook(MARKET_BTC_USD);
const asks = response.asks;
Expand Down
2 changes: 1 addition & 1 deletion v4-client-js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@dydxprotocol/v4-client-js",
"version": "1.1.16",
"version": "1.1.17",
"description": "General client library for the new dYdX system (v4 decentralized)",
"main": "build/src/index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion v4-client-js/src/clients/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const MAINNET_CHAIN_ID = 'dydx-mainnet-1';

// ------------ API URLs ------------
export enum IndexerApiHost {
TESTNET = 'https://dydx-testnet.imperator.co',
TESTNET = 'https://indexer.v4testnet.dydx.exchange/',
LOCAL = 'http://localhost:3002',
// For the deployment by DYDX token holders
MAINNET = 'https://indexer.dydx.trade',
Expand Down
8 changes: 8 additions & 0 deletions v4-client-js/src/clients/modules/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export default class AccountClient extends RestClient {
limit?: number | null,
createdBeforeOrAtHeight?: number | null,
createdBeforeOrAt?: string | null,
page?: number | null,
): Promise<Data> {
const uri = '/v4/transfers';
return this.get(uri, {
Expand All @@ -68,6 +69,7 @@ export default class AccountClient extends RestClient {
limit,
createdBeforeOrAtHeight,
createdBeforeOrAt,
page,
});
}

Expand Down Expand Up @@ -113,6 +115,7 @@ export default class AccountClient extends RestClient {
limit?: number | null,
createdBeforeOrAtHeight?: number | null,
createdBeforeOrAt?: string | null,
page?: number | null,
): Promise<Data> {
const uri = '/v4/fills';
return this.get(uri, {
Expand All @@ -123,6 +126,7 @@ export default class AccountClient extends RestClient {
limit,
createdBeforeOrAtHeight,
createdBeforeOrAt,
page,
});
}

Expand All @@ -131,13 +135,17 @@ export default class AccountClient extends RestClient {
subaccountNumber: number,
effectiveBeforeOrAt?: string | null,
effectiveAtOrAfter?: string | null,
limit?: number | null,
page?: number | null,
): Promise<Data> {
const uri = '/v4/historical-pnl';
return this.get(uri, {
address,
subaccountNumber,
effectiveBeforeOrAt,
effectiveAtOrAfter,
limit,
page,
});
}
}
2 changes: 2 additions & 0 deletions v4-client-js/src/clients/modules/markets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export default class MarketsClient extends RestClient {
market: string,
startingBeforeOrAtHeight?: number | null,
limit?: number | null,
page?: number | null,
): Promise<Data> {
const uri = `/v4/trades/perpetualMarket/${market}`;
return this.get(uri, {
createdBeforeOrAtHeight: startingBeforeOrAtHeight,
limit,
page,
});
}

Expand Down
2 changes: 1 addition & 1 deletion v4-client-py/v4_client_py/clients/modules/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,4 @@ def get_subaccount_historical_pnls(
'effectiveBeforeOrAt': effective_before_or_at,
'effectiveAtOrAfter': effective_at_or_after,
},
)
)
2 changes: 1 addition & 1 deletion v4-client-py/v4_client_py/clients/modules/markets.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,4 @@ def get_perpetual_markets_sparklines(
{
'timePeriod': period,
},
)
)

0 comments on commit 484ff81

Please sign in to comment.