-
Notifications
You must be signed in to change notification settings - Fork 608
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(x/twap): incorrect time delta due to nanoseconds in time #4359
Changes from 5 commits
f27357f
8763b90
cd7c639
211d94f
88367ac
f1972d1
65e5cdb
6d2571e
937d148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,16 +37,19 @@ func GetAllUniqueDenomPairs(denoms []string) []DenomPair { | |
// SpotPriceMulDuration returns the spot price multiplied by the time delta, | ||
// that is the spot price between the current and last TWAP record. | ||
// A single second accounts for 1_000_000_000 when converted to int64. | ||
func SpotPriceMulDuration(sp sdk.Dec, timeDelta time.Duration) sdk.Dec { | ||
func SpotPriceMulDuration(sp sdk.Dec, timeDeltaMs int64) sdk.Dec { | ||
return sp.MulInt64(timeDeltaMs) | ||
} | ||
|
||
func SpotPriceMulDurationOld(sp sdk.Dec, timeDelta time.Duration) sdk.Dec { | ||
p0mvn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
deltaMS := timeDelta.Milliseconds() | ||
return sp.MulInt64(deltaMS) | ||
} | ||
|
||
// AccumDiffDivDuration returns the accumulated difference divided by the the | ||
// time delta, that is the spot price between the current and last TWAP record. | ||
func AccumDiffDivDuration(accumDiff sdk.Dec, timeDelta time.Duration) sdk.Dec { | ||
deltaMS := timeDelta.Milliseconds() | ||
return accumDiff.QuoInt64(deltaMS) | ||
func AccumDiffDivDuration(accumDiff sdk.Dec, timeDeltaMs int64) sdk.Dec { | ||
return accumDiff.QuoInt64(timeDeltaMs) | ||
} | ||
|
||
// LexicographicalOrderDenoms takes two denoms and returns them to be in lexicographically ascending order. | ||
|
@@ -62,6 +65,12 @@ func LexicographicalOrderDenoms(denom0, denom1 string) (string, string, error) { | |
return denom0, denom1, nil | ||
} | ||
|
||
// CanonicalTimeMs returns the canonical time in milleseconds used for twap | ||
p0mvn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// math computations in UTC. Removes any monotonic clock reading prior to conversion to ms. | ||
func CanonicalTimeMs(twapTime time.Time) int64 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just curious, why is it called "canonical" time? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Canonical with respect to block times. It strips away any mononic part from the clock and returns the time in UTC, This should already be done given that we use |
||
return twapTime.Round(0).UnixMilli() | ||
} | ||
|
||
// DenomPair contains pair of assetA and assetB denoms which belong to a pool. | ||
type DenomPair struct { | ||
Denom0 string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we add a one line comment above here on why we use canonical time for the time delta?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this is called in multiple places, I elaborated in the method spec instead:
937d148