Skip to content

Commit

Permalink
Merge pull request #70 from Polymarket/feat/orderbook-hash
Browse files Browse the repository at this point in the history
Feat/ adding orderbook summary hash support
  • Loading branch information
poly-rodr authored Feb 8, 2023
2 parents f957d1e + b8baea1 commit b7b1399
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 22 deletions.
23 changes: 7 additions & 16 deletions examples/getOrderbook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,13 @@ 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);
console.log(
await clobClient.getOrderBook(
"16678291189211314787145083999015737376658799626183230671758641503291735614088", // NO
),
);
console.log(
await clobClient.getOrderBook(
"1343197538147866997676250008839231694243646439454152539053893078719042421992", // YES
),
);
/*
{
asks: [ { price: '0.60', size: '100', side: 'sell' } ],
bids: [ { price: '0.40', size: '100', side: 'buy' } ]
}
*/
const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992";

const orderbook = await clobClient.getOrderBook(YES);
console.log("orderbook", orderbook);

const hash = clobClient.getOrderBookHash(orderbook);
console.log("orderbook hash", hash);
}

main();
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": "1.1.24",
"version": "1.2.0",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
14 changes: 12 additions & 2 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
OptionalParams,
Order,
OrderMarketCancelParams,
OrderBookSummary,
OrderPayload,
OrderType,
Side,
Expand All @@ -36,7 +37,7 @@ import {
post,
} from "./http-helpers";
import { L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors";
import { orderToJson } from "./utilities";
import { generateOrderBookSummaryHash, orderToJson } from "./utilities";
import {
CANCEL_ALL,
CANCEL_ORDER,
Expand Down Expand Up @@ -119,10 +120,19 @@ export class ClobClient {
return get(`${this.host}${GET_MARKET}${conditionID}`);
}

public async getOrderBook(tokenID: string): Promise<any> {
public async getOrderBook(tokenID: string): Promise<OrderBookSummary> {
return get(`${this.host}${GET_ORDER_BOOK}?token_id=${tokenID}`);
}

/**
* Calculates the hash for the given orderbook
* @param orderbook
* @returns
*/
public getOrderBookHash(orderbook: OrderBookSummary): string {
return generateOrderBookSummaryHash(orderbook);
}

public async getMidpoint(tokenID: string): Promise<any> {
return get(`${this.host}${GET_MIDPOINT}?token_id=${tokenID}`);
}
Expand Down
13 changes: 13 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,3 +287,16 @@ export interface OrderMarketCancelParams {
market?: string;
asset_id?: string;
}

export interface OrderBookSummary {
market: string;
asset_id: string;
bids: OrderSummary[];
asks: OrderSummary[];
hash: string;
}

export interface OrderSummary {
price: string;
size: string;
}
15 changes: 14 additions & 1 deletion src/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Side as UtilsSide, SignedOrder } from "@polymarket/order-utils";
import { NewOrder, OrderType, Side } from "./types";
import { createHash } from "crypto";
import { NewOrder, OrderBookSummary, OrderType, Side } from "./types";

export const orderToJson = (order: SignedOrder, owner: string, orderType: OrderType): NewOrder => {
let side = Side.BUY;
Expand Down Expand Up @@ -62,3 +63,15 @@ export const decimalPlaces = (num: number): number => {

return arr[1].length;
};

/**
* Calculates the hash for the given orderbook
* @param orderbook
* @returns
*/
export const generateOrderBookSummaryHash = (orderbook: OrderBookSummary): string => {
orderbook.hash = "";
const hash = createHash("sha1").update(JSON.stringify(orderbook)).digest("hex");
orderbook.hash = hash;
return hash;
};
59 changes: 57 additions & 2 deletions tests/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import "mocha";
import { expect } from "chai";
import { decimalPlaces, orderToJson, roundDown } from "../src/utilities";
import { decimalPlaces, generateOrderBookSummaryHash, orderToJson, roundDown } from "../src/utilities";
import { Side as UtilsSide, SignatureType } from "@polymarket/order-utils";
import { Chain, OrderType, Side, UserMarketOrder, UserOrder } from "../src";
import { Chain, OrderBookSummary, OrderType, Side, UserMarketOrder, UserOrder } from "../src";
import { Wallet } from "@ethersproject/wallet";
import { createMarketBuyOrder, createOrder } from "../src/order-builder/helpers";

Expand Down Expand Up @@ -525,5 +525,60 @@ describe("utilities", () => {
expect(roundDown(0.55, 4)).to.equal(0.55);
expect(roundDown(0.56, 4)).to.equal(0.56);
expect(roundDown(0.57, 4)).to.equal(0.57);
});

it("generateOrderBookSummaryHash", () => {
let orderbook = {
market: "0xaabbcc",
asset_id: "100",
bids: [
{ price: "0.3", size: "100" },
{ price: "0.4", size: "100" },
],
asks: [
{ price: "0.6", size: "100" },
{ price: "0.7", size: "100" },
],
hash: "",
} as OrderBookSummary;

expect(generateOrderBookSummaryHash(orderbook)).to.equal(
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
);
expect(orderbook.hash).to.equal("b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5");

// -
orderbook = {
market: "0xaabbcc",
asset_id: "100",
bids: [
{ price: "0.3", size: "100" },
{ price: "0.4", size: "100" },
],
asks: [
{ price: "0.6", size: "100" },
{ price: "0.7", size: "100" },
],
hash: "b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
} as OrderBookSummary;

expect(generateOrderBookSummaryHash(orderbook)).to.equal(
"b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5",
);
expect(orderbook.hash).to.equal("b8b72c72c6534d1b3a4e7fb47b81672d0e94d5a5");

// -
orderbook = {
market: "0xaabbcc",
asset_id: "100",
bids: [],
asks: [],
hash: "",
} as OrderBookSummary;

expect(generateOrderBookSummaryHash(orderbook)).to.equal(
"7f81a35a09e1933a96b05edb51ac4be4a6163146",
);
expect(orderbook.hash).to.equal("7f81a35a09e1933a96b05edb51ac4be4a6163146");
});
});

0 comments on commit b7b1399

Please sign in to comment.