osmosis icon indicating copy to clipboard operation
osmosis copied to clipboard

Twap: Make timeDelta for calculation in miliseconds

Open ValarDragon opened this issue 3 years ago • 1 comments

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

ValarDragon avatar Jul 23 '22 13:07 ValarDragon

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))
}

stackman27 avatar Jul 24 '22 20:07 stackman27