Skip to content

Commit

Permalink
New endpoints
Browse files Browse the repository at this point in the history
- /boks
- /midpoints
- /prices
- /last-trades-prices
  • Loading branch information
poly-rodr committed Jan 24, 2024
1 parent 93356a7 commit db53b53
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 4 deletions.
27 changes: 27 additions & 0 deletions examples/getLastTradesPrices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { BookParams, 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 clobClient = new ClobClient(host, chainId, wallet);

const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992";
const NO = "16678291189211314787145083999015737376658799626183230671758641503291735614088";

const lastTradesPrices = await clobClient.getLastTradesPrices([
{ token_id: YES },
{ token_id: NO },
] as BookParams[]);

console.log(lastTradesPrices);
}

main();
32 changes: 32 additions & 0 deletions examples/getMidPoints.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, BookParams, 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);

const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992";
const NO = "16678291189211314787145083999015737376658799626183230671758641503291735614088";

const midpoints = await clobClient.getMidpoints([
{ token_id: YES },
{ token_id: NO },
] as BookParams[]);

console.log(midpoints);
}

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

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

async function main() {
const host = process.env.CLOB_API_URL || "http://localhost:8080";
const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain;
const clobClient = new ClobClient(host, chainId);

const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992";
const NO = "16678291189211314787145083999015737376658799626183230671758641503291735614088";

const orderbooks = await clobClient.getOrderBooks([
{ token_id: YES },
{ token_id: NO },
] as BookParams[]);
console.log("orderbooks", orderbooks);
}

main();
34 changes: 34 additions & 0 deletions examples/getPrices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { ethers } from "ethers";
import { config as dotenvConfig } from "dotenv";
import { resolve } from "path";
import { ApiKeyCreds, BookParams, Chain, ClobClient, Side } 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);

const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992";
const NO = "16678291189211314787145083999015737376658799626183230671758641503291735614088";

const prices = await clobClient.getPrices([
{ token_id: YES, side: Side.BUY },
{ token_id: YES, side: Side.SELL },
{ token_id: NO, side: Side.BUY },
{ token_id: NO, side: Side.SELL },
] as BookParams[]);

console.log(prices);
}

main();
6 changes: 3 additions & 3 deletions examples/socketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { ApiKeyCreds } from "../src";

const YES_TOKEN_ID = "1343197538147866997676250008839231694243646439454152539053893078719042421992";
// eslint-disable-next-line max-len
// const NO_TOKEN_ID = "16678291189211314787145083999015737376658799626183230671758641503291735614088";
const NO_TOKEN_ID = "16678291189211314787145083999015737376658799626183230671758641503291735614088";
const CONDITION_ID = "0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af";

interface subscriptionMessage {
Expand Down Expand Up @@ -48,7 +48,7 @@ async function main(type: "user" | "market" | "live-activity") {
if (type == "user") {
subscriptionMessage["markets"] = [CONDITION_ID];
} else {
subscriptionMessage["assets_ids"] = [YES_TOKEN_ID];
subscriptionMessage["assets_ids"] = [YES_TOKEN_ID, NO_TOKEN_ID];
// subscriptionMessage["assets_ids"] = [NO_TOKEN_ID];
}
}
Expand All @@ -73,4 +73,4 @@ async function main(type: "user" | "market" | "live-activity") {
});
}

main("live-activity");
main("market");
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": "4.0.2",
"version": "4.1.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
29 changes: 29 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
PaginationPayload,
MarketTradeEvent,
DropNotificationParams,
BookParams,
} from "./types";
import { createL1Headers, createL2Headers } from "./headers";
import {
Expand Down Expand Up @@ -79,6 +80,10 @@ import {
GET_SAMPLING_SIMPLIFIED_MARKETS,
GET_SAMPLING_MARKETS,
GET_MARKET_TRADES_EVENTS,
GET_ORDER_BOOKS,
GET_MIDPOINTS,
GET_PRICES,
GET_LAST_TRADES_PRICES,
} from "./endpoints";
import { OrderBuilder } from "./order-builder/builder";

Expand Down Expand Up @@ -170,6 +175,12 @@ export class ClobClient {
});
}

public async getOrderBooks(params: BookParams[]): Promise<OrderBookSummary[]> {
return this.get(`${this.host}${GET_ORDER_BOOKS}`, {
data: params,
});
}

public async getTickSize(tokenID: string): Promise<TickSize> {
if (tokenID in this.tickSizes) {
return this.tickSizes[tokenID];
Expand Down Expand Up @@ -198,18 +209,36 @@ export class ClobClient {
});
}

public async getMidpoints(params: BookParams[]): Promise<any> {
return this.get(`${this.host}${GET_MIDPOINTS}`, {
data: params,
});
}

public async getPrice(tokenID: string, side: string): Promise<any> {
return this.get(`${this.host}${GET_PRICE}`, {
params: { token_id: tokenID, side: side },
});
}

public async getPrices(params: BookParams[]): Promise<any> {
return this.get(`${this.host}${GET_PRICES}`, {
data: params,
});
}

public async getLastTradePrice(tokenID: string): Promise<any> {
return this.get(`${this.host}${GET_LAST_TRADE_PRICE}`, {
params: { token_id: tokenID },
});
}

public async getLastTradesPrices(params: BookParams[]): Promise<any> {
return this.get(`${this.host}${GET_LAST_TRADES_PRICES}`, {
data: params,
});
}

public async getLargeOrders(minValue = ""): Promise<any> {
return this.get(`${this.host}${GET_LARGE_ORDERS}`, {
params: { min_value: minValue },
Expand Down
4 changes: 4 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@ export const GET_SIMPLIFIED_MARKETS = "/simplified-markets";
export const GET_MARKETS = "/markets";
export const GET_MARKET = "/markets/";
export const GET_ORDER_BOOK = "/book";
export const GET_ORDER_BOOKS = "/books";
export const GET_MIDPOINT = "/midpoint";
export const GET_MIDPOINTS = "/midpoints";
export const GET_PRICE = "/price";
export const GET_PRICES = "/prices";
export const GET_LAST_TRADE_PRICE = "/last-trade-price";
export const GET_LAST_TRADES_PRICES = "/last-trades-prices";
export const GET_TICK_SIZE = "/tick-size";

// Order endpoints
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,8 @@ export interface MarketTradeEvent {
transaction_hash: string;
timestamp: string;
}

export interface BookParams {
token_id: string;
side: Side;
}

0 comments on commit db53b53

Please sign in to comment.