Skip to content

Commit

Permalink
Merge branch 'main' into feat/orderbook-hash
Browse files Browse the repository at this point in the history
# Conflicts:
#	package.json
#	src/client.ts
#	src/types.ts
  • Loading branch information
poly-rodr committed Feb 7, 2023
2 parents 1e58684 + f957d1e commit b8baea1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 1 deletion.
41 changes: 41 additions & 0 deletions examples/cancelMarketOrders.ts
Original file line number Diff line number Diff line change
@@ -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();
2 changes: 1 addition & 1 deletion examples/cancelOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!`);
Expand Down
29 changes: 29 additions & 0 deletions examples/cancelOrders.ts
Original file line number Diff line number Diff line change
@@ -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();
37 changes: 37 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
OpenOrdersResponse,
OptionalParams,
Order,
OrderMarketCancelParams,
OrderBookSummary,
OrderPayload,
OrderType,
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -389,6 +392,23 @@ export class ClobClient {
return del(`${this.host}${endpoint}`, headers, payload);
}

public async cancelOrders(ordersHashes: string[]): Promise<any> {
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<any> {
this.canL2Auth();
const endpoint = CANCEL_ALL;
Expand All @@ -405,6 +425,23 @@ export class ClobClient {
return del(`${this.host}${endpoint}`, headers);
}

public async cancelMarketOrders(payload: OrderMarketCancelParams): Promise<any> {
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;
Expand Down
2 changes: 2 additions & 0 deletions src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b8baea1

Please sign in to comment.