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/src/client.ts b/src/client.ts index 4b7988f..0a4fa97 100644 --- a/src/client.ts +++ b/src/client.ts @@ -11,6 +11,7 @@ import { OpenOrdersResponse, OptionalParams, Order, + OrderMarketCancelParams, OrderBookSummary, OrderPayload, OrderType, @@ -59,6 +60,8 @@ import { GET_PRICES_HISTORY, GET_TRADE_NOTIFICATIONS, DROP_TRADE_NOTIFICATIONS, + CANCEL_ORDERS, + CANCEL_MARKET_ORDERS, } from "./endpoints"; import { OrderBuilder } from "./order-builder/builder"; @@ -389,6 +392,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; @@ -405,6 +425,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 7b5ee57..0242481 100644 --- a/src/types.ts +++ b/src/types.ts @@ -283,6 +283,11 @@ export interface TradeNotification { timestamp: number; } +export interface OrderMarketCancelParams { + market?: string; + asset_id?: string; +} + export interface OrderBookSummary { market: string; asset_id: string;