From 626155e2e98d0558ff7cd8129f03ee30083e485f Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Mon, 6 Feb 2023 17:04:27 -0300 Subject: [PATCH] Feat/ new order cancelation endpoints --- examples/cancelMarketOrders.ts | 41 ++++++++++++++++++++++++++++++++++ examples/cancelOrder.ts | 2 +- examples/cancelOrders.ts | 29 ++++++++++++++++++++++++ package.json | 2 +- src/client.ts | 37 ++++++++++++++++++++++++++++++ src/endpoints.ts | 2 ++ src/types.ts | 5 +++++ 7 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 examples/cancelMarketOrders.ts create mode 100644 examples/cancelOrders.ts diff --git a/examples/cancelMarketOrders.ts b/examples/cancelMarketOrders.ts new file mode 100644 index 0000000..d276a92 --- /dev/null +++ b/examples/cancelMarketOrders.ts @@ -0,0 +1,41 @@ +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); + + const YES_TOKEN_ID = + "1343197538147866997676250008839231694243646439454152539053893078719042421992"; + const NO_TOKEN_ID = + "16678291189211314787145083999015737376658799626183230671758641503291735614088"; + const CONDITION_ID = "0xbd31dc8a20211944f6b70f31557f1001557b59905b7738480ca09bd4532f84af"; + + // Send it to the server + console.log(await clobClient.cancelMarketOrders({ market: CONDITION_ID })); + console.log( + await clobClient.cancelMarketOrders({ + asset_id: YES_TOKEN_ID, + }), + ); + console.log( + await clobClient.cancelMarketOrders({ + asset_id: NO_TOKEN_ID, + }), + ); +} + +main(); diff --git a/examples/cancelOrder.ts b/examples/cancelOrder.ts index 397aca5..516416f 100644 --- a/examples/cancelOrder.ts +++ b/examples/cancelOrder.ts @@ -20,7 +20,7 @@ async function main() { // Send it to the server const resp = await clobClient.cancelOrder({ - orderID: "0xc836ee742bff164decc24de989dd1d38fa3b20b816cffd978556689660c9416b", // Order ID + orderID: "0x920cb5fc73e8cce1d8ee00c90beca3d7cc4195adc82a8c94ea96134eaefefe39", // Order ID }); console.log(resp); console.log(`Done!`); diff --git a/examples/cancelOrders.ts b/examples/cancelOrders.ts new file mode 100644 index 0000000..09b8be4 --- /dev/null +++ b/examples/cancelOrders.ts @@ -0,0 +1,29 @@ +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); + + // Send it to the server + const resp = await clobClient.cancelOrders([ + "0xd4f32972ee7fbf7f8e666bd63e02ad0cc8a23596f72ba5f0f1b3e437b8cc6459", + ]); + console.log(resp); + console.log(`Done!`); +} + +main(); diff --git a/package.json b/package.json index 56da05f..5399bd1 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@polymarket/clob-client", "description": "Typescript client for Polymarket's CLOB", - "version": "1.1.23", + "version": "1.1.24", "contributors": [ { "name": "Jonathan Amenechi", diff --git a/src/client.ts b/src/client.ts index fb36d59..eb8a47b 100644 --- a/src/client.ts +++ b/src/client.ts @@ -11,6 +11,7 @@ import { OpenOrdersResponse, OptionalParams, Order, + OrderMarketCancelParams, OrderPayload, OrderType, Side, @@ -58,6 +59,8 @@ import { GET_PRICES_HISTORY, GET_TRADE_NOTIFICATIONS, DROP_TRADE_NOTIFICATIONS, + CANCEL_ORDERS, + CANCEL_MARKET_ORDERS, } from "./endpoints"; import { OrderBuilder } from "./order-builder/builder"; @@ -379,6 +382,23 @@ export class ClobClient { return del(`${this.host}${endpoint}`, headers, payload); } + public async cancelOrders(ordersHashes: string[]): Promise { + this.canL2Auth(); + const endpoint = CANCEL_ORDERS; + const l2HeaderArgs = { + method: DELETE, + requestPath: endpoint, + body: JSON.stringify(ordersHashes), + }; + + const headers = await createL2Headers( + this.signer as Wallet | JsonRpcSigner, + this.creds as ApiKeyCreds, + l2HeaderArgs, + ); + return del(`${this.host}${endpoint}`, headers, ordersHashes); + } + public async cancelAll(): Promise { this.canL2Auth(); const endpoint = CANCEL_ALL; @@ -395,6 +415,23 @@ export class ClobClient { return del(`${this.host}${endpoint}`, headers); } + public async cancelMarketOrders(payload: OrderMarketCancelParams): Promise { + this.canL2Auth(); + const endpoint = CANCEL_MARKET_ORDERS; + const l2HeaderArgs = { + method: DELETE, + requestPath: endpoint, + body: JSON.stringify(payload), + }; + + const headers = await createL2Headers( + this.signer as Wallet | JsonRpcSigner, + this.creds as ApiKeyCreds, + l2HeaderArgs, + ); + return del(`${this.host}${endpoint}`, headers, payload); + } + private canL1Auth(): void { if (this.signer === undefined) { throw L1_AUTH_UNAVAILABLE_ERROR; diff --git a/src/endpoints.ts b/src/endpoints.ts index 1812e24..16116f9 100644 --- a/src/endpoints.ts +++ b/src/endpoints.ts @@ -18,8 +18,10 @@ export const GET_LAST_TRADE_PRICE = "/last-trade-price"; // Order endpoints export const POST_ORDER = "/order"; export const CANCEL_ORDER = "/order"; +export const CANCEL_ORDERS = "/orders"; export const GET_ORDER = "/order/"; export const CANCEL_ALL = "/cancel-all"; +export const CANCEL_MARKET_ORDERS = "/cancel-market-orders"; export const GET_LARGE_ORDERS = "/large-orders"; export const GET_OPEN_ORDERS = "/orders"; export const GET_TRADES = "/trades"; diff --git a/src/types.ts b/src/types.ts index 206d19d..0298c1b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -282,3 +282,8 @@ export interface TradeNotification { action: string; timestamp: number; } + +export interface OrderMarketCancelParams { + market?: string; + asset_id?: string; +}