From 484ff81d3ae2b7b4e7f8f68f7bf38fe67d225ecb Mon Sep 17 00:00:00 2001 From: Davide Segullo Date: Mon, 3 Jun 2024 19:22:39 +0200 Subject: [PATCH] feat: add pagination clients support (#159) * feat(v4-client-js): :sparkles: add pagination support * feat(v4-client-py): :sparkles: add pagination support * test(v4-client-js): :white_check_mark: add pagination tests * fix(v4-client-js): :rotating_light: fix lint errors * chore(v4-client-js): :pushpin: bump package version * revert(v4-client-py): :rewind: pagination integration * chore(v4-client-js): :bookmark: bump package version --- .../modules/client/AccountEndpoints.test.ts | 71 +++++++++++++++++++ .../modules/client/MarketsEndpoints.test.ts | 21 ++++++ v4-client-js/package.json | 2 +- v4-client-js/src/clients/constants.ts | 2 +- v4-client-js/src/clients/modules/account.ts | 8 +++ v4-client-js/src/clients/modules/markets.ts | 2 + .../v4_client_py/clients/modules/account.py | 2 +- .../v4_client_py/clients/modules/markets.py | 2 +- 8 files changed, 106 insertions(+), 4 deletions(-) diff --git a/v4-client-js/__tests__/modules/client/AccountEndpoints.test.ts b/v4-client-js/__tests__/modules/client/AccountEndpoints.test.ts index 0ad2af08..e1ca2b4e 100644 --- a/v4-client-js/__tests__/modules/client/AccountEndpoints.test.ts +++ b/v4-client-js/__tests__/modules/client/AccountEndpoints.test.ts @@ -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(); @@ -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(); @@ -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); + }); }); }); diff --git a/v4-client-js/__tests__/modules/client/MarketsEndpoints.test.ts b/v4-client-js/__tests__/modules/client/MarketsEndpoints.test.ts index 0ec69285..274c1f56 100644 --- a/v4-client-js/__tests__/modules/client/MarketsEndpoints.test.ts +++ b/v4-client-js/__tests__/modules/client/MarketsEndpoints.test.ts @@ -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; diff --git a/v4-client-js/package.json b/v4-client-js/package.json index 8f10f1da..d5e7fdef 100644 --- a/v4-client-js/package.json +++ b/v4-client-js/package.json @@ -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": { diff --git a/v4-client-js/src/clients/constants.ts b/v4-client-js/src/clients/constants.ts index 3abddea5..27e30b14 100644 --- a/v4-client-js/src/clients/constants.ts +++ b/v4-client-js/src/clients/constants.ts @@ -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', diff --git a/v4-client-js/src/clients/modules/account.ts b/v4-client-js/src/clients/modules/account.ts index 6730571e..c4379ba2 100644 --- a/v4-client-js/src/clients/modules/account.ts +++ b/v4-client-js/src/clients/modules/account.ts @@ -60,6 +60,7 @@ export default class AccountClient extends RestClient { limit?: number | null, createdBeforeOrAtHeight?: number | null, createdBeforeOrAt?: string | null, + page?: number | null, ): Promise { const uri = '/v4/transfers'; return this.get(uri, { @@ -68,6 +69,7 @@ export default class AccountClient extends RestClient { limit, createdBeforeOrAtHeight, createdBeforeOrAt, + page, }); } @@ -113,6 +115,7 @@ export default class AccountClient extends RestClient { limit?: number | null, createdBeforeOrAtHeight?: number | null, createdBeforeOrAt?: string | null, + page?: number | null, ): Promise { const uri = '/v4/fills'; return this.get(uri, { @@ -123,6 +126,7 @@ export default class AccountClient extends RestClient { limit, createdBeforeOrAtHeight, createdBeforeOrAt, + page, }); } @@ -131,6 +135,8 @@ export default class AccountClient extends RestClient { subaccountNumber: number, effectiveBeforeOrAt?: string | null, effectiveAtOrAfter?: string | null, + limit?: number | null, + page?: number | null, ): Promise { const uri = '/v4/historical-pnl'; return this.get(uri, { @@ -138,6 +144,8 @@ export default class AccountClient extends RestClient { subaccountNumber, effectiveBeforeOrAt, effectiveAtOrAfter, + limit, + page, }); } } diff --git a/v4-client-js/src/clients/modules/markets.ts b/v4-client-js/src/clients/modules/markets.ts index 961a30ea..5e8b207e 100644 --- a/v4-client-js/src/clients/modules/markets.ts +++ b/v4-client-js/src/clients/modules/markets.ts @@ -20,11 +20,13 @@ export default class MarketsClient extends RestClient { market: string, startingBeforeOrAtHeight?: number | null, limit?: number | null, + page?: number | null, ): Promise { const uri = `/v4/trades/perpetualMarket/${market}`; return this.get(uri, { createdBeforeOrAtHeight: startingBeforeOrAtHeight, limit, + page, }); } diff --git a/v4-client-py/v4_client_py/clients/modules/account.py b/v4-client-py/v4_client_py/clients/modules/account.py index d2e9ec65..fb571ca6 100644 --- a/v4-client-py/v4_client_py/clients/modules/account.py +++ b/v4-client-py/v4_client_py/clients/modules/account.py @@ -418,4 +418,4 @@ def get_subaccount_historical_pnls( 'effectiveBeforeOrAt': effective_before_or_at, 'effectiveAtOrAfter': effective_at_or_after, }, - ) + ) \ No newline at end of file diff --git a/v4-client-py/v4_client_py/clients/modules/markets.py b/v4-client-py/v4_client_py/clients/modules/markets.py index 72e1df70..c4bae70f 100644 --- a/v4-client-py/v4_client_py/clients/modules/markets.py +++ b/v4-client-py/v4_client_py/clients/modules/markets.py @@ -211,4 +211,4 @@ def get_perpetual_markets_sparklines( { 'timePeriod': period, }, - ) + ) \ No newline at end of file