*4.1. Algorithmic Model*

Algorithm 1 shows step by step how to estimate the safety of TWAP oracle. Model outputs are attack cost *AC* and minimum collateral *C* an attacker needs to profit from their manipulation. Knowing these parameters allows us to estimate the economic feasibility of such an attack and, therefore, to decide whether it is safe to implement the given DEX as an oracle. Input parameters needed for the model can be divided as protocol specific, oracle specific (averaging window size *WS*) and DEX specific (liquidity *L*). These parameters can be easily found in protocol's web-page and in DEXs page (*L*). To find values of Δ*x* (number of tokens needed to move the price to the target value) and Δ*y* (number of tokens to receive in return after the swap), one needs to know the type of AMM that DEX uses to price assets. In Appendices A and B, we have derived equation for Δ*x* and Δ*y* for constant product and stableswap AMMs. Overall, all these parameters can be precisely found and no assumptions need to be made. There is one more parameter that is crucial to take into account—the number of blocks *m* during which attacker will try to manipulate the price. This can not be known upfront but its value affects the cost of an attack significantly. One would suppose that the value of *m* should be small because any deviation from the spot price would be noticed by arbitrageurs and set back to the real value, not allowing an attacker to manipulate for many blocks. However, in pools with infrequent trading activity, the arbitrage opportunity might go unnoticed for longer times. Moreover, there is a chance of multi-block MEV-style attack, where an attacker could cooperate with the miner to mine a few blocks in a row. This style of attack combined with the oracle price manipulation makes the oracle attack cost cheaper. In the examples and simulations below, we assume no MMEV attack and frequent trading activity in a pool.

**Algorithm 1:** Model to estimate the safety of DEX-based oracle

**Input:** Protocol risk parameters, *WS*, *m*, Δ*x*, Δ*y*, *L* **Output:** *AC*, *C*


$$TWAP\_{\mathfrak{m}} = TWAP\_{\mathfrak{s}} + T\mathsf{V}AP\_{\mathfrak{s}} \times \mathfrak{e}^{\mathfrak{s}}$$

	- (I) Estimate the oracle window size *WS*.
	- (II) Decide on number of blocks *m* to manipulate the price.
	- (III) Depending on aggregation method, calculate *pm* using Equation (12) or Equation (14).

$$A\mathbf{C} = \Delta \mathbf{x} - \Delta y$$

