osmosis
osmosis copied to clipboard
Twap: Make timeDelta for calculation in miliseconds
Background
Currently all of our time deltas in the TWAP code cast time durations to their integer representations, which is in nanoseconds. Having nanoseconds precision is very unnecessary as Tendermint forces every block time to be at least 1ms since the last, and because this costs us in precision for the Arithmetic accumulator, which we have to think about.
I suggest we make a helper method in types, for taking the difference of two times, and then rounding to the nearest milisecond, and then dividing by 10**6.
This will help save us on precision, as we start working towards overflow handling in the TWAP logic, and thinking about max spot price precisions.
Acceptance Criteria
- [ ] Implementation of previously mentioned
Hi @ValarDragon can we achieve this by doing something simple like this;
x/gamm/twap/types/utils.go
func SpotPriceTimesDuration(sp sdk.Dec, timeDelta time.Duration) sdk.Dec {
deltaMilliSecond := timeDelta.Milliseconds()
return sp.MulInt64(int64(deltaMilliSecond))
}
func AccumDiffDivDuration(accumDiff sdk.Dec, timeDelta time.Duration) sdk.Dec {
deltaMilliSecond := timeDelta.Milliseconds()
return accumDiff.QuoInt64(int64(deltaMilliSecond))
}