From 61c8929c851530c5fa30da5d34072e9db6889461 Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Mon, 26 Sep 2022 19:23:35 -0300 Subject: [PATCH] Adding support for /trades endpoint --- examples/{getTradeHistory.ts => getTrades.ts} | 13 +++---- package.json | 2 +- src/client.ts | 26 +++++++++----- src/endpoints.ts | 2 +- src/http-helpers/index.ts | 33 ++++++++++++++++-- src/types.ts | 34 +++++++++++++------ tests/http-helpers/index.test.ts | 31 +++++++++++++++-- 7 files changed, 107 insertions(+), 34 deletions(-) rename examples/{getTradeHistory.ts => getTrades.ts} (77%) diff --git a/examples/getTradeHistory.ts b/examples/getTrades.ts similarity index 77% rename from examples/getTradeHistory.ts rename to examples/getTrades.ts index 6536093..4858c78 100644 --- a/examples/getTradeHistory.ts +++ b/examples/getTrades.ts @@ -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(); diff --git a/package.json b/package.json index d61d6d4..2b39efb 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@polymarket/clob-client", "description": "Typescript client for Polymarket's CLOB", - "version": "1.1.0", + "version": "1.1.1", "contributors": [ { "name": "Jonathan Amenechi", diff --git a/src/client.ts b/src/client.ts index 63fa196..2a21524 100644 --- a/src/client.ts +++ b/src/client.ts @@ -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 { @@ -26,7 +36,7 @@ import { GET_ORDER, POST_ORDER, TIME, - TRADE_HISTORY, + TRADES, GET_ORDER_BOOK, DELETE_API_KEY, MIDPOINT, @@ -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 { + public async getTrades(params?: TradeParams): Promise { this.canL2Auth(); - const endpoint = TRADE_HISTORY; + const endpoint = TRADES; const headerArgs = { method: GET, requestPath: endpoint, @@ -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); } @@ -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); } diff --git a/src/endpoints.ts b/src/endpoints.ts index 124a60b..2ea3c17 100644 --- a/src/endpoints.ts +++ b/src/endpoints.ts @@ -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"; diff --git a/src/http-helpers/index.ts b/src/http-helpers/index.ts index 1ae9fe2..a6d8e00 100644 --- a/src/http-helpers/index.ts +++ b/src/http-helpers/index.ts @@ -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"; @@ -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}?`; @@ -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; +}; diff --git a/src/types.ts b/src/types.ts index 26c7d7d..364ad06 100644 --- a/src/types.ts +++ b/src/types.ts @@ -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 { diff --git a/tests/http-helpers/index.test.ts b/tests/http-helpers/index.test.ts index fb28cbb..edb0763 100644 --- a/tests/http-helpers/index.test.ts +++ b/tests/http-helpers/index.test.ts @@ -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", () => { @@ -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, @@ -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", + ); + }); + }); });