diff --git a/package.json b/package.json index 8275c4a..5f9d801 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@polymarket/clob-client", "description": "Typescript client for Polymarket's CLOB", - "version": "4.1.0", + "version": "4.1.1", "contributors": [ { "name": "Jonathan Amenechi", diff --git a/src/client.ts b/src/client.ts index d805bbb..1dbec35 100644 --- a/src/client.ts +++ b/src/client.ts @@ -47,7 +47,12 @@ import { RequestOptions, } from "./http-helpers"; import { L1_AUTH_UNAVAILABLE_ERROR, L2_AUTH_NOT_AVAILABLE } from "./errors"; -import { generateOrderBookSummaryHash, isTickSizeSmaller, orderToJson } from "./utilities"; +import { + generateOrderBookSummaryHash, + isTickSizeSmaller, + orderToJson, + priceValid, +} from "./utilities"; import { CANCEL_ALL, CANCEL_ORDER, @@ -457,6 +462,14 @@ export class ClobClient { const tickSize = await this._resolveTickSize(tokenID, options?.tickSize); const negRisk = options?.negRisk ?? false; + if (!priceValid(userOrder.price, tickSize)) { + throw new Error( + `invalid price (${userOrder.price}), min: ${parseFloat(tickSize)} - max: ${ + 1 - parseFloat(tickSize) + }`, + ); + } + return this.orderBuilder.buildOrder(userOrder, { tickSize, negRisk, @@ -479,6 +492,14 @@ export class ClobClient { userMarketOrder.price = parseFloat(marketPrice); } + if (!priceValid(userMarketOrder.price, tickSize)) { + throw new Error( + `invalid price (${userMarketOrder.price}), min: ${parseFloat(tickSize)} - max: ${ + 1 - parseFloat(tickSize) + }`, + ); + } + return this.orderBuilder.buildMarketOrder(userMarketOrder, { tickSize, negRisk, diff --git a/src/utilities.ts b/src/utilities.ts index 99d730c..ab2096e 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -84,3 +84,7 @@ export const generateOrderBookSummaryHash = (orderbook: OrderBookSummary): strin export const isTickSizeSmaller = (a: TickSize, b: TickSize): boolean => { return parseFloat(a) < parseFloat(b); }; + +export const priceValid = (price: number, tickSize: TickSize): boolean => { + return price >= parseFloat(tickSize) && price <= 1 - parseFloat(tickSize); +}; diff --git a/tests/utilities.test.ts b/tests/utilities.test.ts index 63accc3..8a0bc7f 100644 --- a/tests/utilities.test.ts +++ b/tests/utilities.test.ts @@ -6,6 +6,7 @@ import { generateOrderBookSummaryHash, isTickSizeSmaller, orderToJson, + priceValid, roundDown, } from "../src/utilities"; import { Side as UtilsSide, SignatureType } from "@polymarket/order-utils"; @@ -5617,4 +5618,50 @@ describe("utilities", () => { expect(isTickSizeSmaller("0.0001", "0.001")).to.be.true; expect(isTickSizeSmaller("0.0001", "0.0001")).to.be.false; }); + + it("priceValid", () => { + expect(priceValid(0.00001, "0.0001")).to.be.false; + expect(priceValid(0.0001, "0.0001")).to.be.true; + expect(priceValid(0.001, "0.0001")).to.be.true; + expect(priceValid(0.01, "0.0001")).to.be.true; + expect(priceValid(0.1, "0.0001")).to.be.true; + expect(priceValid(0.9, "0.0001")).to.be.true; + expect(priceValid(0.99, "0.0001")).to.be.true; + expect(priceValid(0.999, "0.0001")).to.be.true; + expect(priceValid(0.9999, "0.0001")).to.be.true; + expect(priceValid(0.99999, "0.0001")).to.be.false; + + expect(priceValid(0.00001, "0.001")).to.be.false; + expect(priceValid(0.0001, "0.001")).to.be.false; + expect(priceValid(0.001, "0.001")).to.be.true; + expect(priceValid(0.01, "0.001")).to.be.true; + expect(priceValid(0.1, "0.001")).to.be.true; + expect(priceValid(0.9, "0.001")).to.be.true; + expect(priceValid(0.99, "0.001")).to.be.true; + expect(priceValid(0.999, "0.001")).to.be.true; + expect(priceValid(0.9999, "0.001")).to.be.false; + expect(priceValid(0.99999, "0.001")).to.be.false; + + expect(priceValid(0.00001, "0.01")).to.be.false; + expect(priceValid(0.0001, "0.01")).to.be.false; + expect(priceValid(0.001, "0.01")).to.be.false; + expect(priceValid(0.01, "0.01")).to.be.true; + expect(priceValid(0.1, "0.01")).to.be.true; + expect(priceValid(0.9, "0.01")).to.be.true; + expect(priceValid(0.99, "0.01")).to.be.true; + expect(priceValid(0.999, "0.01")).to.be.false; + expect(priceValid(0.9999, "0.01")).to.be.false; + expect(priceValid(0.99999, "0.01")).to.be.false; + + expect(priceValid(0.00001, "0.1")).to.be.false; + expect(priceValid(0.0001, "0.1")).to.be.false; + expect(priceValid(0.001, "0.1")).to.be.false; + expect(priceValid(0.01, "0.1")).to.be.false; + expect(priceValid(0.1, "0.1")).to.be.true; + expect(priceValid(0.9, "0.1")).to.be.true; + expect(priceValid(0.99, "0.1")).to.be.false; + expect(priceValid(0.999, "0.1")).to.be.false; + expect(priceValid(0.9999, "0.1")).to.be.false; + expect(priceValid(0.99999, "0.1")).to.be.false; + }); });