Custom Oracle Implementations

Time-Weighted Average Price (TWAP) Calculation uses geometric mean for price calculation with the formula TWAP=(i=t1+1t2Pi)1t2t1TWAP = \left( \prod_{i = t_1 + 1}^{t_2} P_i \right)^{\frac{1}{t_2 - t_1}}, which can be computationally optimized using logarithms as

TWAP=exp(1t2t1i=t1+1t2ln(Pi))TWAP = \exp \left( \frac{1}{t_2 - t_1} \sum_{i = t_1 + 1}^{t_2} \ln(P_i) \right)

Price Accumulator Method provides an efficient implementation where TWAP=1.0001a(t2)a(t1)t2t1TWAP = 1.0001^{\frac{a(t_2) - a(t_1)}{t_2 - t_1}} witha(t)=i=0ttickia(t) = \sum_{i=0}^{t} tick_i representing the price accumulator and the fraction representing the time-weighted average tick.

Arithmetic Mean TWAP (Alternative) is also supported using TWAParithmetic=1tnt0i=0n1Pi(ti+1ti)TWAP_{arithmetic} = \frac{1}{t_n - t_0} \sum_{i=0}^{n-1} P_i (t_{i+1} - t_i) for comparison purposes, providing a different calculation methodology

Geometric mean approach is preferred over arithmetic mean because it provides better resistance to price manipulation and flash loan attacks, making it more suitable for DeFi oracle implementations.

Implementation flexibility allows the oracle system to use configurable time windows for different use cases, enabling protocols to balance between price accuracy and manipulation resistance based on their specific requirements.

Last updated