Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve(create): Prevent rounding errors that unintentionally break the GCR restraint on-chain #177

Closed
wants to merge 1 commit into from

Conversation

nicholaspai
Copy link
Member

Conservatively CEIL and FLOOR the [min amount of backing collateral]/[max amount of tokens] when calling create. This trades off perfect capital efficiency for UX. We want to eliminate this oft-repeated user error where an obscure "gas required exceeeds allowance" error message occurs, which just indicates that the transaction is failing on-chain, because the front-end is incorrectly rounding numbers

Screen Shot 2020-09-27 at 16 56 21

@vercel
Copy link

vercel bot commented Sep 27, 2020

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/uma/emp-tools/csw7sudjc
✅ Preview: https://emp-tools-git-npai-rounding-errors.uma.vercel.app

@@ -96,7 +96,7 @@ const Create = () => {
// We want to round down the number for better UI display, but we don't actually want the collateral
// amount to round down since we want the minimum amount of collateral to pass the GCR constraint. So,
// we'll add a tiny amount of collateral to avoid accidentally rounding too low.
setCollateral((minBackingCollateral + 0.00005).toFixed(4));
setCollateral(Math.ceil(minBackingCollateral).toString());
Copy link
Member Author

Choose a reason for hiding this comment

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

@eng pls double check my math. If you CEIL the backing collateral, then the GCR check should always succeed right?

Copy link
Member

Choose a reason for hiding this comment

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

how many decimal points will this be CEILing to?

Copy link
Member Author

Choose a reason for hiding this comment

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

  1. If we don't care about preserving precision then we can completely avoid the GCR errors that manifest itself as "gas required exceeds allowance"

Copy link
Member

Choose a reason for hiding this comment

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

I see, that makes sense! SGTM

Copy link
Contributor

@daywiss daywiss Sep 28, 2020

Choose a reason for hiding this comment

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

can you give an example of what these could should be? I don't understand how this can even work without big numbers.

Copy link
Member

Choose a reason for hiding this comment

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

This seems okay to me... but I think ceiling to the nearest 1BTC or 1 WETH kinda defeats the point of these buttons. If we're ceiling to the nearest full unit of collateral, the max and min buttons seem like they add complexity to the dapp without adding a lot of value.

It seems like all of these issues are basically coming out of the fact that we're doing very high precision math with plain JS floating point numbers. Is it difficult to move all of these to BNs? Or is that super annoying?

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh shoot I didn't even think about the fact that 1 BTC and 1 WETH are very large numbers. This seems like a good fix for the tokens side of things, but not the collateral

@@ -141,7 +141,7 @@ const Create = () => {
const maxTokensToCreate = _gcr > 0 ? collateral / _gcr - startingTokens : 0;
// Unlike the min collateral, we're ok if we round down the tokens slightly as round down
// can only increase the position's CR and maintain it above the GCR constraint.
setTokens((maxTokensToCreate - 0.0001).toFixed(4));
setTokens(Math.floor(maxTokensToCreate).toString());
};

Copy link
Member Author

Choose a reason for hiding this comment

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

Similarly, if you FLOOR the tokens to create, the GCR will always be below your desired ratio

Copy link
Member

@mrice32 mrice32 left a comment

Choose a reason for hiding this comment

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

A bit worried that we're reducing the precision of the math to such a degree that the user would do a better job of guess-and-checking for themselves.

@nicholaspai
Copy link
Member Author

A bit worried that we're reducing the precision of the math to such a degree that the user would do a better job of guess-and-checking for themselves.

I agree, moving this to draft mode. I'm hoping to fix this without changing everything to BN's but that might be the fix. This will just be painful but better for us long term. Already mentioned this problem in #118

@nicholaspai nicholaspai marked this pull request as draft September 28, 2020 17:56
@pemulis
Copy link

pemulis commented Feb 5, 2021

Still in progress or should we close?

@fulminmaxi
Copy link

Should we close this? @nicholaspai

Copy link
Contributor

@daywiss daywiss left a comment

Choose a reason for hiding this comment

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

i dont think this really works, as matt points out, you dont want to round up if you are at 1.00001 btc. same with flooring to show the estimated max tokens. Id be a lot happier if we considering doing this as more generalized math utility which can be unit tested.

@nicholaspai nicholaspai closed this Apr 8, 2021
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.

6 participants