Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(ts-sdk): route hosted order book reads #1021

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
realfishsam wants to merge 1 commit into main
base: main
Choose a base branch
Loading
from fix/issue-1011-ts-hosted-orderbook-routing
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
13 changes: 11 additions & 2 deletions sdks/typescript/pmxt/client.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import { logger } from "./logger.js";
// once the matching files exist.
import {
HOSTED_TRADING_VENUES,
HOSTED_CATALOG_BASE_URL,
_tradingRequest,
resolveWalletAddress,
ensureHostedTradingSupported,
Expand Down Expand Up @@ -1076,7 +1077,11 @@ export abstract class Exchange {
if (limit === undefined) args.push(null);
args.push(params);
}
const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrderBook`, {
const route = this.pmxtApiKey ? HOSTED_METHOD_ROUTES.get("fetchOrderBook") : undefined;
const url = route
? `${HOSTED_CATALOG_BASE_URL}${formatRoutePath(route, { venue: this.exchangeName })}`
: `${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrderBook`;
const response = await this.fetchWithRetry(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
body: JSON.stringify({ args, credentials: this.getCredentials() }),
Expand Down Expand Up @@ -1105,7 +1110,11 @@ export abstract class Exchange {
try {
const args: any[] = [];
args.push(outcomeIds.map(resolveOutcomeId));
const response = await this.fetchWithRetry(`${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrderBooks`, {
const route = this.pmxtApiKey ? HOSTED_METHOD_ROUTES.get("fetchOrderBooks") : undefined;
const url = route
? `${HOSTED_CATALOG_BASE_URL}${formatRoutePath(route, { venue: this.exchangeName })}`
: `${this.resolveBaseUrl()}/api/${this.exchangeName}/fetchOrderBooks`;
const response = await this.fetchWithRetry(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json', ...this.getAuthHeaders() },
body: JSON.stringify({ args, credentials: this.getCredentials() }),
Expand Down
2 changes: 2 additions & 0 deletions sdks/typescript/pmxt/hosted-routing.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ export const HOSTED_METHOD_ROUTES: ReadonlyMap<string, HostedRoute> = new Map([
["fetchMyTrades", { method: "GET", path: "/v0/user/{address}/trades", base: "trading", requiresWalletAddress: true }],
["fetchBalance", { method: "GET", path: "/v0/user/{address}/balances", base: "trading", requiresWalletAddress: true }],
["fetchPositions", { method: "GET", path: "/v0/user/{address}/positions", base: "trading", requiresWalletAddress: true }],
["fetchOrderBook", { method: "POST", path: "/api/{venue}/fetchOrderBook", base: "catalog" }],
["fetchOrderBooks", { method: "POST", path: "/api/{venue}/fetchOrderBooks", base: "catalog" }],
["escrowApproveTx", { method: "POST", path: "/v0/escrow/approve", base: "trading" }],
["escrowDepositTx", { method: "POST", path: "/v0/escrow/deposit", base: "trading" }],
["escrowWithdrawTx", { method: "POST", path: "/v0/escrow/withdraw", base: "trading" }],
Expand Down
45 changes: 45 additions & 0 deletions sdks/typescript/tests/hosted-dispatch.test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ jest.mock("../pmxt/hosted-typed-data", () => ({

import { Polymarket } from "../pmxt/client";
import {
HOSTED_CATALOG_BASE_URL,
HOSTED_METHOD_ROUTES,
HOSTED_TRADING_BASE_URL,
} from "../pmxt/hosted-routing";
import { NotSupported } from "../pmxt/errors";
Expand Down Expand Up @@ -85,6 +87,49 @@ afterEach(() => {
// --------------------------------------------------------------------------

describe("hosted read dispatch", () => {
it("route table includes hosted catalog order book reads", () => {
expect(HOSTED_METHOD_ROUTES.get("fetchOrderBook")).toMatchObject({
method: "POST",
path: "/api/{venue}/fetchOrderBook",
base: "catalog",
});
expect(HOSTED_METHOD_ROUTES.get("fetchOrderBooks")).toMatchObject({
method: "POST",
path: "/api/{venue}/fetchOrderBooks",
base: "catalog",
});
});

it("fetchOrderBook → hosted catalog /api/{venue}/fetchOrderBook", async () => {
const spy = installFetchSpy(() =>
jsonResponse({ success: true, data: { bids: [], asks: [] } }),
);
const api = makePolymarket();
await api.fetchOrderBook("outcome-1", 5, { outcome: "yes" } as any);
const reqs = captured(spy);
expect(reqs).toHaveLength(1);
expect(reqs[0].url).toBe(`${HOSTED_CATALOG_BASE_URL}/api/polymarket/fetchOrderBook`);
expect(reqs[0].init?.method).toBe("POST");
const headers = reqs[0].init?.headers as Record<string, string> | undefined;
expect(headers?.Authorization).toBe(`Bearer ${PMXT_API_KEY}`);
const body = JSON.parse((reqs[0].init?.body as string) ?? "{}");
expect(body.args).toEqual(["outcome-1", 5, { outcome: "yes" }]);
});

it("fetchOrderBooks → hosted catalog /api/{venue}/fetchOrderBooks", async () => {
const spy = installFetchSpy(() =>
jsonResponse({ success: true, data: { "outcome-1": { bids: [], asks: [] } } }),
);
const api = makePolymarket();
await api.fetchOrderBooks(["outcome-1"]);
const reqs = captured(spy);
expect(reqs).toHaveLength(1);
expect(reqs[0].url).toBe(`${HOSTED_CATALOG_BASE_URL}/api/polymarket/fetchOrderBooks`);
expect(reqs[0].init?.method).toBe("POST");
const body = JSON.parse((reqs[0].init?.body as string) ?? "{}");
expect(body.args).toEqual([["outcome-1"]]);
});

it("fetchBalance → GET /v0/user/{addr}/balances", async () => {
const spy = installFetchSpy(() =>
jsonResponse({ balances: [{ currency: "USDC", amount: 12.5 }] }),
Expand Down
16 changes: 16 additions & 0 deletions sdks/typescript/tests/hosted-routing.test.ts
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { HOSTED_METHOD_ROUTES } from "../pmxt/hosted-routing";

describe("hosted routing table", () => {
it("routes order book reads through the hosted catalog API", () => {
expect(HOSTED_METHOD_ROUTES.get("fetchOrderBook")).toMatchObject({
method: "POST",
path: "/api/{venue}/fetchOrderBook",
base: "catalog",
});
expect(HOSTED_METHOD_ROUTES.get("fetchOrderBooks")).toMatchObject({
method: "POST",
path: "/api/{venue}/fetchOrderBooks",
base: "catalog",
});
});
});
Loading

AltStyle によって変換されたページ (->オリジナル) /