Chapter 6: Exchange Rate Providers

Welcome back! In Chapter 5: SelfPeggingAsset Factory, we learned how new SelfPeggingAsset (SPA) Pools and their associated tokens are created using a standardized “assembly line”. When setting up these pools, especially for tokens that might not always be worth exactly the same (like different stablecoins, or tokens representing staked assets), the factory needs to tell the pool how to find out the current real-world value of those tokens.

What Problem Do Exchange Rate Providers Solve?

Imagine our SelfPeggingAsset (SPA) Pool is like an exchange booth trying to swap US Dollars (USD) for Euros (EUR). To offer a fair exchange rate and calculate its service fee correctly, the booth needs to know the current market rate – is 1 EUR worth $1.05 today, or $1.10? It can’t just guess or use yesterday’s rate.

Similarly, our SPA Pool holds different digital tokens. Let’s say it holds USDC and USDT. While both aim to be worth $1, tiny differences in their market prices can exist. Or imagine a pool holding stETH (staked Ether) and WETH (Wrapped Ether). Their values are closely related but not always exactly 1:1.

The SPA Pool needs a way to look up the current approximate real-world exchange rate for the tokens it holds. This information is crucial for:

  1. Calculating Fees: Some fees might be higher if the pool becomes unbalanced, and knowing the relative value of tokens helps determine the balance.
  2. Maintaining Peg Stability: For pools designed to keep assets pegged (like stablecoins), knowing the external market rate helps the internal math work correctly.
  3. AMM Calculations: The core Automated Market Maker formula inside the pool uses these rates to adjust its internal calculations, ensuring swaps are priced fairly according to current market conditions.

Where does the pool get this vital price information? From Exchange Rate Providers!

What are Exchange Rate Providers?

Exchange Rate Providers are specialized smart contracts that act like trusted price tickers for the blockchain. Think of them like subscribing to a financial news feed (like Bloomberg or Reuters) that constantly reports the latest stock prices or currency exchange rates.

Each Exchange Rate Provider contract is designed to fetch or calculate the price for a specific type of token:

The SelfPeggingAsset Factory selects and configures the correct Exchange Rate Provider for each token when it creates a new pool.

How Are They Used?

The SelfPeggingAsset (SPA) Pool doesn’t store the exchange rates itself; it queries the providers when necessary.

Example Scenario: During a Swap

  1. You want to swap 100 USDC for USDT in a SPA Pool.