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

Fix spotprice sigfig rounding for small spot prices #1699

Merged
merged 4 commits into from
Jun 7, 2022

Conversation

ValarDragon
Copy link
Member

What is the purpose of the change

Fixes query bug on mainnet, for pools with an asset with 10**18 precision. We passed all spot prices through 8 sig fig rounding, however our sig fig rounding was incorrect for inputs with value less than 1.

This fixes a bug with spot price being called on values less than 1.

Brief Changelog

  • Fixes SpotPrice rounding on extremely small inputs

Testing and Verifying

This change added a new test for a large input from mainnet, that was returning 0.
I also checked that spot price is not called anywhere in the state machine, except in the cosmwasm bindings.
We will have to ensure that no contract gets uploaded with spot price calls in the v9.x release series, which was also the general plan since contracts should depend on TWAP not spot price.

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? yes
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? yes
  • How is the feature or change documented? Correct code to the spec

@ValarDragon ValarDragon requested review from czarcas7ic and a team June 7, 2022 20:47
@github-actions github-actions bot added the C:x/gamm Changes, features and bugs related to the gamm module. label Jun 7, 2022
@ValarDragon ValarDragon added the A:backport/v9.x Do not use. backport patches to v9.x branch label Jun 7, 2022
@github-actions github-actions bot added the C:app-wiring Changes to the app folder label Jun 7, 2022
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

Nice work!

dkSigFig := dTimesK.MulInt(tenToSigFig)
numerator := dkSigFig.RoundInt().ToDec()
denominator := tenToSigFig
if k != 0 {
Copy link
Member

Choose a reason for hiding this comment

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

if k == 0, then tenTok is 1 so the conditional might be redundant

Please consider removing it. I found translating the comment above directly to code to be very helpful. At first, this check got me confused.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I guess thats fair, don't need to optimize on two multiplications right now / there are better ways to do that.


var pointOne = sdk.OneDec().QuoInt64(10)

func SigFigRound(d sdk.Dec, tenToSigFig sdk.Int) sdk.Dec {
Copy link
Member

Choose a reason for hiding this comment

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

Let's create an issue to add unit tests directly for this function

@ValarDragon ValarDragon merged commit c211999 into main Jun 7, 2022
@ValarDragon ValarDragon deleted the dev/fix_spotprice_sigfig branch June 7, 2022 22:30
mergify bot pushed a commit that referenced this pull request Jun 7, 2022
* Fix spotprice sigfig rounding for small spot prices

* Changelog

* Fix off by one in precision

* Address PR comment

(cherry picked from commit c211999)

# Conflicts:
#	CHANGELOG.md
ValarDragon added a commit that referenced this pull request Jun 7, 2022
…1703)

* Fix spotprice sigfig rounding for small spot prices (#1699)

* Fix spotprice sigfig rounding for small spot prices

* Changelog

* Fix off by one in precision

* Address PR comment

(cherry picked from commit c211999)

# Conflicts:
#	CHANGELOG.md

* Update CHANGELOG.md

Co-authored-by: Dev Ojha <[email protected]>
@github-actions github-actions bot mentioned this pull request Apr 1, 2024
@github-actions github-actions bot mentioned this pull request May 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A:backport/v9.x Do not use. backport patches to v9.x branch C:app-wiring Changes to the app folder C:x/gamm Changes, features and bugs related to the gamm module.
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants