From 49cd032274788271f06d1c7e8c7c0143dd8ae171 Mon Sep 17 00:00:00 2001 From: Dimo Dzhurenov Date: Tue, 4 May 2021 15:56:47 +0300 Subject: [PATCH] using BigNumber instead of Number (#570) * using BigNumber instead of Number * remove wrong calculation of ocean ammount Signed-off-by: mihaisc * remove calculateAmountOfOceansRemoved * using decimal.js for remove * bump to ocean.js v0.14.8 Co-authored-by: mihaisc Co-authored-by: Matthias Kretschmann --- package-lock.json | 16 ++++++------- package.json | 3 +-- .../organisms/AssetActions/Pool/Remove.tsx | 24 +++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/package-lock.json b/package-lock.json index f408c5d21..b5efdc9a1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -3627,19 +3627,19 @@ "integrity": "sha512-p7aFIUT8RVoMzdPP7ML8G08BnQ09syywKjOT16hqJm0GmofunEuVffUXbryG4EkQ+qRbf/zeoxSmesi79kQXlA==" }, "@oceanprotocol/lib": { - "version": "0.14.7", - "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.14.7.tgz", - "integrity": "sha512-xnf2XX3VjmsvcNjLi8nwfWryTNT/t/oK+eOttOu5CrOBEm4Ghi98LKT1zX3ZlE4WObUDypaXDIkLuaxCtBVOOg==", + "version": "0.14.8", + "resolved": "https://registry.npmjs.org/@oceanprotocol/lib/-/lib-0.14.8.tgz", + "integrity": "sha512-eqab5iEgowyIM/LcDDs6xhZo/KToOmVw0betjXLG0+g70zS8R6XL2RHzCpFyutSdf/cH0w/ltPUfR8ZBElIyhQ==", "requires": { "@ethereum-navigator/navigator": "^0.5.2", - "@oceanprotocol/contracts": "^0.5.16", + "@oceanprotocol/contracts": "0.5.16", "@types/crypto-js": "^4.0.1", "cross-fetch": "^3.1.2", "crypto-js": "^4.0.0", "decimal.js": "^10.2.1", "fs": "0.0.1-security", "lzma": "^2.3.2", - "node-abort-controller": "^1.2.0", + "node-abort-controller": "^2.0.0", "save-file": "^2.3.1", "uuid": "^8.3.2", "web3": "^1.3.5", @@ -28882,9 +28882,9 @@ } }, "node-abort-controller": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-1.2.1.tgz", - "integrity": "sha512-79PYeJuj6S9+yOHirR0JBLFOgjB6sQCir10uN6xRx25iD+ZD4ULqgRn3MwWBRaQGB0vEgReJzWwJo42T1R6YbQ==" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/node-abort-controller/-/node-abort-controller-2.0.0.tgz", + "integrity": "sha512-L8RfEgjBTHAISTuagw51PprVAqNZoG6KSB6LQ6H1bskMVkFs5E71IyjauLBv3XbuomJlguWF/VnRHdJ1gqiAqA==" }, "node-addon-api": { "version": "2.0.2", diff --git a/package.json b/package.json index 3cc2fcc7a..ac5b5e886 100644 --- a/package.json +++ b/package.json @@ -27,8 +27,7 @@ "@coingecko/cryptoformat": "^0.4.2", "@loadable/component": "^5.14.1", "@oceanprotocol/art": "^3.0.0", - "@oceanprotocol/contracts": "0.5.16", - "@oceanprotocol/lib": "^0.14.7", + "@oceanprotocol/lib": "^0.14.8", "@oceanprotocol/typographies": "^0.1.0", "@portis/web3": "^3.0.3", "@sindresorhus/slugify": "^1.0.0", diff --git a/src/components/organisms/AssetActions/Pool/Remove.tsx b/src/components/organisms/AssetActions/Pool/Remove.tsx index 933efe593..06fe5e732 100644 --- a/src/components/organisms/AssetActions/Pool/Remove.tsx +++ b/src/components/organisms/AssetActions/Pool/Remove.tsx @@ -21,6 +21,7 @@ import UserLiquidity from '../../../atoms/UserLiquidity' import InputElement from '../../../atoms/Input/InputElement' import { useOcean } from '../../../../providers/Ocean' import { useWeb3 } from '../../../../providers/Web3' +import Decimal from 'decimal.js' const contentQuery = graphql` query PoolRemoveQuery { @@ -155,14 +156,6 @@ export default function Remove({ totalPoolTokens ]) - async function calculateAmountOfOceansRemoved(amountPoolShares: string) { - const oceanAmount = await ocean.pool.getOceanRemovedforPoolShares( - poolAddress, - amountPoolShares - ) - setAmountOcean(oceanAmount) - } - useEffect(() => { const minOceanAmount = (Number(amountOcean) * (100 - Number(slippage))) / 100 @@ -177,19 +170,24 @@ export default function Remove({ setAmountPercent(e.target.value) if (!poolTokens) return - const amountPoolShares = (Number(e.target.value) / 100) * Number(poolTokens) + const amountPoolShares = new Decimal(e.target.value) + .dividedBy(100) + .mul(new Decimal(poolTokens)) + .toPrecision(18) // in some cases the returned value contain more than 18 digits which break conversion to wei + setAmountPoolShares(`${amountPoolShares}`) - calculateAmountOfOceansRemoved(`${amountPoolShares}`) } function handleMaxButton(e: ChangeEvent) { e.preventDefault() setAmountPercent(amountMaxPercent) - const amountPoolShares = - (Number(amountMaxPercent) / 100) * Number(poolTokens) + const amountPoolShares = new Decimal(amountMaxPercent) + .dividedBy(100) + .mul(new Decimal(poolTokens)) + .toPrecision(18) + setAmountPoolShares(`${amountPoolShares}`) - calculateAmountOfOceansRemoved(`${amountPoolShares}`) } function handleAdvancedButton(e: FormEvent) {