Skip to content

Commit

Permalink
Merge pull request #51 from Polymarket/feat/new-orders-endpoint
Browse files Browse the repository at this point in the history
Add support for orders enpoint
  • Loading branch information
mttwlkr authored Oct 5, 2022
2 parents 6bb3f01 + 3ffd3ef commit f3671b6
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 21 deletions.
9 changes: 3 additions & 6 deletions examples/getOpenOrders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@ async function main() {
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

const resp = await clobClient.getOpenOrders();
console.log(resp);

// Filtered
const filteredByMarket = await clobClient.getOpenOrders({
const resp = await clobClient.getOpenOrders({
market: "16678291189211314787145083999015737376658799626183230671758641503291735614088",
owner: creds.key,
});
console.log(filteredByMarket);
console.log(resp);
console.log(`Done!`);
}

Expand Down
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@polymarket/clob-client",
"description": "Typescript client for Polymarket's CLOB",
"version": "1.1.1",
"version": "1.1.2",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand All @@ -10,6 +10,10 @@
{
"name": "Rodrigo Calvo",
"url": "https://github.com/poly-rodr"
},
{
"name": "Matt Walker",
"url": "https://github.com/mttwlkr"
}
],
"main": "dist/index.js",
Expand Down
22 changes: 12 additions & 10 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ import { JsonRpcSigner } from "@ethersproject/providers";
import { SignatureType, SignedOrder } from "@polymarket/order-utils";
import {
ApiKeyCreds,
OrderPayload,
UserOrder,
OpenOrdersResponse,
Order,
ApiKeysResponse,
Chain,
FilterParams,
OrderHistory,
OpenOrdersParams,
OpenOrdersResponse,
OptionalParams,
Chain,
TradeParams,
Order,
OrderHistory,
OrderPayload,
Trade,
TradeParams,
UserOrder,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
addFilterParamsToUrl,
addOpenOrderParamsToUrl,
addTradeParamsToUrl,
del,
DELETE,
Expand All @@ -42,10 +44,10 @@ import {
MIDPOINT,
PRICE,
OPEN_ORDERS,
ORDER_HISTORY,
DERIVE_API_KEY,
GET_LAST_TRADE_PRICE,
GET_LARGE_ORDERS,
ORDER_HISTORY,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";

Expand Down Expand Up @@ -261,7 +263,7 @@ export class ClobClient {
return this.orderBuilder.buildOrder(userOrder);
}

public async getOpenOrders(params?: FilterParams): Promise<OpenOrdersResponse> {
public async getOpenOrders(params?: OpenOrdersParams): Promise<OpenOrdersResponse> {
this.canL2Auth();
const endpoint = OPEN_ORDERS;
const l2HeaderArgs = {
Expand All @@ -275,7 +277,7 @@ export class ClobClient {
l2HeaderArgs,
);

const url = addFilterParamsToUrl(`${this.host}${endpoint}`, params);
const url = addOpenOrderParamsToUrl(`${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 @@ -20,7 +20,7 @@ export const ORDER_HISTORY = "/order-history";

export const GET_ORDER = "/order/";

export const OPEN_ORDERS = "/open-orders";
export const OPEN_ORDERS = "/orders";

export const POST_ORDER = "/order";

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

export const GET = "GET";
export const POST = "POST";
Expand Down Expand Up @@ -93,6 +94,23 @@ export const addFilterParamsToUrl = (baseUrl: string, params?: FilterParams): st
return url;
};

export const addOpenOrderParamsToUrl = (baseUrl: string, params?: OpenOrdersParams): string => {
let url = baseUrl;
if (params !== undefined) {
url = `${url}?`;
if (params.market !== undefined) {
url = buildQueryParams(url, "market", params.market);
}
if (params.owner !== undefined) {
url = buildQueryParams(url, "owner", params.owner);
}
if (params.id !== undefined) {
url = buildQueryParams(url, "id", params.id as string);
}
}
return url;
};

export const addTradeParamsToUrl = (baseUrl: string, params?: TradeParams): string => {
let url = baseUrl;
if (params !== undefined) {
Expand Down
21 changes: 19 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,21 @@ export interface Order {
tokenID: string;
}

export interface OpenOrdersResponse {
orders: Order[];
export interface OpenOrder {
associate_trades: null | Trade[];
id: string;
market: string;
original_size: string;
outcome: string;
outcome_index: number;
owner: string;
price: string;
side: string;
size_matched: string;
}

export type OpenOrdersResponse = OpenOrder[];

export interface FilterParams {
market?: string;
max?: number;
Expand All @@ -122,6 +133,12 @@ export interface TradeParams {
after?: string;
}

export interface OpenOrdersParams {
market: string;
owner: string;
id?: string;
}

export interface Trade {
id: string;
market_order: string;
Expand Down
18 changes: 17 additions & 1 deletion tests/http-helpers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
buildQueryParams,
addFilterParamsToUrl,
addTradeParamsToUrl,
addOpenOrderParamsToUrl,
} from "../../src/http-helpers/index";
import { TradeParams } from "../../src";
import { OpenOrdersParams } from "../../dist/types";

describe("utilities", () => {
describe("buildQueryParams", () => {
Expand Down Expand Up @@ -41,7 +43,7 @@ describe("utilities", () => {
});
});

describe("addFilterParamsToUrl", () => {
describe("addTradeParamsToUrl", () => {
it("checking url + params", () => {
const url = addTradeParamsToUrl("http://tracker", {
market: "10000",
Expand All @@ -60,4 +62,18 @@ describe("utilities", () => {
);
});
});

describe("addOpenOrderParamsToUrl", () => {
it("checking url + params", () => {
const url = addOpenOrderParamsToUrl("http://tracker", {
market: "10000",
owner: "0x69",
id: "1",
} as OpenOrdersParams);
expect(url).not.null;
expect(url).not.undefined;
expect(url).not.empty;
expect(url).equal("http://tracker?market=10000&owner=0x69&id=1");
});
});
});

0 comments on commit f3671b6

Please sign in to comment.