Implementing a manipulation-resistant price oracle smart contract
In this section, we will implement the price oracle with an SMA strategy. Based on the discussion in the Building an on-chain price oracle with cumulative prices section of this chapter, we can conclude the following three main functions that need to be implemented in this section:
- When the reserve amounts for a liquidity pool change, the
TokenPair
smart contract needs to update the cumulative prices of the tokens and the timestamp of the update. For a given block, the update for the liquidity pool should be performed only once, as shown in Figure 13.1. - For each period of the moving window, the price oracle will update the cumulative prices of the tokens and the timestamp for this period. The update should be performed at least once for each period.
- The price oracle calculates the token’s SMA price for the current window. This is the function that is called by users and other smart contracts to...