# Oracle usage

marginfi primarily relies on Pyth oracle price feeds for token market prices, leveraging Pyth’s extensive safety features for a more secure understanding of token price activity and risk management. However, for assets that Pyth does not support, the protocol employs Switchboard oracles with a similar set of best practices.

**marginfi protects for price accuracy with a few key components:**

**Price staleness checks:**Price staleness is a situation when an oracle provides price data that is out of date. To mitigate this risk, the marginfi protocol applies stricter staleness checks than both Pyth and Switchboard, defining its own maximum valid price staleness of**60 seconds**.**Confidence intervals:**Pyth and Switchboard publish confidence intervals with each price feed, representing possibly different prices for each asset by giving users a probability distribution over price. marginfi takes a conservative approach when calculating token prices for both assets and liabilities. For both assets and liabilities, the**95% confidence interval range**is used. For assets, the lower bound of the confidence interval range is used. For liabilities, the higher bound of the confidence interval range is used.**Moving average application:**To mitigate the effect of high price volatility, marginfi uses the exponential moving average price that Pyth provides. The EMA is a type of moving average that gives more weight to recent prices. In Pyth’s case, it uses a slot-weighted, inverse confidence-weighted EMA. It considers a window of 5921 slots (approximately 1 hour on Solana mainnet) and gives more weight to recent slots. Additionally, it is inverse confidence-weighted, meaning samples with tighter confidence intervals are given more weight. The formula can be expressed as:

$\text{EMA}_{price} = \frac{\sum_{i=1}^{5921} (price_{i} \times slot_{i} \times \frac{1}{confidence_{i}})}{\sum_{i=1}^{5921} (slot_{i} \times \frac{1}{confidence_{i}})}$

Where $price_{i}$, $slot_{i}$, and $confidence_{i}$ represent the price, slot number, and confidence interval at each $i$th slot within the considered window.

Switchboard does not natively provide EMA pricing. Live prices are used for assets powered by switchboard oracles. As mentioned above, the same confidence interval approach is applied to Switcboard assets.

The combination of these features enables the marginfi protocol to estimate token prices conservatively, protect against volatility and price manipulation, and provide a safer and smoother lending experience for users.

Last updated