*3.1. Arithmetic Mean Time-Weighted Average Price*

The arithmetic mean TWAP over *n* price updates is calculated as follows:

$$TWAP = \frac{\sum\_{i=1}^{n} t\_i p\_i}{\sum\_{i=1}^{n} t\_i} \tag{9}$$

where *ti* is the time elapsed between the price update *i* and next price update *i* + 1, and *pi* is the price during that period. *n* is the averaging window.

We estimate the effect of manipulation on the TWAP price when the attacker wants to consistently manipulate the spot price for *m* times of price updates within the averaging window *n*.

$$TWAP\_{\mathfrak{m}} = \frac{\sum\_{i=1}^{n-m} t\_i p\_i + \sum\_{j=n-m+1}^{n} t\_j p\_j}{\sum\_{i=1}^{n} t\_i} \tag{10}$$

assuming that attack would happen in the last *m* blocks. From here, we would like to estimate the *pj*—how big should the manipulated price be that the attacker should target in order to achieve the desired effect on *TWAPm*.

$$\sum\_{j=0}^{m} t\_j p\_j = TWAP\_m \times \sum\_{i=1}^{n} t\_i - \sum\_{i=1}^{n-m} t\_i p\_i \tag{11}$$

In the case when the attacker does not want to be exposed to arbitrageurs and wants to manipulate the price within one block *m* = 1, the manipulated price will be as follows:

$$p\_j = \frac{TWAP\_m \times \sum\_{i=1}^{n} t\_i - \sum\_{i=1}^{n-m} t\_i p\_i}{t\_j} \tag{12}$$

Although using the TWAP instead of a spot price is safer in terms of avoiding the malicious price manipulations, the output from the averaging might not be accurate.
