> For the complete documentation index, see [llms.txt](https://docs.botanixlabs.com/bitcoin-yield/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.botanixlabs.com/bitcoin-yield/technical-details/exchange-rate-calculations.md).

# Exchange rate calculations

The relationship between stBTC and BTC is governed by a dynamic exchange rate that reflects accumulated yield. Understanding this mechanism is crucial for users of Bitcoin Yield.

## The stBTC/BTC Exchange Rate

The exchange rate between stBTC and BTC is determined by:

```
Exchange Rate = Total BTC in Protocol / Total stBTC Supply
```

This simple but powerful formula ensures that as more BTC enters the protocol (through transaction fees), each stBTC becomes worth more BTC.

## Example Calculation

Let's walk through a simplified example:

1. **Initial State**:
   * Total BTC in protocol: 100 BTC
   * Total stBTC supply: 100 stBTC
   * Exchange rate: 100/100 = 1 BTC per stBTC
2. **After Fee Collection**:
   * Transaction fees add 5 BTC to the protocol
   * Total BTC in protocol: 105 BTC
   * Total stBTC supply: still 100 stBTC (no new minting)
   * New exchange rate: 105/100 = 1.05 BTC per stBTC
3. **For an Individual User**:
   * If you hold 10 stBTC, initially worth 10 BTC
   * After fee collection, your 10 stBTC is now worth 10 × 1.05 = 10.5 BTC
   * Effective yield: 5% increase

## Technical Implementation

The exchange rate is implemented through the EIP-4626 tokenized vault standard:

* **Share Accounting**: stBTC tokens represent shares of the total pool of BTC
* **Asset-to-Share Conversion**: When depositing, the protocol calculates how many stBTC shares your BTC assets are worth
* **Share-to-Asset Conversion**: When withdrawing, the protocol calculates how many BTC assets your stBTC shares are worth

## Exchange Rate Updates

* The exchange rate is updated with every interaction with the protocol:
  * Deposits
  * Withdrawals
  * Fee distribution events
* The smart contract automatically recalculates the exchange rate based on:
  * Current BTC balance in the protocol
  * Current stBTC token supply

## Handling Intermediate Token (pegged BTC)

The protocol uses pegged BTC (pBTC) as an intermediate token:

1. When depositing: BTC → pBTC (1:1) → stBTC (at current exchange rate)
2. When withdrawing: stBTC → pBTC (at current exchange rate) → BTC (1:1)

This two-token system enables the protocol to maintain a clean separation between the pegged asset (pBTC) and the yield-bearing token (stBTC).
