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

feat(x/twap): use exp2 in twapPow; add overflow test #3809

Merged
merged 6 commits into from
Dec 21, 2022

Conversation

p0mvn
Copy link
Member

@p0mvn p0mvn commented Dec 21, 2022

Closes: #3540

What is the purpose of the change

This PR switches to use the new Exp2 function (#3708) in x/twap for computing geometric twap.

It also adds a test at max spot price, showing that there is no overflow.

Testing and Verifying

This change added overflow test and refactored twap math function tests to be simpler.

Note that adding more hand-calculated tests is tracked in: #3541

On top of this PR, I made a PR with query tests that currently pass: #3802

Documentation and Release Note

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

@github-actions github-actions bot added the C:x/twap Changes to the twap module label Dec 21, 2022
@p0mvn p0mvn added the V:state/compatible/no_backport State machine compatible PR, depends on prior breaks label Dec 21, 2022
x/twap/strategy_test.go Outdated Show resolved Hide resolved
Comment on lines +269 to +271
if exponent.IsNegative() {
return osmomath.OneDec().Quo(exp2).SDKDec()
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Note: this is possible if sp0 is < 1

Currently, there is a micro-optimization possible to avoid taking 1 / result twice. This happens when:

arithmeticMeanOfLogPrices.IsNegative() && quoteAsset == startRecord.Asset1Denom

I'm planning to revisit this in the future

Copy link
Member Author

Choose a reason for hiding this comment

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

tracked in: #3541

@p0mvn p0mvn marked this pull request as ready for review December 21, 2022 02:41
@ValarDragon
Copy link
Member

ValarDragon commented Dec 21, 2022

amazing the TWAP testing time spent in pow has 10x decreased, great sign for our Exp2 fn :)

x/twap/logic.go Outdated Show resolved Hide resolved
RoundingDir: osmomath.RoundDown,
}

oneYear := OneSec.MulInt64(60 * 60 * 24 * 365)
Copy link
Member

Choose a reason for hiding this comment

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

Can we test w/ 100 years 👀

Copy link
Member

Choose a reason for hiding this comment

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

Do we also need to test min spot price, since this can get underflow? (or is that a future PR?)

Copy link
Member

Choose a reason for hiding this comment

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

(not blocking merge, as long as we track in an issue otherwise)

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Tested with 100 years - will address underflow in #3815

Copy link
Member

@ValarDragon ValarDragon left a comment

Choose a reason for hiding this comment

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

LGTM aside from my overflow & underflow comment!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C:x/twap Changes to the twap module V:state/compatible/no_backport State machine compatible PR, depends on prior breaks
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

test(twap): add overflow tests for geometric TWAP
2 participants