Skip to content

Commit

Permalink
Merge pull request #73 from Polymarket/fix/price-rounding
Browse files Browse the repository at this point in the history
[PLAT-148] fix/ price rounding
  • Loading branch information
poly-rodr authored Feb 2, 2023
2 parents c869e64 + cec6632 commit c6c87ef
Show file tree
Hide file tree
Showing 4 changed files with 55 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": "1.1.22",
"version": "1.1.23",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
9 changes: 9 additions & 0 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,23 @@ export const orderToJson = (order: SignedOrder, owner: string, orderType: OrderT
};

export const roundNormal = (num: number, decimals: number): number => {
if (decimalPlaces(num) <= decimals) {
return num;
}
return Math.round((num + Number.EPSILON) * 10 ** decimals) / 10 ** decimals;
};

export const roundDown = (num: number, decimals: number): number => {
if (decimalPlaces(num) <= decimals) {
return num;
}
return Math.floor(num * 10 ** decimals) / 10 ** decimals;
};

export const roundUp = (num: number, decimals: number): number => {
if (decimalPlaces(num) <= decimals) {
return num;
}
return Math.ceil(num * 10 ** decimals) / 10 ** decimals;
};

Expand Down
34 changes: 34 additions & 0 deletions tests/order-builder/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,40 @@ describe("helpers", () => {
expect(orderData.takerAmount).to.equal("2435871700");
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.39);
});

it("correctly rounds price amounts for validity buy - 6", async () => {
const order: UserMarketOrder = {
tokenID: "123",
price: 0.56,
amount: 1,
};
const orderData: OrderData = await buildMarketBuyOrderCreationArgs(
"",
"",
SignatureType.EOA,
order,
);
expect(orderData.makerAmount).to.equal("1000000");
expect(orderData.takerAmount).to.equal("1785700");
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.39);
});

it("correctly rounds price amounts for validity buy - 7", async () => {
const order: UserMarketOrder = {
tokenID: "123",
price: 0.57,
amount: 1,
};
const orderData: OrderData = await buildMarketBuyOrderCreationArgs(
"",
"",
SignatureType.EOA,
order,
);
expect(orderData.makerAmount).to.equal("1000000");
expect(orderData.takerAmount).to.equal("1754300");
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.39);
});
});

describe("createMarketBuyOrder", () => {
Expand Down
12 changes: 11 additions & 1 deletion tests/utilities.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import "mocha";
import { expect } from "chai";
import { decimalPlaces, orderToJson } from "../src/utilities";
import { decimalPlaces, orderToJson, roundDown } from "../src/utilities";
import { Side as UtilsSide, SignatureType } from "@polymarket/order-utils";
import { Chain, OrderType, Side, UserMarketOrder, UserOrder } from "../src";
import { Wallet } from "@ethersproject/wallet";
Expand Down Expand Up @@ -516,4 +516,14 @@ describe("utilities", () => {
expect(decimalPlaces(949.9970999999999)).to.equal(13);
expect(decimalPlaces(949)).to.equal(0);
});

it("roundDown", () => {
expect(roundDown(0.55, 2)).to.equal(0.55);
expect(roundDown(0.56, 2)).to.equal(0.56);
expect(roundDown(0.57, 2)).to.equal(0.57);

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);
});
});

0 comments on commit c6c87ef

Please sign in to comment.