Skip to content

Fix edge case calculation errors in remove liquidity screen#586

Merged
kremalicious merged 10 commits into
mainfrom
fix/584
May 18, 2021
Merged

Fix edge case calculation errors in remove liquidity screen#586
kremalicious merged 10 commits into
mainfrom
fix/584

Conversation

@DimitarSD

@DimitarSD DimitarSD commented May 6, 2021

Copy link
Copy Markdown

Fixes #584 .

Changes proposed in this PR:

  • making sure any number has 18 decimals at all times, since toPrecision() alone led to errors in certain edge cases

@vercel

vercel Bot commented May 6, 2021

Copy link
Copy Markdown

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/oceanprotocol/market/6i5WZM6VZbpMJBUfmH5b8Sru5ZJN
✅ Preview: https://market-git-fix-584-oceanprotocol.vercel.app

@DimitarSD DimitarSD requested review from kremalicious and mihaisc May 6, 2021 11:04
@kremalicious

Copy link
Copy Markdown
Contributor

In my tests it looked like using toPrecision in combination with rounding down is the solution #584 (comment)

Or any reason why toFixed would be better here?

@mihaisc

mihaisc commented May 7, 2021

Copy link
Copy Markdown
Contributor

Based on documentation we should use toPrecision with rounding. toFixed will fail. Check documentation #584 (comment)

@DimitarSD

Copy link
Copy Markdown
Author

Using toPrecision() keeps breaking the code when having a low amount of shares. I'm using the exact same code from @kremalicious and I keep getting the same error. So far, I never got an error using .toFixed - it's because it counts the 0s

toPrecision digit range is.sd: number: integer, 1 to 1e+9 inclusive - it doesn't count 0s

Also, it seems that when adding a low amount of liquidity and when trying to remove it we get another conversion error with toPrecision - check issue #589

@DimitarSD DimitarSD linked an issue May 10, 2021 that may be closed by this pull request
@DimitarSD DimitarSD marked this pull request as ready for review May 10, 2021 12:28
@mihaisc

mihaisc commented May 11, 2021

Copy link
Copy Markdown
Contributor

toPrecision converts in scientific notation, use http://mikemcl.github.io/decimal.js/#toExpPos

@mihaisc

mihaisc commented May 11, 2021

Copy link
Copy Markdown
Contributor

Actually i think we need http://mikemcl.github.io/decimal.js/#toExpNeg

@DimitarSD

Copy link
Copy Markdown
Author

Neither of the two works and I keep getting the same error over and over again. I tested it with toExpPos and toExpNeg, in the cases with

  • .toString()
  • .toFixed(18, Decimal.ROUND_DOWN) / .toFixed(18)
  • .toPrecision(18, Decimal.ROUND_DOWN) / .toPrecision(18)

Best way to test:

  1. Switch the network to Polygon (or Rinkeby)
  2. Choose a dataset for which you don't have an added liquidity (or if you have, remove it first). I've been using Maritime Word List on Polygon
  3. Add the minimum amount of liquidity to a pool (0.0001 - or a close amount)
  4. Try to remove the liquidity from the pool

Using .toFixed(18, Decimal.ROUND_DOWN) / .toFixed(18) is the only time when the code doesn't fail with one of the two errors (when working with min amount or converting to wei)

@mihaisc

mihaisc commented May 12, 2021

Copy link
Copy Markdown
Contributor

ok, i'll take a look at it

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

mihaisc commented May 13, 2021

Copy link
Copy Markdown
Contributor

@DimitarSD please test this, it works for me

@DimitarSD

DimitarSD commented May 13, 2021

Copy link
Copy Markdown
Author

@mihaisc I'm getting only converting to wei error. Btw I'm testing by adding the min allowed amount for liquidity - 0.00001

Screenshot 2021-05-13 at 14 31 44

@mihaisc

mihaisc commented May 13, 2021

Copy link
Copy Markdown
Contributor

When are you substracting? Can you show me the code?

@DimitarSD

Copy link
Copy Markdown
Author

I didn't change anything in the code. I made a pull and ran the code with your latest changes. The only thing I did was to add 0.00001 liquidity to Maritime Word List on Polygon and then tried to remove it (hence the error)

@mihaisc

mihaisc commented May 13, 2021

Copy link
Copy Markdown
Contributor

Ok, i seem to replicate this as well. It looks like sometimes precision is ignored

@DimitarSD

Copy link
Copy Markdown
Author

It's not ignored, .toPrecision doesn't count 0s in the beginning after the decimal point.

If we have 0.004673258821970082 (total 18 digits after the decimal point) when using .toPrecision(18) it will count only 4673258821970082 (16 digits) - it will skip the first two 0s. And because we use .toPrecision(18) it will add two 0s at the end to make it 18. However, we then get 0.00467325882197008200 (20 digits), and when converted to a string and passed to wei conversion it breaks.

Every time we have 0s after the decimal point, and the total number of digits is close to 18 it will break (most of the time in edge cases and very small amounts)

mihaisc added 2 commits May 14, 2021 13:05
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
Signed-off-by: mihaisc <mihai.scarlat@smartcontrol.ro>
@mihaisc

mihaisc commented May 14, 2021

Copy link
Copy Markdown
Contributor

Not the best solution, but at the end of the day it's a form of rounding down.

@DimitarSD

Copy link
Copy Markdown
Author

When using the slider or MAX button now it works, however (again an edge case) when trying to remove 1% of 0.00001 or even 100% of 0.00001, it fails after the APPROVE & REMOVE is clicked. I'm not sure if this is actually coming from ocean.js

Screenshot 2021-05-14 at 14 33 12

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

mihaisc commented May 14, 2021

Copy link
Copy Markdown
Contributor

Ok, good catch. It was because ocean and dt values. Now it should be ok.

@DimitarSD

Copy link
Copy Markdown
Author

@mihaisc I get the same first error again if I try to remove 1% of 0.00001 (I don't have problems if I try to remove 100%). Also I noticed that certain percentages (like 9%) doesn't calculate properly and it basically add nothing:

Screenshot 2021-05-17 at 17 12 22

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

Copy link
Copy Markdown

Code Climate has analyzed commit 294c570 and detected 2 issues on this pull request.

Here's the issue category breakdown:

Category Count
Duplication 2

View more on Code Climate.

@DimitarSD

Copy link
Copy Markdown
Author

@mihaisc after the last updates I didn't manage to reproduce any of the bugs so far.

@kremalicious kremalicious left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could not break it anymore in my limited manual test cases so looks like we made it 🥳

@kremalicious kremalicious changed the title switched to toFixed instead of toPrecision Fix edge case calculation errors in remove liquidity screen May 18, 2021
@kremalicious kremalicious merged commit 4a28907 into main May 18, 2021
@kremalicious kremalicious deleted the fix/584 branch May 18, 2021 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding a low amount of liquidity breaks conversion when removing it Remove liquidity estimation fails on some pools

3 participants