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

[x/TWAP] Expose a geometric TWAP API #3529

Merged
merged 7 commits into from
Dec 1, 2022
Merged

[x/TWAP] Expose a geometric TWAP API #3529

merged 7 commits into from
Dec 1, 2022

Conversation

stackman27
Copy link
Contributor

Closes: Part of #3514 [1-3]

What is the purpose of the change

We need to expose a geometric TWAP API. Its implementation would be almost identical to the arithmetic TWAP API.
Therefore, we must develop proper abstractions to reduce code duplication and avoid complexity.

Brief Changelog

n/a

Testing and Verifying

n/a

Documentation and Release Note

  • Does this pull request introduce a new feature or user-facing behavior changes? (yes / no)
  • Is a relevant changelog entry added to the Unreleased section in CHANGELOG.md? (yes / no)
  • How is the feature or change documented? (not applicable / specification (x/<module>/spec/) / Osmosis docs repo / not documented)

@stackman27 stackman27 requested review from a team and p0mvn November 26, 2022 01:53
@github-actions github-actions bot added the C:x/twap Changes to the twap module label Nov 26, 2022
x/twap/strategy.go Show resolved Hide resolved
quoteAssetDenom string,
startTime time.Time,
) (sdk.Dec, error) {
// decide whether we want to use arithmetic or geometric twap
Copy link
Member

Choose a reason for hiding this comment

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

What do you mean by "decide between arithmetic or geometric" here? I think this might confuse folks.

arithmetic strategy should always utilize the respective arithmetic methods

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isn't the idea to have a isArithmeticType bool to decide if we want to calculate arithmetic or geometric in this function?

Copy link
Member

Choose a reason for hiding this comment

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

There is no isArithmeticType boolean in this function.

If you are referring to some changes introduced in: #3420, those would actually need to be refactored to be integrated with the strategy pattern here. So the boolean parameter isArithmeticTwap in #3420 would likely be changed to taking a strategy instead.

I suggest assuming that #3420 does not exist for the context of this PR. We will integrate them after one or the other is merged

Copy link
Contributor Author

Choose a reason for hiding this comment

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

aah gotchu

x/twap/api.go Show resolved Hide resolved
x/twap/api.go Show resolved Hide resolved
@p0mvn p0mvn added the V:state/compatible/no_backport State machine compatible PR, depends on prior breaks label Nov 26, 2022
@stackman27
Copy link
Contributor Author

@p0mvn Added some simple godocs because i couldn't figure out a lot to add. lmk what you think

x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/api.go Outdated Show resolved Hide resolved
x/twap/api.go Outdated Show resolved Hide resolved
x/twap/api.go Outdated Show resolved Hide resolved
x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/api.go Outdated Show resolved Hide resolved
x/twap/api.go Outdated Show resolved Hide resolved
@p0mvn
Copy link
Member

p0mvn commented Nov 27, 2022

@p0mvn Added some simple godocs because i couldn't figure out a lot to add. lmk what you think

Added some suggestions. LGTM once addressed!

@stackman27
Copy link
Contributor Author

stackman27 commented Nov 27, 2022

@p0mvn Resolved comments bcad2b2 :D

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.

LGTM. Nice work

@stackman27
Copy link
Contributor Author

stackman27 commented Nov 29, 2022

@p0mvn rebased! might wanna take a quick skim

@p0mvn
Copy link
Member

p0mvn commented Nov 29, 2022

@czarcas7ic @nicolaslara hey, can we get one more review here please?

Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

This LGTM

quoteAssetDenom string,
startTime time.Time) (sdk.Dec, error)
// computeTwap calculates the TWAP with specific startRecord and endRecord.
computeTwap(startRecord types.TwapRecord, endRecord types.TwapRecord, quoteAsset string) (sdk.Dec, error)
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Should we unify these names: either both be called "get" or both called "compute"?

Copy link
Contributor

Choose a reason for hiding this comment

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

also. Is there a reason to have getTwapToNow be part of the strategy?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i think unifying makes sense, i'm up for naming it getTwapToNow and getTwap. @p0mvn lmk what you think of this

For getTwapToNow I was calling the keeper method directly here but i changed it to use strategy.go route

Copy link
Contributor

@nicolaslara nicolaslara left a comment

Choose a reason for hiding this comment

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

Some nits in the comments, but other than that this LGTM

x/twap/api.go Outdated
) (sdk.Dec, error) {
if startTime.After(endTime) {
return sdk.Dec{}, types.StartTimeAfterEndTimeError{StartTime: startTime, EndTime: endTime}
}
if endTime.Equal(ctx.BlockTime()) {
return k.GetArithmeticTwapToNow(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime)
return strategy.getTwapToNow(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime)
Copy link
Member

Choose a reason for hiding this comment

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

Just realized something from @nicolaslara 's comment.

The only reason we define getTwapToNow on strategy is to be able to call it here.

However, we can instead do the following:

k.getTwapToNow(ctx, poolId, baseAssetDenom, quoteAssetDenom, startTime, strategy)

and remove the getTwapToNow from strategy in general

Copy link
Contributor Author

Choose a reason for hiding this comment

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

sounds good

x/twap/strategy.go Outdated Show resolved Hide resolved
x/twap/strategy.go Outdated Show resolved Hide resolved
@@ -92,14 +122,12 @@ func (k Keeper) GetArithmeticTwapToNow(
if err != nil {
return sdk.Dec{}, err
}
return computeTwap(startRecord, endRecord, quoteAssetDenom, arithmeticTwapType)

return strategy.computeTwap(startRecord, endRecord, quoteAssetDenom)
Copy link
Member

Choose a reason for hiding this comment

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

Just commenting for discussion. I think that in the future PR, we should actually revert this and make computeTwap take the strategy itself instead of the type to improve the low-level compute logic.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we update #3514 to add it there then?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah I will update today

@p0mvn p0mvn merged commit 0d13512 into main Dec 1, 2022
@p0mvn p0mvn deleted the twap-geo branch December 1, 2022 16:58
czarcas7ic pushed a commit that referenced this pull request Jan 4, 2023
* refactored twap api.go for geometric TWAP

* added barebon docs

* romans feedback

* new

* fix

* nichola feedback

* final roman comments
czarcas7ic added a commit that referenced this pull request Jan 5, 2023
…14 (#3925)

* Upgrade IBC to v4.2.0 (#3838)

* initial changes to migrate to ibc v4

* added checksum to proposal

* begin and end block are now being called inside nextBlock

* added changelog

* linked pr on changelog

* remove local replace

* using error acks from osmoutils

* osmoutils tagged

* go sum

* added checksum

* feat(x/twap): modify cli to add geometric option (#3812)

* feat(x/twap): geometric twap code gen query boilerplate

* revert cli change

* query gen

* wire up API

* test

* fix

* fixes

* cli

* lint

* refactor via flag

* refactor

* refactor

* fixes

* lint

* add arithmetic twap alias

* Make wasm hooks importable (#3850)

* moved ibc-hooks to its own go.mod

* updated ibc hooks version

* go sum

* add ics23 patch into x/ibc-hooks

* Fix wasm import version conflict

* Update x/ibc-hooks to osmoutils v0.0.2

* Update versions

Co-authored-by: Dev Ojha <[email protected]>

* refactor(x/twap): handle spot price error case in the context of geometric twap (#3845)

* refactor(x/twap): handle spot price error case

* supporting test cases

* table-driven log tests

* test(x/twap): add randomized geometric twap test on a balancer pool (#3844)

* test(x/twap): add randomized test with a balancer pool

* comments

* multiplicative tolerance, fewer retries and larger initial supply range

* Basic geometric twap e2e test (#3835)

* feat(x/twap): geometric twap code gen query boilerplate

* revert cli change

* query gen

* wire up API

* test

* fix

* fixes

* add geometric queries

* create pool.json

* add test

* resolve conflict

* fix: swap uosmo in

* fix problem with wallet creation

* updates

* simplify and add comments

* Update tests/e2e/e2e_test.go

* Update tests/e2e/e2e_test.go

* Update tests/e2e/configurer/chain/queries.go

Co-authored-by: Adam Tucker <[email protected]>

* Update tests/e2e/configurer/chain/queries.go

Co-authored-by: Adam Tucker <[email protected]>

* Update tests/e2e/e2e_test.go

Co-authored-by: Adam Tucker <[email protected]>

* Update tests/e2e/e2e_test.go

Co-authored-by: Adam Tucker <[email protected]>

* Update tests/e2e/e2e_test.go

Co-authored-by: Adam Tucker <[email protected]>

* Update tests/e2e/e2e_test.go

Co-authored-by: Adam Tucker <[email protected]>

Co-authored-by: Roman <[email protected]>
Co-authored-by: Roman <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>

* feat(x/twap): whitelist GeometricTwap and GeometricTwapToNow (#3852)

* feat(x/twap): GeometricTwap and GeometricTwapToNow queries added to Stargate whitelist

* update docs

* fix(scripts): proto gen for osmoutils (#3854)

* fix(scripts): proto gen for osmoutils

* Update scripts/protocgen.sh

* fix(scripts): proto gen osmoutils path (#3859)

* added packet timeouts to wasm hooks (#3862)

* add negative to cli (#3888)

* Making osmoutils compile on chains that don't use our SDK fork (#3899)

* making osmoutils compile on chains that don't use osmosis' fork of the cosmos sdk

* updated imports for e2e tests

* go fumpt

* updated version everywhere

* added changelog entry

* remove deprecation from arithmetic & geometric twap to now query (#3917)

* Add types & boilerplate for the Downtime detector module (#3609)

Sub-component of #3603

Adds types for the thin module intended for downtime detection

- Add downtime detection module types

No tests added

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

* Add downtime detector module (#3688)

* WIP

* Switch to enum

* Remove params query

* Add query

* Add wiring, add import/export test

* Start begin block test

* Finish keeper tests

* Add CLI

* Wire downtime detector CLI + queries

* more module wiring

* add types test

* Fix state alteration test

* Fix superfluid test

* Add stargate whitelist support

* Apply code comment

* Rename folder

* Add requested godoc

* Update x/downtime-detector/genesis.go

Co-authored-by: Adam Tucker <[email protected]>

* Apply adam suggestion for having a `-`

* move genesis test to own file

Co-authored-by: Adam Tucker <[email protected]>

* Initial by hand fixes

* feat(osmomath): Exp2 function (#3708)

* feat(osmomath): exp2 function

* export exp2

* changelog

* refactor ErrTolerance to use Dec instead of Int for additive tolerance

* Update osmomath/exp2.go

* Update osmomath/exp2.go

* Update osmomath/exp2.go

* Update osmomath/exp2_test.go

* Update osmomath/exp2_test.go

* do bit shift instead of multiplication

* godoc about error bounds

* comment about bit shift equivalency

* merge conflict

* improve godoc

* typo

* remove TODOs - confirmed obsolete

* Runge's phenomenon comment

* [x/TWAP] Expose a geometric TWAP API  (#3529)

* refactored twap api.go for geometric TWAP

* added barebon docs

* romans feedback

* new

* fix

* nichola feedback

* final roman comments

* fix twap by hand

* change to gamm

* fix balancer test

* bump to v14 upgrade

* e2e fix

* add remaining diff from main to ibc-rate-limit

* update contracts test

* osmomath: `AddMut` and `QuoMut` (#3779)

* mut add

* test add mut

* quo  mut

* test quo mut/ remove want from test struct

* refactor exp

* change mutatives code

* change

* not allocaing

* exp change to quomut

* remove file

* refactor quo

* refactor ad

* refactor tests

* Modify CHANGELOG

* Whitelist EstimateSwapExactAmountOut (#3693)

* whitelist EstimateSwapExactAmountOut

* doc: changelog

* updated rate limit contract

* Fix rust checks (#3576)

* added cargo.lock

* added Cargo.lock as an artifact

* added new bytecode with lock file

Co-authored-by: Nicolas Lara <[email protected]>
Co-authored-by: Roman <[email protected]>
Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: Hieu Vu <[email protected]>
Co-authored-by: Roman <[email protected]>
Co-authored-by: Supanat <[email protected]>
Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: Sishir Giri <[email protected]>
Co-authored-by: Ruslan Akhtariev <[email protected]>
Co-authored-by: mattverse <[email protected]>
Co-authored-by: ByeongSu Hong <[email protected]>
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.

4 participants