-
Notifications
You must be signed in to change notification settings - Fork 601
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): implement reusable TWAP API #3514
Comments
The PR split could be done in the following way to limit the scope and simplify reviews:
First PR (1-3) can be started immediately while the rest is blocked on #3420 |
@stackman27 please let me know if you're interested in picking this up Would also appreciate reviews of the proposal from anyone to agree on the direction |
Hi @p0mvn yea i can give points 1-3 a shot |
This looks good to me. I don't expect other TWAP strategies to exist, but I like the abstraction pattern so that we can use it on other parts of the codebase |
Sweet, thanks for reviewing @nicolaslara . Could you please take a look at the first PR towards this when you have the time: #3529 |
@stackman27 FYI, updated the A/C by adding a step 4 to cover changes we discussed in #3529 |
closed by #3696 , thanks @stackman27 |
Background
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.
Consider the current implementation of
GetArithmeticTWAP()
:With the
GetGeometricTWAP()
, all code would be similar besides the lines marked as "difference" in comments. Even the lines that differ would have the same API.That is, in the naive code-duplication implementation:
GetArithmeticTwapToNow
would becomeGetGeometricTwapToNow
computeArithmeticTwap
would becomecomputeGeometricTwap
taking the same arguments.
Suggested Design
To avoid code duplication we should consider using strategy pattern.
Then, we can extract a reusable function that takes strategy as a parameter:
and the existing
GetArithmeticTwap
exported function would become:Similarly, we should be able to introduce
geometric
strategy and refactorGetArithmaticTwapToNow
Acceptance Criteria
twapStrategy
interface is introduced in `strategy.goarithmetic
implementation is added tostrategy.go
geometric
implementation is added tostrategy.go
computeTwap
to taketwapStrategy
instead oftwapType
.computeGeometrictTwap
to its respective strategy'scomputeTwap
.computeArithmeticTwap
to its respective strategy'scomputeTwap
.getTwap
function is introduced toapi.go
getArithmaticTwap
arithmetic
andgeometric
strategies, minimizing code duplication in testsgetArithmeticTwap
utilizesarithmetic
strategy and callsgetTwap
getTwap
getTwapToNow
function is introduced toapi.go
getArithmaticTwapToNow
arithmetic
andGeometric
strategies, minimizing code duplication in testsgetArithmeticTwapToNow
utilizesarithmetic
strategy and callsgetTwapToNow
getTwap
getGeometricTwap
andgetGeometricTwapToNow
The text was updated successfully, but these errors were encountered: