Skip to content

Commit

Permalink
Merge pull request #50 from Polymarket/feat/new-trades-endpoint
Browse files Browse the repository at this point in the history
Adding support for /trades endpoint
  • Loading branch information
poly-rodr authored Sep 26, 2022
2 parents 5644746 + 61c8929 commit 6bb3f01
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 33 deletions.
13 changes: 5 additions & 8 deletions examples/getTradeHistory.ts → examples/getTrades.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,13 @@ async function main() {
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

console.log(`Response: `);
const resp = await clobClient.getTradeHistory();
console.log(resp);
// Filtered
const filteredResp = await clobClient.getTradeHistory({
const trades = await clobClient.getTrades({
market: "16678291189211314787145083999015737376658799626183230671758641503291735614088",
max: 2,
maker: await wallet.getAddress(),
limit: 10,
});
console.log(`Filtered: `);
console.log(filteredResp);
console.log(`trades: `);
console.log(trades);
}

main();
26 changes: 18 additions & 8 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,23 @@ import {
Order,
ApiKeysResponse,
FilterParams,
TradeHistory,
OrderHistory,
OptionalParams,
Chain,
TradeParams,
Trade,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import { addQueryParamsToUrl, del, DELETE, GET, get, POST, post } from "./http-helpers";
import {
addFilterParamsToUrl,
addTradeParamsToUrl,
del,
DELETE,
GET,
get,
POST,
post,
} from "./http-helpers";
import { L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors";
import { orderToJson } from "./utilities";
import {
Expand All @@ -26,7 +36,7 @@ import {
GET_ORDER,
POST_ORDER,
TIME,
TRADE_HISTORY,
TRADES,
GET_ORDER_BOOK,
DELETE_API_KEY,
MIDPOINT,
Expand Down Expand Up @@ -222,14 +232,14 @@ export class ClobClient {
l2HeaderArgs,
);

const url = addQueryParamsToUrl(`${this.host}${endpoint}`, params);
const url = addFilterParamsToUrl(`${this.host}${endpoint}`, params);
return get(url, headers);
}

public async getTradeHistory(params?: FilterParams): Promise<TradeHistory> {
public async getTrades(params?: TradeParams): Promise<Trade[]> {
this.canL2Auth();

const endpoint = TRADE_HISTORY;
const endpoint = TRADES;
const headerArgs = {
method: GET,
requestPath: endpoint,
Expand All @@ -241,7 +251,7 @@ export class ClobClient {
headerArgs,
);

const url = addQueryParamsToUrl(`${this.host}${endpoint}`, params);
const url = addTradeParamsToUrl(`${this.host}${endpoint}`, params);
return get(url, headers);
}

Expand All @@ -265,7 +275,7 @@ export class ClobClient {
l2HeaderArgs,
);

const url = addQueryParamsToUrl(`${this.host}${endpoint}`, params);
const url = addFilterParamsToUrl(`${this.host}${endpoint}`, params);
return get(url, headers);
}

Expand Down
2 changes: 1 addition & 1 deletion src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const DELETE_API_KEY = "/delete-api-key";

export const DERIVE_API_KEY = "/derive-api-key";

export const TRADE_HISTORY = "/trade-history";
export const TRADES = "/trades";

export const ORDER_HISTORY = "/order-history";

Expand Down
33 changes: 31 additions & 2 deletions src/http-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { Method } from "axios";
import { FilterParams } from "src/types";
import { FilterParams, TradeParams } from "src/types";

export const GET = "GET";
export const POST = "POST";
Expand Down Expand Up @@ -73,7 +73,7 @@ export const buildQueryParams = (url: string, param: string, value: string): str
return urlWithParams;
};

export const addQueryParamsToUrl = (baseUrl: string, params?: FilterParams): string => {
export const addFilterParamsToUrl = (baseUrl: string, params?: FilterParams): string => {
let url = baseUrl;
if (params !== undefined) {
url = `${url}?`;
Expand All @@ -92,3 +92,32 @@ export const addQueryParamsToUrl = (baseUrl: string, params?: FilterParams): str
}
return url;
};

export const addTradeParamsToUrl = (baseUrl: string, params?: TradeParams): string => {
let url = baseUrl;
if (params !== undefined) {
url = `${url}?`;
if (params.market !== undefined) {
url = buildQueryParams(url, "market", params.market as string);
}
if (params.maker !== undefined) {
url = buildQueryParams(url, "maker", `${params.maker}`);
}
if (params.taker !== undefined) {
url = buildQueryParams(url, "taker", `${params.taker}`);
}
if (params.id !== undefined) {
url = buildQueryParams(url, "id", `${params.id}`);
}
if (params.limit !== undefined) {
url = buildQueryParams(url, "limit", `${params.limit}`);
}
if (params.before !== undefined) {
url = buildQueryParams(url, "before", `${params.before}`);
}
if (params.after !== undefined) {
url = buildQueryParams(url, "after", `${params.after}`);
}
}
return url;
};
34 changes: 23 additions & 11 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,21 +112,33 @@ export interface FilterParams {
endTs?: number;
}

export interface TradeParams {
market: string;
id?: string;
taker?: string;
maker?: string;
limit?: number;
before?: string;
after?: string;
}

export interface Trade {
tradeID: string;
id: string;
market_order: string;
market: string;
timestamp: string;
// TODO: re-define
marketOrderID: string;
limitOrderIDs: string[];
filledAmount: string;
type: string;
side: string;
size: string;
price: string;
status: string;
match_time: string;
last_update: string;
outcome: string;
outcome_index: number;
owner: string;
avgPrice: string;
}

export interface TradeHistory {
history: Trade[];
bucket_index: number;
limit_orders: string[];
limit_order_sizes_prices: string[][];
}

export interface OrderHistory {
Expand Down
31 changes: 28 additions & 3 deletions tests/http-helpers/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import "mocha";
import { expect } from "chai";
import { buildQueryParams, addQueryParamsToUrl } from "../../src/http-helpers/index";
import {
buildQueryParams,
addFilterParamsToUrl,
addTradeParamsToUrl,
} from "../../src/http-helpers/index";
import { TradeParams } from "../../src";

describe("utilities", () => {
describe("buildQueryParams", () => {
Expand All @@ -21,9 +26,9 @@ describe("utilities", () => {
});
});

describe("addQueryParamsToUrl", () => {
describe("addFilterParamsToUrl", () => {
it("checking url + params", () => {
const url = addQueryParamsToUrl("http://tracker", {
const url = addFilterParamsToUrl("http://tracker", {
market: "10000",
max: 250,
startTs: 1450000,
Expand All @@ -35,4 +40,24 @@ describe("utilities", () => {
expect(url).equal("http://tracker?market=10000&max=250&startTs=1450000&endTs=1460000");
});
});

describe("addFilterParamsToUrl", () => {
it("checking url + params", () => {
const url = addTradeParamsToUrl("http://tracker", {
market: "10000",
taker: "0x1",
maker: "0x2",
id: "1",
after: "100",
before: "200",
limit: 5,
} as TradeParams);
expect(url).not.null;
expect(url).not.undefined;
expect(url).not.empty;
expect(url).equal(
"http://tracker?market=10000&maker=0x2&taker=0x1&id=1&limit=5&before=200&after=100",
);
});
});
});

0 comments on commit 6bb3f01

Please sign in to comment.