From 25291fe2e9586e3304832d9dd8dfc98efca80ede Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Fri, 13 Jan 2023 12:40:29 -0300 Subject: [PATCH] New float numbers handling improvement --- .vscode/settings.json | 17 +++++++++ package.json | 2 +- src/utilities.ts | 19 +++-------- tests/order-builder/helpers.test.ts | 53 +++++++++++++++++++++++++++++ tests/utilities.test.ts | 6 +++- 5 files changed, 81 insertions(+), 16 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3d31174 --- /dev/null +++ b/.vscode/settings.json @@ -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" +} diff --git a/package.json b/package.json index c6d8600..93d2171 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/utilities.ts b/src/utilities.ts index 1685957..808fe05 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -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; }; diff --git a/tests/order-builder/helpers.test.ts b/tests/order-builder/helpers.test.ts index cd4b5ed..d09274e 100644 --- a/tests/order-builder/helpers.test.ts +++ b/tests/order-builder/helpers.test.ts @@ -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", @@ -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", () => { @@ -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", () => { diff --git a/tests/utilities.test.ts b/tests/utilities.test.ts index e82f0ee..c3468ee 100644 --- a/tests/utilities.test.ts +++ b/tests/utilities.test.ts @@ -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"; @@ -511,4 +511,8 @@ describe("utilities", () => { }); }); }); + + it("decimalPlaces", () => { + expect(decimalPlaces(949.9970999999999)).to.equal(13); + }); });