1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-11-15 01:34:57 +01:00

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 <mihai.scarlat@smartcontrol.ro>

* slice price

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* slice price

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* slice ocean and dt amount

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* add logs

Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>

* switch to decimal

Co-authored-by: mihaisc <mihai.scarlat@smartcontrol.ro>
This commit is contained in:
Dimo Dzhurenov 2021-05-18 11:32:19 +03:00 committed by GitHub
parent 0954613b5e
commit 4a28907fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -81,6 +81,8 @@ export default function Remove({
const [minOceanAmount, setMinOceanAmount] = useState<string>('0') const [minOceanAmount, setMinOceanAmount] = useState<string>('0')
const [minDatatokenAmount, setMinDatatokenAmount] = useState<string>('0') const [minDatatokenAmount, setMinDatatokenAmount] = useState<string>('0')
Decimal.set({ toExpNeg: -18, precision: 18, rounding: 1 })
async function handleRemoveLiquidity() { async function handleRemoveLiquidity() {
setIsLoading(true) setIsLoading(true)
try { try {
@ -157,12 +159,18 @@ export default function Remove({
]) ])
useEffect(() => { useEffect(() => {
const minOceanAmount = const minOceanAmount = new Decimal(amountOcean)
(Number(amountOcean) * (100 - Number(slippage))) / 100 .mul(new Decimal(100).minus(new Decimal(slippage)))
const minDatatokenAmount = .dividedBy(100)
(Number(amountDatatoken) * (100 - Number(slippage))) / 100 .toString()
setMinOceanAmount(`${minOceanAmount}`)
setMinDatatokenAmount(`${minDatatokenAmount}`) 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]) }, [slippage, amountOcean, amountDatatoken, isAdvanced])
// Set amountPoolShares based on set slider value // Set amountPoolShares based on set slider value
@ -173,9 +181,9 @@ export default function Remove({
const amountPoolShares = new Decimal(e.target.value) const amountPoolShares = new Decimal(e.target.value)
.dividedBy(100) .dividedBy(100)
.mul(new Decimal(poolTokens)) .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<HTMLInputElement>) { function handleMaxButton(e: ChangeEvent<HTMLInputElement>) {
@ -185,9 +193,9 @@ export default function Remove({
const amountPoolShares = new Decimal(amountMaxPercent) const amountPoolShares = new Decimal(amountMaxPercent)
.dividedBy(100) .dividedBy(100)
.mul(new Decimal(poolTokens)) .mul(new Decimal(poolTokens))
.toPrecision(18) .toString()
setAmountPoolShares(`${amountPoolShares}`) setAmountPoolShares(`${amountPoolShares.slice(0, 18)}`)
} }
function handleAdvancedButton(e: FormEvent<HTMLButtonElement>) { function handleAdvancedButton(e: FormEvent<HTMLButtonElement>) {