---
description: How is Stability Maintained? How Can Users Mint and Redeem USC?
---

# Dual Stability Mechanism: Minting and Redeeming USC

## Introduction to the Dual Stability Mechanism

The dual stability mechanism is designed to maintain the price of USC stable at 1 USD. To understand the mechanism behind this contract, users must be familiar with some essential calculations on arbitrage data.&#x20;

<figure><img src="https://3220527323-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIjw533sxGca2ZktrOjAI%2Fuploads%2FKQyteegHKQBRjcBsXPJ1%2FFrame%20129.png?alt=media&#x26;token=ffd02c5c-3767-4ebe-8e7d-22f7a8310adf" alt=""><figcaption></figcaption></figure>

## Arbitrage Data&#x20;

Arbitrage data includes all the necessary information needed in the arbitrage functions to identify the current arbitrage opportunity and execute transactions accordingly. Five data variables exist: `isPriceAboveTarget, isExcessOfReserves, reserveDiff, delta, and discount.`&#x20;

{% tabs %}
{% tab title="Boolean" %}
**`PriceAboveTarget`** identifies if the price of USC is above or below 1 USD:&#x20;

```
isPriceAboveTarget = (uscPrice >= USC_TARGET_PRICE)
```

**`isExcessOfReserves`** verifies the solvency state of the protocol by checking if the LSTs and ETH reserves to back USC are in excess or in deficit&#x20;

```
if (reserveValue > uscTotalSupplyValue) {
isExcessOfReserves = true;
}
else {isExcessOfReserves = false;
}
```
{% endtab %}

{% tab title="uint256" %}
**`reserveDiff`** is a measure of the excess or deficit gap between reserves and circulating USC supply. `reserveDiff` defines the discount when the price of USC is at 1 USD and the reserves are in deficit or excess.

```
if (isExcessOfReserves) { 
reserveDiff = (reserveValue - uscTotalSupplyValue) / BASE_PRICE; 
} else 
reserveDiff = (uscTotalSupplyValue - reserveValue) / BASE_PRICE; 
}
```

**delta** is a number that indicates the amount of ETH or USC that must be added to the USC/ETH Uniswap pool to keep it balanced (in USD terms) and re-peg the price at 1 USD&#x20;

```
function _calculateDelta(uint256 reserveIn, uint256 priceIn,uint256 reserveOut, uint256 priceOut) public pure returns (uint256)
```

Depending on the price of USC with respect to the target price, the **function** `_calculateDelta` is called with different inputs. In all cases, it returns the root solution to a quadratic equation (i.e., `delta`).&#x20;

When the price of USC is above peg ( `_arbitrageAbovePegExcessOfReserves, _arbitrageAbovePegDeficitOfReserves`), the arbitrage function calls `_calculateDeltaUSC`, which returns the delta in USC that must be added in the pool to return its price at 1 USD.  &#x20;

```
 function _calculateDeltaUSC(uint256 ethPrice) public view returns (uint256)
(uint256 reserveUSC, uint256 reserveWETH) = UniswapV2Library.getReserves(
      address(poolFactory),
      address(USC),
      address(WETH)
    );
    return _calculateDelta(reserveUSC, USC_TARGET_PRICE, reserveWETH, ethPrice);
  }
```

In this case, the quadratic equation is of the following form:&#x20;

<figure><img src="https://3220527323-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIjw533sxGca2ZktrOjAI%2Fuploads%2FhvUKyWxQ8d2kuf8Ina1r%2FScreenshot%202023-10-29%20at%2016.15.07.png?alt=media&#x26;token=625cbd8e-c296-49dc-b3b9-ffbc756a09e8" alt="" width="375"><figcaption></figcaption></figure>

Note: $$x = ETH \space reserves$$, $$y = USC  \space reserves$$ , $$\Delta y = amount \space In \space USC = delta$$ , $$0.997 = 1-pool \space fee$$

Similarly, when the price of USC is below peg ( `_arbitrageBelowPegExcessOfReserves`, `_arbitrageBelowPegDeficitOfReserves`), the arbitrage function calls `_calculateDeltaETH`, which returns the delta in ETH that must be added to increase the price at 1 USD.&#x20;

```
function _calculateDeltaETH(uint256 ethPrice) public view returns (uint256) {
    (uint256 reserveUSC, uint256 reserveWETH) = UniswapV2Library.getReserves(
      address(poolFactory),
      address(USC),
      address(WETH)
    );
    return _calculateDelta(reserveWETH, ethPrice, reserveUSC, USC_TARGET_PRICE);
  }
```

In this scenario, the quadratic equation is as follows: \
\


<figure><img src="https://3220527323-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FIjw533sxGca2ZktrOjAI%2Fuploads%2FRbadP11dUnClc5Lg6ouI%2FScreenshot%202023-10-29%20at%2016.29.46.png?alt=media&#x26;token=27da0f42-7e37-4d6e-bf3d-15e1d6665d78" alt="" width="375"><figcaption></figcaption></figure>

Note: $$x = ETH \space reserves$$, $$y = USC  \space reserves$$ , $$\Delta x = amount \space In \space ETH = delta$$, $$0.997 = 1-pool \space fee$$

In the background, the **`function`** `calculateDelta` performs all the necessary steps to get a real (positive) root for `delta` .&#x20;

1. `f = (1 - pool_fee) = 0.997` on 18 decimals, in square root formula `a = f`
2. parameter `b` in square root formula, `b = rIn * (1+f)` , on 18 decimals
3. parameter `c` in square root formula, `c = rIn^2 - (rIn * rOut * priceOut) / priceIn.`&#x20;

```
if rIn^2 > (rIn * rOut * priceOut) / priceIn: 
  c= rIn^2 - (rIn * rOut * priceOut) / priceIn 
  delta = sqrt(b^2 - 4ac)/2a - b

else if rIn^2 < (rIn * rOut * priceOut) / priceIn 
c= (rIn * rOut * priceOut) / priceIn - rIn^2  
  delta = sqrt(b^2 + 4ac)/2a - b 
```

**`To learn more about delta calculation:`** [**`https://hackmd.io/@6jzKo0C7QCGkNGHQ9oU28g/ryQdB_5M6`**](https://hackmd.io/@6jzKo0C7QCGkNGHQ9oU28g/ryQdB_5M6)

**discount** is a number between zero and one which measures the difference between reserves and USC supply. This used to define an additional arbitrage opportunity when the price of USC is at  approximately 1 USD.&#x20;

```
if (isExcessOfReserves) { 
discount = (reserveDiff * 1 ether) / uscTotalSupplyValue; 
} else 
discount = (reserveDiff * 1 ether) / reserveValue;
}
```
{% endtab %}
{% endtabs %}

## Arbitrage Functions

Users can call arbitrage functions to make profits and stabilise the price of USC at 1 USD. Different arbitrage opportunities can arise depending on the price of USC and the solvency state. There exist six arbitrage opportunities:&#x20;

| Arbitrage Name                        | USC Price vs Target           | USC Supply Value vs Reserves Value    |
| ------------------------------------- | ----------------------------- | ------------------------------------- |
| `_arbitrageAbovePegExcessOfReserves`  | `uscPrice ≥ USC_TARGET_PRICE` | `reservesValue > UscTotalSupplyValue` |
| `_arbitrageAbovePegDeficitOfReserves` | `uscPrice ≥ USC_TARGET_PRICE` | `reservesValue ≤ UscTotalSupplyValue` |
| `_arbitrageBelowPegExcessOfReserves`  | `uscPrice < USC_TARGET_PRICE` | `reservesValue > UscTotalSupplyValue` |
| `_arbitrageBelowPegDeficitOfReserves` | `uscPrice < USC_TARGET_PRICE` | `reservesValue ≤ UscTotalSupplyValue` |
| `arbitrageAtPegExcessOfReserves`      | `discount != 0`               | `reservesValue > UscTotalSupplyValue` |
| `_arbitrageAtPegDeficitOfReserves`    | `discount != 0`               | `reservesValue ≤ UscTotalSupplyValue` |

All arbitrage functions return the `rewardValue` in USD.&#x20;

<details>

<summary>Price above 1 USD</summary>

The arbitrage contract caps the mint amount of USC at `delta` and users can call the respective functions when its price is above 1 USD. USC is minted through `_arbitrageAbovePegExcessOfReserves and _arbitrageAbovePegDeficitOfReserves.` These functions get executed  only if the arbitrage is profitable. In other words,  if the `deltaInETH > ethAmountReceived` from the swap  of newly minted USC, the transaction gets reverted.&#x20;

#### Excess Reserves

This function executes arbitrage that occurs when the price of USC is above 1 USD, and the reserves are in excess.&#x20;

```
function _arbitrageAbovePegExcessOfReserves(uint256 reserveDiff, uint256 ethPrice) private returns (uint256)
```

Following the swap of the newly minted `delta`USC for ETH, the function verifies if the `reserveDiff` is sufficient to back the `delta`. Assuming `deltaUSD ≤ reserveDiff, the ethAmountToSwap` is set to `deltaInETH,` exchanged for CHI and the `chiAmountReceived` is burnt.

When `deltaUSD > reserveDiff,` the `ethAmountToSwap` in CHI is capped to `reserveDiff.` This is done to ensure the full backing of USC after the `delta` has been added to the circulating supply. The remaining difference (`deltaInETH` -  `ethAmountToSwap)`, known as `ethAmountForReserves`, is added to the `reserveHolder`.&#x20;

#### Deficit Reserves&#x20;

This function executes arbitrage  which occurs when the price of USC is above 1 USD,  and the reserves are in deficit.&#x20;

```
  function_arbitrageAbovePegDeficitOfReserves(uint256 reserveDiff, uint256 ethPrice) private returns (uint256)
```

Following the swap of the newly minted `delta` USC for ETH, the `deltaInETH`is added to the `reserveHolder.`

</details>

<details>

<summary>Price below 1 USD </summary>

The arbitrage contract performs buybacks on USC for ETH when the protocol has excess reserves and the price of USC is below 1 USD. Moreover USC can be burnt when there is a deficit of reserves and the price is below 1 USD or when the price of USC is below 1 USD and the reserves are equivalent to the outstanding stablecoin debt . Redeem and burn functions are called through `_arbitrageBelowPegExcessOfReserves` and `_arbitrageBelowPegDeficitOfReserves .`

#### Excess Reserves

`_arbitrageBelowPegExcessOfReserves` can be called when the price of USC is below 1 USD, and the reserves are in excess.

```
function _arbitrageBelowPegExcessOfReserves (uint256 reserveDiff, uint256 ethPrice) private returns (uint256)
```

Assuming `delta ≤ reserveDiff,` the `ethAmountToRedeem` is set to `deltaInETH` and it's removed from the `reserveHolder` to buy USC on Uniswap. Finally, the `uscAmountReceived`   from the swap in USC is sent to the  `RewardController` contract as `uscAmountToReward` and distributed to USC stakers .&#x20;

When `deltaUSD > reserveDiff,` the `ethAmountToRedeem` from the `reserveHolder`is capped to `deltaInETH.`Once is USC bought back, `reservesDiff` in USC is preserved in the system as reward and `deltaUSD - reserveDiff` is burnt. This is done to ensure the solvency of the protocol following the arbitrage.&#x20;

#### Deficit Reserves

`_arbitrageBelowPegDeficitOfReserves`can be called when the price of USC is below 1 USD, and the reserves are in deficit.&#x20;

```
 function _arbitrageBelowPegDeficitOfReserves(uint256 reserveDiff, uint256 ethPrice) private returns (uint256)
```

When `deltaUSD ≤ reserveDiff,` the function computes the `chiAmountToMint` from the   `deltaETH and sets ethAmountFromChi = deltaETH` `.`Once minted, this value in CHI is first swapped for ETH and then exchanged for USC.  The `deltaUSD` in USC is then burnt.

When `deltaUSD > reserveDiff,` the `chiAmountToMint is` limited to `ethAmountFromChi = reserveDiffInETH.`This limits the potential dilution of CHI holders to the `reserveDiff` value. The remaining difference `ethAmountToRedeem = deltaETH - ethAmountFromChi` is covered with the protocol's reserves. Finally`, ethAmountFromChi + ethAmountToRedeem` is  exchanged for USC and`deltaUSD` is burnt`.`

</details>

<details>

<summary>Price at 1 USD</summary>

The arbitrage contract can profit from situations in which the price of USC is at 1 USD and the reserves are in excess or deficit. This ensures that even if the price of USC trades at 1 USD, there will be an arbitrage opportunity to make the value of the reserves equivalent to the one of USC debt.&#x20;

#### Excess Reserves

`_arbitrageAtPegExcessOfReserves` can be called when the USC price is 1 USD, and the reserves are in excess.

```
 function _arbitrageAtPegExcessOfReserves(uint256 reserveDiff, uint256 discount, uint256 ethPrice) private returns (uint256) 
```

Since the protocol has excess reserves to back USC, the `discount` is measured by `reserveDiff / uscTotalSupplyValue.`When the function is called the contract mints the `reserveDiff` in USC.&#x20;

#### Deficit Reserves

`_arbitrageAtPegDeficitOfReserves`can be called when the price of USC is 1 USD, and the reserves to back it are in deficit.&#x20;

```
  function _arbitrageAtPegDeficitOfReserves(uint256 reserveDiff, uint256 discount, uint256 ethPrice) private returns (uint256)
```

Since the protocol has a deficit, the `discount` is measured through `reserveDiff /` reserveValue`.`When the function is called,  the `reserveDiff` in USC is burnt from the arbitrage contract.  Assuming the the `totalMintedUSC` stored in the arbitrage contract is not sufficient to cover the deficit, `ethToGetInUsd` is set to `reserveDiffInUsc - totalMintedUsc`.  Following this, the calculation of `chiArbitrageReward` as a percentage (`discount`) of the `chiToCoverEth` is performed.&#x20;

Finally, `chiToCoverEth + chiArbitrageReward` is minted: `chiToCoverEth is swapped` for ETH which is then added to the `reserveHolder` and `chiArbitrageReward` is sent to the function caller.

</details>

### Rewards to Function Caller&#x20;

The function caller is rewarded with the arbitrage profits in all arbitrage functions. The arbitrage profits arise because smart contracts always price USC at 1 USD.

Rewards for \_`arbitrageAbovePegExcessOfReserves and _arbitrageAbovePegDeficitOfReserves` are distributed in ETH`:`&#x20;

```
uint256 rewardAmount = ethAmountReceived - ethAmounToSwap - ethAmountForReserves;
_transferEther(msg.sender, rewardAmount);
```

Rewards for \_`arbitrageBelowPegExcessOfReserves and _arbitrageBelowPegDeficitOfReserves` are distributed in USC`:`&#x20;

```
uint256 rewardAmount = uscAmountReceived - uscAmountToFreeze - uscAmountToBurn;
USC.safeTransfer(msg.sender, rewardAmount);
```

```
USC.safeTransfer(msg.sender, uscAmountReceived - uscAmountToBurn);
```

Finally, since the ending currency for arbitrage `_arbitrageAtPegExcessOfReserves and _arbitrageAtPegDeficitOfReserves` is CHI, the arbitrage rewards are given with the governance token.&#x20;

```
IERC20(CHI).transfer(msg.sender, chiArbitrageReward);
```

For `_arbitrageAtPegExcessOfReserves,` `chiArbitrageReward`  is `discount` \* `chiReceived` from the ETH - CHI swap. While,  in `_arbitrageAtPegDeficitOfReserves,` `chiArbitrageReward`  is `discount` \* `chiToCoverEth.`

## Minting and Redeeming USC&#x20;

Any user can mint USC by depositing ETH, stETH or WETH and calling the `mint` function . Similarly, any user can burn USC and receive the equivalent dollar value in WETH from the protocol's reserves.&#x20;

_Note that when minting (redeeming)  USC, a protocol fee of 0.3% is deducted from the minted (redeemed) amount._

```
 function mint() external payable whenMintNotPaused nonReentrant onlyWhenMintableOrBurnable returns (uint256);
 function burn(uint256 amount) external whenBurnNotPaused nonReentrant onlyWhenMintableOrBurnable returns (uint256);
```

It is important to note that USC can only be minted and burnt when the price of USC and the collateral ratio are within a predefined range (≈ $1 price for USC,  ≈ CR 100%) .\
\
When USC is minted with ETH (WETH), the `ethAmount - fee  ( wethAmount - fee)` is sent to the `reserveHolder` and then staked into Lido via a rebalancing function.

When USC is burnt, the `ethAmountToRedeem` is set to the USC value less any fee, which is then deducted from the protocol's reserves and given to the redeemer in the form of WETH.
