Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ export class ClobClient {
params?: TradeParams,
only_first_page = false,
next_cursor?: string,
): Promise<Trade[]> {
): Promise<Trade[] | ClobErrorResponseBody> {
this.canL2Auth();

const endpoint = GET_TRADES;
Expand All @@ -622,8 +622,10 @@ export class ClobClient {
headers,
params: _params,
});
const data = this.getPaginatedResponseData<Trade>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}
Expand Down Expand Up @@ -934,7 +936,7 @@ export class ClobClient {
params?: OpenOrderParams,
only_first_page = false,
next_cursor?: string,
): Promise<OpenOrdersResponse> {
): Promise<OpenOrdersResponse | ClobErrorResponseBody> {
this.canL2Auth();
const endpoint = GET_OPEN_ORDERS;
const l2HeaderArgs = {
Expand All @@ -960,16 +962,18 @@ export class ClobClient {
headers,
params: _params,
});
const data = this.getPaginatedResponseData<OpenOrder>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}

public async getPreMigrationOrders(
only_first_page = false,
next_cursor?: string,
): Promise<PreMigrationOrdersResponse> {
): Promise<PreMigrationOrdersResponse | ClobErrorResponseBody> {
this.canL2Auth();
const endpoint = GET_PRE_MIGRATION_ORDERS;
const l2HeaderArgs = {
Expand All @@ -992,8 +996,10 @@ export class ClobClient {
headers,
params: _params,
});
const data = this.getPaginatedResponseData<PreMigrationOrder>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}
Expand Down Expand Up @@ -1184,7 +1190,9 @@ export class ClobClient {
}

// Rewards
public async getEarningsForUserForDay(date: string): Promise<UserEarning[]> {
public async getEarningsForUserForDay(
date: string,
): Promise<UserEarning[] | ClobErrorResponseBody> {
this.canL2Auth();

const endpoint = GET_EARNINGS_FOR_USER_FOR_DAY;
Expand Down Expand Up @@ -1213,8 +1221,10 @@ export class ClobClient {
headers,
params,
});
const data = this.getPaginatedResponseData<UserEarning>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}
Expand Down Expand Up @@ -1251,7 +1261,7 @@ export class ClobClient {
order_by = "",
position = "",
no_competition = false,
): Promise<UserRewardsEarning[]> {
): Promise<UserRewardsEarning[] | ClobErrorResponseBody> {
this.canL2Auth();

const endpoint = GET_REWARDS_EARNINGS_PERCENTAGES;
Expand Down Expand Up @@ -1283,8 +1293,10 @@ export class ClobClient {
headers,
params,
});
const data = this.getPaginatedResponseData<UserRewardsEarning>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}
Expand Down Expand Up @@ -1312,28 +1324,34 @@ export class ClobClient {
return this.get(`${this.host}${endpoint}`, { headers, params: _params });
}

public async getCurrentRewards(): Promise<MarketReward[]> {
public async getCurrentRewards(): Promise<MarketReward[] | ClobErrorResponseBody> {
let results: MarketReward[] = [];
let next_cursor = INITIAL_CURSOR;
while (next_cursor !== END_CURSOR) {
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS_CURRENT}`, {
params: { next_cursor },
});
const data = this.getPaginatedResponseData<MarketReward>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}

public async getRawRewardsForMarket(conditionId: string): Promise<MarketReward[]> {
public async getRawRewardsForMarket(
conditionId: string,
): Promise<MarketReward[] | ClobErrorResponseBody> {
let results: MarketReward[] = [];
let next_cursor = INITIAL_CURSOR;
while (next_cursor !== END_CURSOR) {
const response = await this.get(`${this.host}${GET_REWARDS_MARKETS}${conditionId}`, {
params: { next_cursor },
});
const data = this.getPaginatedResponseData<MarketReward>(response);
if (!data) return response;
next_cursor = response.next_cursor;
results = [...results, ...response.data];
results = [...results, ...data];
}
return results;
}
Expand Down Expand Up @@ -1419,6 +1437,10 @@ export class ClobClient {
return builderCode !== undefined && builderCode !== bytes32Zero;
}

private getPaginatedResponseData<T>(response: { data?: unknown } | null | undefined): T[] | undefined {
return Array.isArray(response?.data) ? [...response.data] : undefined;
}

private async _ensureMarketInfoCached(tokenID: string): Promise<void> {
if (tokenID in this.feeInfos) return;

Expand Down
4 changes: 3 additions & 1 deletion src/types/clob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,9 @@ export interface BuilderApiKeyResponse {
}

export type ClobErrorResponseBody = {
error: string;
error: unknown;
status?: number;
[key: string]: unknown;
};

export interface TradesPaginatedResponse {
Expand Down
42 changes: 42 additions & 0 deletions tests/client/paginationErrorHandling.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it, vi } from "vitest";

vi.mock("../../src/headers/index.js", () => ({
createL1Headers: vi.fn(),
createL2Headers: vi.fn(async () => ({})),
}));

import { Chain, ClobClient } from "../../src";

const makeClient = () =>
new ClobClient({
host: "http://localhost:8080",
chain: Chain.AMOY,
signer: {} as any,
creds: {
key: "key",
secret: "secret",
passphrase: "passphrase",
},
});

describe("pagination error handling", () => {
it("preserves structured errors for getOpenOrders", async () => {
const client = makeClient();
vi.spyOn(client as any, "get").mockResolvedValue({ error: "boom", status: 500 });

await expect(client.getOpenOrders()).resolves.toMatchObject({
error: "boom",
status: 500,
});
});

it("preserves structured errors for getCurrentRewards", async () => {
const client = makeClient();
vi.spyOn(client as any, "get").mockResolvedValue({ error: "boom", status: 503 });

await expect(client.getCurrentRewards()).resolves.toMatchObject({
error: "boom",
status: 503,
});
});
});