Skip to content
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

[PLAT-420] New FE notifications service #99

Merged
merged 3 commits into from
Sep 20, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ async function main() {
const clobClient = new ClobClient(host, chainId, wallet, creds);

console.log(
await clobClient.getTradeNotifications({
index: 0,
await clobClient.dropNotifications({
ids: ["3"],
}),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ async function main() {
};
const clobClient = new ClobClient(host, chainId, wallet, creds);

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

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": "3.1.0",
"version": "3.2.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
29 changes: 17 additions & 12 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import {
OrderType,
Side,
Trade,
TradeNotification,
TradeNotificationParams,
Notification,
TradeParams,
UserMarketOrder,
UserOrder,
Expand All @@ -31,13 +30,15 @@ import {
PriceHistoryFilterParams,
PaginationPayload,
MarketTradeEvent,
DropNotificationParams,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
del,
DELETE,
GET,
get,
parseDropNotificationParams,
parseOrdersScoringParams,
POST,
post,
Expand Down Expand Up @@ -65,8 +66,8 @@ import {
GET_MARKETS,
GET_MARKET,
GET_PRICES_HISTORY,
GET_TRADE_NOTIFICATIONS,
DROP_TRADE_NOTIFICATIONS,
GET_NOTIFICATIONS,
DROP_NOTIFICATIONS,
CANCEL_ORDERS,
CANCEL_MARKET_ORDERS,
GET_BALANCE_ALLOWANCE,
Expand Down Expand Up @@ -353,12 +354,10 @@ export class ClobClient {
return this.get(`${this.host}${endpoint}`, { headers, params });
}

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

const endpoint = GET_TRADE_NOTIFICATIONS;
const endpoint = GET_NOTIFICATIONS;
const headerArgs = {
method: GET,
requestPath: endpoint,
Expand All @@ -370,13 +369,16 @@ export class ClobClient {
headerArgs,
);

return this.get(`${this.host}${endpoint}`, { headers, params });
return this.get(`${this.host}${endpoint}`, {
headers,
params: { signature_type: this.orderBuilder.signatureType },
});
}

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

const endpoint = DROP_TRADE_NOTIFICATIONS;
const endpoint = DROP_NOTIFICATIONS;
const l2HeaderArgs = {
method: DELETE,
requestPath: endpoint,
Expand All @@ -388,7 +390,10 @@ export class ClobClient {
l2HeaderArgs,
);

return this.del(`${this.host}${endpoint}`, { headers, params });
return this.del(`${this.host}${endpoint}`, {
headers,
params: parseDropNotificationParams(params),
});
}

public async getBalanceAllowance(
Expand Down
4 changes: 2 additions & 2 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const ARE_ORDERS_SCORING = "/orders-scoring";
export const GET_PRICES_HISTORY = "/prices-history";

// Notifications
export const GET_TRADE_NOTIFICATIONS = "/trade-notifications";
export const DROP_TRADE_NOTIFICATIONS = "/drop-trade-notifications";
export const GET_NOTIFICATIONS = "/notifications";
export const DROP_NOTIFICATIONS = "/notifications";

// Balance
export const GET_BALANCE_ALLOWANCE = "/balance-allowance";
Expand Down
14 changes: 13 additions & 1 deletion src/http-helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import axios, { AxiosRequestHeaders, Method } from "axios";
import { OrdersScoringParams } from "src/types";
import { DropNotificationParams, OrdersScoringParams } from "src/types";
import { isBrowser } from "browser-or-node";

export const GET = "GET";
Expand Down Expand Up @@ -89,3 +89,15 @@ export const parseOrdersScoringParams = (orderScoringParams?: OrdersScoringParam
}
return params;
};

export const parseDropNotificationParams = (
dropNotificationParams?: DropNotificationParams,
): QueryParams => {
const params: QueryParams = {};
if (dropNotificationParams !== undefined) {
if (dropNotificationParams.ids !== undefined) {
params["ids"] = dropNotificationParams?.ids.join(",");
}
}
return params;
};
24 changes: 6 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,27 +276,15 @@ export enum PriceHistoryInterval {
ONE_HOUR = "1h",
}

export interface TradeNotificationParams {
index: number;
export interface DropNotificationParams {
ids: string[];
}

export interface TradeNotification {
id: number;
export interface Notification {
type: number;
owner: string;
order_id: string;
market: string;
asset_id: string;
side: string;
type: string;
price: string;
original_size: string;
matched_size: string;
remaining_size: string;
outcome: string;
outcome_index: number;
action: string;
timestamp: number;
transaction_hash: OrderType;

payload: any;
}

export interface OrderMarketCancelParams {
Expand Down
18 changes: 16 additions & 2 deletions tests/http-helpers/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import "mocha";
import { expect } from "chai";
import { parseOrdersScoringParams } from "../../src/http-helpers/index";
import { OrdersScoringParams } from "../../src";
import {
parseDropNotificationParams,
parseOrdersScoringParams,
} from "../../src/http-helpers/index";
import { DropNotificationParams, OrdersScoringParams } from "../../src";

describe("utilities", () => {
describe("parseOrdersScoringParams", () => {
Expand All @@ -15,4 +18,15 @@ describe("utilities", () => {
expect(params).deep.equal({ order_ids: "0x0,0x1,0x2" });
});
});
describe("parseDropNotificationParams", () => {
it("checking params", () => {
const params = parseDropNotificationParams({
ids: ["0", "1", "2"],
} as DropNotificationParams);
expect(params).not.null;
expect(params).not.undefined;
expect(params).not.empty;
expect(params).deep.equal({ ids: "0,1,2" });
});
});
});
Loading