Skip to content

Commit

Permalink
Merge pull request #105 from Polymarket/fix/price-precision-validation
Browse files Browse the repository at this point in the history
Validating order prices according to the current tick size
  • Loading branch information
poly-rodr authored Mar 1, 2024
2 parents 236f318 + 73d2614 commit 1477a56
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
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": "4.1.0",
"version": "4.1.1",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
23 changes: 22 additions & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
47 changes: 47 additions & 0 deletions tests/utilities.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
generateOrderBookSummaryHash,
isTickSizeSmaller,
orderToJson,
priceValid,
roundDown,
} from "../src/utilities";
import { Side as UtilsSide, SignatureType } from "@polymarket/order-utils";
Expand Down Expand Up @@ -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;
});
});

0 comments on commit 1477a56

Please sign in to comment.