Skip to content

Commit

Permalink
Merge pull request #66 from Polymarket/fix/amounts-rounding
Browse files Browse the repository at this point in the history
New float numbers handling improvement
  • Loading branch information
poly-rodr authored Jan 13, 2023
2 parents 6337f9e + 25291fe commit 9cfa43e
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 16 deletions.
17 changes: 17 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"editor.formatOnSave": true,
"files.eol": "\n",
"mochaExplorer.files": "tests/**/*.test.ts",
"mochaExplorer.require": [
"ts-node/register",
"tsconfig-paths/register",
"jsdom-global/register"
],
"mochaExplorer.monkeyPatch": false,
"mochaExplorer.timeout": 60000,
"mochaExplorer.env": {
"TS_NODE_PROJECT": "tsconfig.json",
"TS_NODE_COMPILER_OPTIONS": "{\"module\": \"commonjs\" }"
},
"editor.defaultFormatter": "esbenp.prettier-vscode"
}
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.16",
"version": "1.1.17",
"contributors": [
{
"name": "Jonathan Amenechi",
Expand Down
19 changes: 5 additions & 14 deletions src/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,10 @@ export const roundUp = (num: number, decimals: number): number => {
return Math.ceil(num * 10 ** decimals) / 10 ** decimals;
};

export const decimalPlaces = (n: number): number => {
const isInt = (n: number) => {
return (
typeof n === "number" &&
parseFloat(n.toString()) == parseInt(n.toString(), 10) &&
!isNaN(n)
);
};

let c = n;
let count = 1;
while (!isInt(c) && isFinite(c)) {
c = n * Math.pow(10, count++);
export const decimalPlaces = (num: number): number => {
if (Number.isInteger(num)) {
return 0;
}
return count - 1;
const arr = num.toString().split(".");
return arr[1].length;
};
53 changes: 53 additions & 0 deletions tests/order-builder/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,24 @@ describe("helpers", () => {
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.78);
});

it("correctly rounds price amounts for validity buy - 5", async () => {
const order: UserOrder = {
tokenID: "123",
size: 2435.89,
price: 0.39,
side: Side.BUY,
};
const orderData: OrderData = await buildOrderCreationArgs(
"",
"",
SignatureType.EOA,
order,
);
expect(orderData.makerAmount).to.equal("949997100");
expect(orderData.takerAmount).to.equal("2435890000");
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.39);
});

it("correctly rounds price amounts for validity sell", async () => {
const order: UserOrder = {
tokenID: "123",
Expand Down Expand Up @@ -354,6 +372,24 @@ describe("helpers", () => {
expect(orderData.takerAmount).to.equal("9999600");
expect(Number(orderData.takerAmount) / Number(orderData.makerAmount)).to.gte(0.78);
});

it("correctly rounds price amounts for validity sell - 5", async () => {
const order: UserOrder = {
tokenID: "123",
size: 2435.89,
price: 0.39,
side: Side.SELL,
};
const orderData: OrderData = await buildOrderCreationArgs(
"",
"",
SignatureType.EOA,
order,
);
expect(orderData.makerAmount).to.equal("2435890000");
expect(orderData.takerAmount).to.equal("949997100");
expect(Number(orderData.takerAmount) / Number(orderData.makerAmount)).to.gte(0.39);
});
});

describe("createOrder", () => {
Expand Down Expand Up @@ -570,6 +606,23 @@ describe("helpers", () => {
expect(orderData.takerAmount).to.equal("12807600");
expect(Number(orderData.makerAmount) / Number(orderData.takerAmount)).to.gte(0.78);
});

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

describe("createMarketBuyOrder", () => {
Expand Down
6 changes: 5 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 { orderToJson } from "../src/utilities";
import { decimalPlaces, orderToJson } 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 @@ -511,4 +511,8 @@ describe("utilities", () => {
});
});
});

it("decimalPlaces", () => {
expect(decimalPlaces(949.9970999999999)).to.equal(13);
});
});

0 comments on commit 9cfa43e

Please sign in to comment.