Skip to content

Commit

Permalink
Merge pull request #71 from Polymarket/feat/trade-notifications
Browse files Browse the repository at this point in the history
Feat/ trade notifications
  • Loading branch information
poly-rodr authored Feb 2, 2023
2 parents 9119e58 + 23a5629 commit 3328c63
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 3 deletions.
28 changes: 28 additions & 0 deletions examples/dropTradeNofications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, Chain, ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

console.log(
await clobClient.dropTradeNotifications({
index: 0,
}),
);
}

main();
28 changes: 28 additions & 0 deletions examples/getTradeNofications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, Chain, ClobClient } from "../src";

dotenvConfig({ path: resolve(__dirname, "../.env") });

async function main() {
const wallet = new ethers.Wallet(`${process.env.PK}`);
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`);

const host = process.env.CLOB_API_URL || "http://localhost:8080";
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

console.log(
await clobClient.getTradeNotifications({
index: 0,
}),
);
}

main();
2 changes: 1 addition & 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.20",
"version": "1.1.21",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
45 changes: 45 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
OrderType,
Side,
Trade,
TradeNotification,
TradeNotificationParams,
TradeParams,
UserMarketOrder,
UserOrder,
Expand All @@ -23,6 +25,7 @@ import { createL1Headers, createL2Headers } from "./headers";
import {
addFilterParamsToUrl,
addOpenOrderParamsToUrl,
addTradeNotificationParamsToUrl,
addTradeParamsToUrl,
del,
DELETE,
Expand Down Expand Up @@ -53,6 +56,8 @@ import {
GET_MARKETS,
GET_MARKET,
GET_PRICES_HISTORY,
GET_TRADE_NOTIFICATIONS,
DROP_TRADE_NOTIFICATIONS,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";

Expand Down Expand Up @@ -257,6 +262,46 @@ export class ClobClient {
return get(url, headers);
}

public async getTradeNotifications(
params?: TradeNotificationParams,
): Promise<TradeNotification[]> {
this.canL2Auth();

const endpoint = GET_TRADE_NOTIFICATIONS;
const headerArgs = {
method: GET,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
headerArgs,
);

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

public async dropTradeNotifications(params?: TradeNotificationParams): Promise<void> {
this.canL2Auth();

const endpoint = DROP_TRADE_NOTIFICATIONS;
const l2HeaderArgs = {
method: DELETE,
requestPath: endpoint,
};

const headers = await createL2Headers(
this.signer as Wallet | JsonRpcSigner,
this.creds as ApiKeyCreds,
l2HeaderArgs,
);

const url = addTradeNotificationParamsToUrl(`${this.host}${endpoint}`, params);
return del(url, headers);
}

public async createOrder(userOrder: UserOrder): Promise<SignedOrder> {
this.canL1Auth();

Expand Down
4 changes: 4 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ export const GET_TRADES = "/trades";

// Price history
export const GET_PRICES_HISTORY = "/prices-history";

// Notifications
export const GET_TRADE_NOTIFICATIONS = "/trade-notifications";
export const DROP_TRADE_NOTIFICATIONS = "/drop-trade-notifications";
16 changes: 15 additions & 1 deletion 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, TradeParams } from "src/types";
import { FilterParams, TradeNotificationParams, TradeParams } from "src/types";
import { OpenOrderParams } from "../types";

export const GET = "GET";
Expand Down Expand Up @@ -162,3 +162,17 @@ export const addTradeParamsToUrl = (baseUrl: string, params?: TradeParams): stri
}
return url;
};

export const addTradeNotificationParamsToUrl = (
baseUrl: string,
params?: TradeNotificationParams,
): string => {
let url = baseUrl;
if (params !== undefined) {
url = `${url}?`;
if (params.index !== undefined) {
url = buildQueryParams(url, "index", params.index.toString());
}
}
return url;
};
21 changes: 21 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,24 @@ export interface MarketPrice {
v: string; // volume
ps: string; // positions
}

export interface TradeNotificationParams {
index: number;
}

export interface TradeNotification {
id: number;
owner: string;
order_id: string;
market: string;
asset_id: string;
side: string;
price: string;
original_size: string;
matched_size: string;
remaining_size: string;
outcome: string;
outcome_index: number;
action: string;
timestamp: number;
}
15 changes: 14 additions & 1 deletion tests/http-helpers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import {
addFilterParamsToUrl,
addTradeParamsToUrl,
addOpenOrderParamsToUrl,
addTradeNotificationParamsToUrl,
} from "../../src/http-helpers/index";
import { OpenOrderParams, Side, TradeParams } from "../../src";
import { OpenOrderParams, Side, TradeNotificationParams, TradeParams } from "../../src";

describe("utilities", () => {
describe("buildQueryParams", () => {
Expand Down Expand Up @@ -84,4 +85,16 @@ describe("utilities", () => {
expect(url).equal("http://tracker?market=0xaabbccdd&asset_id=10000&owner=0x69&id=1");
});
});

describe("addTradeNotificationParamsToUrl", () => {
it("checking url + params", () => {
const url = addTradeNotificationParamsToUrl("http://tracker", {
index: 1234,
} as TradeNotificationParams);
expect(url).not.null;
expect(url).not.undefined;
expect(url).not.empty;
expect(url).equal("http://tracker?index=1234");
});
});
});

0 comments on commit 3328c63

Please sign in to comment.