From 4a28907fe330dcd5e8de153ac16fc494c1f3cebc Mon Sep 17 00:00:00 2001 From: Dimo Dzhurenov Date: Tue, 18 May 2021 11:32:19 +0300 Subject: [PATCH] Fix edge case calculation errors in remove liquidity screen (#586) * switched to toFixed instead of toPrecision * add round down parameter toFixed * removed console log * added toExpPos * set decimal precision Signed-off-by: mihaisc * slice price Signed-off-by: mihaisc * slice price Signed-off-by: mihaisc * slice ocean and dt amount Signed-off-by: mihaisc * add logs Signed-off-by: mihaisc * switch to decimal Co-authored-by: mihaisc --- .../organisms/AssetActions/Pool/Remove.tsx | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/components/organisms/AssetActions/Pool/Remove.tsx b/src/components/organisms/AssetActions/Pool/Remove.tsx index 06fe5e732..1d1443c4b 100644 --- a/src/components/organisms/AssetActions/Pool/Remove.tsx +++ b/src/components/organisms/AssetActions/Pool/Remove.tsx @@ -81,6 +81,8 @@ export default function Remove({ const [minOceanAmount, setMinOceanAmount] = useState('0') const [minDatatokenAmount, setMinDatatokenAmount] = useState('0') + Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 }) + async function handleRemoveLiquidity() { setIsLoading(true) try { @@ -157,12 +159,18 @@ export default function Remove({ ]) useEffect(() => { - const minOceanAmount = - (Number(amountOcean) * (100 - Number(slippage))) / 100 - const minDatatokenAmount = - (Number(amountDatatoken) * (100 - Number(slippage))) / 100 - setMinOceanAmount(`${minOceanAmount}`) - setMinDatatokenAmount(`${minDatatokenAmount}`) + const minOceanAmount = new Decimal(amountOcean) + .mul(new Decimal(100).minus(new Decimal(slippage))) + .dividedBy(100) + .toString() + + const minDatatokenAmount = new Decimal(amountDatatoken) + .mul(new Decimal(100).minus(new Decimal(slippage))) + .dividedBy(100) + .toString() + + setMinOceanAmount(minOceanAmount.slice(0, 18)) + setMinDatatokenAmount(minDatatokenAmount.slice(0, 18)) }, [slippage, amountOcean, amountDatatoken, isAdvanced]) // Set amountPoolShares based on set slider value @@ -173,9 +181,9 @@ export default function Remove({ 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 + .toString() - setAmountPoolShares(`${amountPoolShares}`) + setAmountPoolShares(`${amountPoolShares.slice(0, 18)}`) } function handleMaxButton(e: ChangeEvent) { @@ -185,9 +193,9 @@ export default function Remove({ const amountPoolShares = new Decimal(amountMaxPercent) .dividedBy(100) .mul(new Decimal(poolTokens)) - .toPrecision(18) + .toString() - setAmountPoolShares(`${amountPoolShares}`) + setAmountPoolShares(`${amountPoolShares.slice(0, 18)}`) } function handleAdvancedButton(e: FormEvent) {