# Technical: Implementation of the Rebase Operation

**URL:** <https://forum.ampleforth.org/t/technical-implementation-of-the-rebase-operation/578>\
**Category:** Learning Resources\
**Created:** [August 20, 2022, 10:35pm UTC](https://forum.ampleforth.org/t/technical-implementation-of-the-rebase-operation/578 "2022-08-20T22:35:41Z")\
**Posts on this page:** 1\
**Showing post:** 1

<div class="post-metadata">

**Author:** ![pascal-merkleplant](https://yyz2.discourse-cdn.com/flex030/user_avatar/forum.ampleforth.org/pascal-merkleplant/32/104_2.png) [@pascal-merkleplant](https://forum.ampleforth.org/u/pascal-merkleplant)\
**Post date:** [August 20, 2022, 10:35pm UTC](https://forum.ampleforth.org/t/technical-implementation-of-the-rebase-operation/578/1 "2022-08-20T22:35:41Z")

</div>

This post aims to explain how Ampleforth’s rebase operation is implemented. While the whitepaper provides an overview of the different components involved, it misses some implementation details.

I hope the post helps community members to gain a better understanding of the underlying details. Please comment if things are unclear, missing, or faulty.

# The Rebase Operation

The rebase operation is an atomically executed adjustment to AMPL’s supply and user balances according to a discretional rule set.

## Overview

 ![rebase](https://canada1.discourse-cdn.com/flex030/uploads/governance1/original/1X/5f3a5940102b7b53ef2641c1be55a40c3143335c.png)

### 1. A User initiates the Rebase Operation

To initiate the rebase a user needs to call the `rebase` function in the [`Orchestrator`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/Orchestrator.sol#L33) contract. The intention behind the `Orchestrator` is to coordinate AMPL’s supply change with external contracts. This becomes important later in step 7.

It is also worth mentioning that the `Orchestrator`’s `rebase` function [fails](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/Orchestrator.sol#L41) if its called by a contract, meaning only EOAs (_Externally Owned Contracts_) can initiate a rebase. This makes it harder to atomically sandwich-trade AMPL’s supply adjustment.

### 2. The `Orchestrator` calls the `MonetaryPolicy`’s `rebase` function

Ampleforth’s monetary policy is rules-based, i.e. supply adjustments are done algorithmically. These rules are implemented inside the [`MonetaryPolicy`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragmentsPolicy.sol#L20) contract.

The [first rule enforced](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragmentsPolicy.sol#L105) is that the `rebase` is only allowed to happen once during a reoccurring time window. Currently, the time window is set to 20 minutes and opens every 24 hours.

### 3. The `MonetaryPolicy` fetches the Target and Market Rate from the Oracles

In order to compute AMPL’s supply delta, the policy needs to know AMPL’s current target and market rate. These values are fetched from two different instances of the same [`MedianOracle`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/MedianOracle.sol#L13) contract.

A `MedianOracle` is a contract in which a set of authorized [`providers`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/MedianOracle.sol#L26) are allowed to push [`reports`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/MedianOracle.sol#L21). Each report becomes only valid after some time passed (the [`reportDelaySec`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/MedianOracle.sol#L41)) and invalid again after some more time (the [`reportExpirationTimeSec`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/MedianOracle.sol#L38)).

Upon request, the `MedianOracle` returns the median of the averages of valid reports for each provider.

Currently, the value pushed to the `MedianOracle` the `MonetaryPolicy` queries as the target rate is the 2019 CPI value. For the market rate `MedianOracle`, AMPL’s VWAP (_Volume Weighted Average Price_) of the last 24 hours is pushed by the providers.

For more info about the oracle’s providers, see for example [this discussion](https://forum.ampleforth.org/t/proposal-to-add-tellor-as-an-oracle-part-deux/198).

### 4. The `MonetaryPolicy` computes AMPL’s `supplyDelta`

Having AMPL’s target and market rate, the `MonetaryPolicy` computes how much AMPL’s supply has to change. This `supplyDelta` can be either positive (expansion) or negative (contraction).

For more info about the mathematical details, see [the AIP-5 discussion](https://forum.ampleforth.org/t/rebase-curve-upgrade-proposal/327).

### 5. The `MonetaryPolicy` calls `AMPL`’s `rebase` function

The [`AMPL ERC20`](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragments.sol#L10)’s `rebase` function receives the computed `supplyDelta` as an argument. The function must be only callable by the `MonetaryPolicy` contract.

### 6. AMPL’s `rebase scalar` is updated

To update all user balances at once, the AMPL token uses an _internal_, _fixed_ (i.e. non-rebasing) [user balance](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragments.sol#L73) and “[simulates](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragments.sol#L169)” an _external_ user balance (the AMPL balance in your wallet) by multiplying the fixed balance with some number called `rebase scalar`.

Changing AMPL’s supply, that is the sum of all _external_ user balances ([isn’t it?](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/UFragments.sol#L34) 😜), only involves updating the `rebase scalar` with which the _external_ balances are calculated.

This implementation ensures the supply adjustment is always independent of the number of user balances!

Furthermore, it is guaranteed that a supply adjustment is always non-dilutive. (This is an interesting property, meaning even the `MonetaryPolicy` can not introduce any Cantillon effect)

### 7. The `Orchestrator` calls external Contracts to help Coordinate the Supply Adjustment

Now comes the job of the `Orchestrator` contract.

The `Orchestrator` manages a set of [transactions](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/Orchestrator.sol#L20) that get executed [directly](https://github.com/ampleforth/ampleforth-contracts/blob/master/contracts/Orchestrator.sol#L48) after AMPL’s supply adjustment.

Two of the transactions currently included are:

- Adjusting Balancer’s AMPL-USDC smart pool ratio. For more info see [this post](https://blog.ampleforth.org/ampl-usdc-smart-pool-on-balancer-be8eed9a264a).
- Call UniswapV2’s `sync` function for AMPL pools to force the Uniswap pools to update its cached balance of AMPL tokens inside the pool. Without calling this function, Uniswap’s `x * y = k` property would operate on an outdated AMPL balance.

If any of the function calls fail, the whole rebase operation is reverted. Furthermore, every transaction added to the `Orchestrator` increases the gas costs to execute a rebase operation. It should also be noted that the transactions must not depend on each other because the order in which they are executed is random.

One policy of the ForthDAO should therefore be to not blow the `Orchestrator` with transactions but rather integrate external contracts by other methods, e.g. wrapped AMPL.

---

_[View the full topic](https://forum.ampleforth.org/t/technical-implementation-of-the-rebase-operation/578)._
