diff --git a/examples/getLastTradesPrices.ts b/examples/getLastTradesPrices.ts new file mode 100644 index 0000000..0460b18 --- /dev/null +++ b/examples/getLastTradesPrices.ts @@ -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(); diff --git a/examples/getMidPoints.ts b/examples/getMidPoints.ts new file mode 100644 index 0000000..f223594 --- /dev/null +++ b/examples/getMidPoints.ts @@ -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(); diff --git a/examples/getOrderbooks.ts b/examples/getOrderbooks.ts new file mode 100644 index 0000000..4edce65 --- /dev/null +++ b/examples/getOrderbooks.ts @@ -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(); diff --git a/examples/getPrices.ts b/examples/getPrices.ts new file mode 100644 index 0000000..558407d --- /dev/null +++ b/examples/getPrices.ts @@ -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(); diff --git a/examples/socketConnection.ts b/examples/socketConnection.ts index ae4af4f..7867c22 100644 --- a/examples/socketConnection.ts +++ b/examples/socketConnection.ts @@ -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 { @@ -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]; } } @@ -73,4 +73,4 @@ async function main(type: "user" | "market" | "live-activity") { }); } -main("live-activity"); +main("market"); diff --git a/package.json b/package.json index 81e670b..8275c4a 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/client.ts b/src/client.ts index 47b5451..d805bbb 100644 --- a/src/client.ts +++ b/src/client.ts @@ -32,6 +32,7 @@ import { PaginationPayload, MarketTradeEvent, DropNotificationParams, + BookParams, } from "./types"; import { createL1Headers, createL2Headers } from "./headers"; import { @@ -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"; @@ -170,6 +175,12 @@ export class ClobClient { }); } + public async getOrderBooks(params: BookParams[]): Promise { + return this.get(`${this.host}${GET_ORDER_BOOKS}`, { + data: params, + }); + } + public async getTickSize(tokenID: string): Promise { if (tokenID in this.tickSizes) { return this.tickSizes[tokenID]; @@ -198,18 +209,36 @@ export class ClobClient { }); } + public async getMidpoints(params: BookParams[]): Promise { + return this.get(`${this.host}${GET_MIDPOINTS}`, { + data: params, + }); + } + public async getPrice(tokenID: string, side: string): Promise { return this.get(`${this.host}${GET_PRICE}`, { params: { token_id: tokenID, side: side }, }); } + public async getPrices(params: BookParams[]): Promise { + return this.get(`${this.host}${GET_PRICES}`, { + data: params, + }); + } + public async getLastTradePrice(tokenID: string): Promise { return this.get(`${this.host}${GET_LAST_TRADE_PRICE}`, { params: { token_id: tokenID }, }); } + public async getLastTradesPrices(params: BookParams[]): Promise { + return this.get(`${this.host}${GET_LAST_TRADES_PRICES}`, { + data: params, + }); + } + public async getLargeOrders(minValue = ""): Promise { return this.get(`${this.host}${GET_LARGE_ORDERS}`, { params: { min_value: minValue }, diff --git a/src/endpoints.ts b/src/endpoints.ts index 361f2a9..b0faaa6 100644 --- a/src/endpoints.ts +++ b/src/endpoints.ts @@ -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 diff --git a/src/types.ts b/src/types.ts index 8949749..fcd7487 100644 --- a/src/types.ts +++ b/src/types.ts @@ -383,3 +383,8 @@ export interface MarketTradeEvent { transaction_hash: string; timestamp: string; } + +export interface BookParams { + token_id: string; + side: Side; +}