From d5a97bc607ea4303aff0fc6f9e2874d2790d6090 Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Thu, 2 Feb 2023 20:32:22 -0300 Subject: [PATCH 1/3] fix/ price rounding --- examples/order2.ts | 36 +++++++++++++++++++++++++++++ package.json | 2 +- src/utilities.ts | 9 ++++++++ tests/order-builder/helpers.test.ts | 34 +++++++++++++++++++++++++++ tests/utilities.test.ts | 8 ++++++- 5 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 examples/order2.ts diff --git a/examples/order2.ts b/examples/order2.ts new file mode 100644 index 0000000..9c42bb4 --- /dev/null +++ b/examples/order2.ts @@ -0,0 +1,36 @@ +import { ethers } from "ethers"; +import { config as dotenvConfig } from "dotenv"; +import { resolve } from "path"; +import { ApiKeyCreds, Chain, ClobClient, OrderType, Side } from "../src"; + +dotenvConfig({ path: resolve(__dirname, "../.env") }); + +async function main() { + const wallet = new ethers.Wallet(`${process.env.PK}`); + const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain; + console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`); + + const host = process.env.CLOB_API_URL || "http://localhost:8080"; + const creds: ApiKeyCreds = { + key: `${process.env.CLOB_API_KEY}`, + secret: `${process.env.CLOB_SECRET}`, + passphrase: `${process.env.CLOB_PASS_PHRASE}`, + }; + const clobClient = new ClobClient(host, chainId, wallet, creds); + + // Create a buy order for 100 YES for 0.50c + const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992"; + console.log(Side, OrderType); + + await clobClient.postOrder( + await clobClient.createOrder({ + tokenID: YES, + side: Side.SELL, + price: 0.5, + size: 10, + }), + OrderType.GTC, + ); +} + +main(); diff --git a/package.json b/package.json index 8659d17..56da05f 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.22", + "version": "1.1.23", "contributors": [ { "name": "Jonathan Amenechi", diff --git a/src/utilities.ts b/src/utilities.ts index 85f796e..13ecd34 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -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; }; diff --git a/tests/order-builder/helpers.test.ts b/tests/order-builder/helpers.test.ts index 4c1ef4c..b526c0f 100644 --- a/tests/order-builder/helpers.test.ts +++ b/tests/order-builder/helpers.test.ts @@ -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", () => { diff --git a/tests/utilities.test.ts b/tests/utilities.test.ts index 4636f03..ebda98f 100644 --- a/tests/utilities.test.ts +++ b/tests/utilities.test.ts @@ -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"; @@ -516,4 +516,10 @@ 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); + }); }); From dc8fd8ba8ba018d0a6dab5f356f705fb0e81157b Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Thu, 2 Feb 2023 20:35:13 -0300 Subject: [PATCH 2/3] removing extra file --- examples/order2.ts | 36 ------------------------------------ 1 file changed, 36 deletions(-) delete mode 100644 examples/order2.ts diff --git a/examples/order2.ts b/examples/order2.ts deleted file mode 100644 index 9c42bb4..0000000 --- a/examples/order2.ts +++ /dev/null @@ -1,36 +0,0 @@ -import { ethers } from "ethers"; -import { config as dotenvConfig } from "dotenv"; -import { resolve } from "path"; -import { ApiKeyCreds, Chain, ClobClient, OrderType, Side } from "../src"; - -dotenvConfig({ path: resolve(__dirname, "../.env") }); - -async function main() { - const wallet = new ethers.Wallet(`${process.env.PK}`); - const chainId = parseInt(`${process.env.CHAIN_ID || Chain.MUMBAI}`) as Chain; - console.log(`Address: ${await wallet.getAddress()}, chainId: ${chainId}`); - - const host = process.env.CLOB_API_URL || "http://localhost:8080"; - const creds: ApiKeyCreds = { - key: `${process.env.CLOB_API_KEY}`, - secret: `${process.env.CLOB_SECRET}`, - passphrase: `${process.env.CLOB_PASS_PHRASE}`, - }; - const clobClient = new ClobClient(host, chainId, wallet, creds); - - // Create a buy order for 100 YES for 0.50c - const YES = "1343197538147866997676250008839231694243646439454152539053893078719042421992"; - console.log(Side, OrderType); - - await clobClient.postOrder( - await clobClient.createOrder({ - tokenID: YES, - side: Side.SELL, - price: 0.5, - size: 10, - }), - OrderType.GTC, - ); -} - -main(); From cec6632864b1137d18996e5e6e31f55fb87367c8 Mon Sep 17 00:00:00 2001 From: Rodrigo <95635797+poly-rodr@users.noreply.github.com> Date: Thu, 2 Feb 2023 20:36:26 -0300 Subject: [PATCH 3/3] improving validations --- src/utilities.ts | 6 +++--- tests/utilities.test.ts | 4 ++++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/utilities.ts b/src/utilities.ts index 13ecd34..330f285 100644 --- a/src/utilities.ts +++ b/src/utilities.ts @@ -30,21 +30,21 @@ export const orderToJson = (order: SignedOrder, owner: string, orderType: OrderT }; export const roundNormal = (num: number, decimals: number): number => { - if (decimalPlaces(num) == decimals) { + 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) { + 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) { + if (decimalPlaces(num) <= decimals) { return num; } return Math.ceil(num * 10 ** decimals) / 10 ** decimals; diff --git a/tests/utilities.test.ts b/tests/utilities.test.ts index ebda98f..f37ab65 100644 --- a/tests/utilities.test.ts +++ b/tests/utilities.test.ts @@ -521,5 +521,9 @@ describe("utilities", () => { 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); }); });