> For the complete documentation index, see [llms.txt](https://docs.valantis.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.valantis.xyz/amms-and-vaults/staked-amm/stepwise-fee-module.md). Agents can also search and read this documentation through its [MCP server](https://docs.valantis.xyz/~gitbook/mcp).

# Stepwise Fee Module

As mentioned in the [Stake Exchange Whitepaper](https://github.com/ValantisLabs/stex-amm-whitepaper/blob/main/STEX_AMM_WHITEPAPER.pdf), there are several valid fee mechanisms which may yield high returns for LPs in a specialized AMM like STEX. Since [STEXAMM.sol](https://docs.valantis.xyz/amms-and-vaults/staked-amm/smart-contracts/stexamm.sol.md) mediates that an LST or Native Token are never sold below their true value, and Valantis Core (Sovereign Pool) ensures a Swap Fee Module has a specific and siloed functionality, the design space for Swap Fee Modules in STEX is ample and safe to explore.

The Stepwise Swap Fee Module is the pinnacle of such experimentation. It takes a data-rich philosophy, as there are numerous external factors that influence the fees charged when buying LSTs from users and returning them Native Token, too much information to effectively consider onchain.

In short, the Stepwise Swap Fee Module is an elegant solution that makes fees reactive to both onchain and offchain data. It defines a set of "steps" that correspond to the fees applied to various states of post-swap reserves. It leverages several data sources to inform the pool how it should react to different swap sizes, LP deposits, and LP withdrawal conditions. This allows the pool to consider much more data relevant to targeting high returns and competitive prices while market-making the LST. This data includes, but is not limited to:

* The broader market rates for the LST across several exchanges.
* The health of large lending-borrow positions that could lead to heavy liquidations, thereby affecting AMM volume.
* Volatility of swap volume across the AMM and other trading venues
* The general health of the LST protocol and time to withdrawal estimates for LST reserves.
* The current reserve proportions of the pool compared to the target reserves.

This ensures the Stepwise Fee Module is always able to provide the most competitive rates across all exchanges for the asset to receive the most volume, target the highest returns for LPs, and ensure it is not prone to manipulation attacks or illiquidity of its own reserves.

The Swap Fee Module applies a fee based on swap direction:

* `Native Token -> LST` swaps always receive a zero fee. This ensures rebalancing back to the native token happens often. If the AMM does not need Native Token liquidity to fulfill volume, it can always "stake" to convert these reserves back to LST at no cost.
* `LST -> Native Token` swaps receive a fee determined by the Native Token reserves after the swap. This fee is read from a fee array based on the input size of the token. This array is updated periodically from offchain according to an analysis of multiple data sources.

<figure><img src="https://986147118-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F1hNJeG4CApeBcE2NlR61%2Fuploads%2Fgit-blob-8bbf0e7d70a278e69611b1834ef8adf12f6202b2%2FSTEPWISE.png?alt=media" alt=""><figcaption></figcaption></figure>

Steps are of a fixed swap size and never decreasing between steps. Though, fees can remain constant for multiple steps. Fee Curves are passed periodically from offchain as a reaction to multiple external data sources. In the absence of offchain updates; the pool's fees remain reactive to changes in its internal liquidity conditions from swaps, deposits, and withdrawals of liquidity.

For example:

* Swap size `<2,000`: `1 bps`
* Swap size `2,000-4,000`: `2 bps`
* Swap size `4,000-12,000`: `4 bps`
* Swap size `12,000-50,000`: `10 bps`
* Swap size `50,000-60,000`: `15 bps`
* Swap size `60,000-120,000`: `17 bps`
* Swap size `120,000-140,000`: `20 bps`
* Swap size `140,000-142,000`: `21 bps`
* Swap size `142,000-144,000`: `22 bps`
* Swap size `144,000-146,000`: `23 bps`
* \[.....]
* Swap size `>300,000`: `100 bps`

Offchain, the logic may look something like this:

Under `100,000` LST, withdrawals are processed quickly, so we charge competitive rates based on the global prices across other exchanges. Between `100,000-200,000` LST we raise rates slightly to accommodate longer withdrawal wait times since the queue is more congested at this size. After `200,000` LST, we begin a steady increase in fees to incentivize new Native Token deposits before hitting a maximum fee at `>300,000` LST swap size.

NOTE: We use the notion of swap size in the example above, but we actually mean is reserves after a swap. i.e. A swap of `100,000` LST, followed by a swap of `1` LST. The swap of `1` LST will pay the same or a higher fee than the previous swap of `100,000`. The fees are determined by the Native Token reserves after the swap, not just the swap size. Similarly, the removal of liquidity from LPs will also increase fees, as they lower post-swap reserves.

Notably, Fees can be updated from offchain without swaps ocurring. For example, if a large withdrawal for `1M` LST enters the LST Protocol Withdrawal Queue, the AMM may want to adjust its fee steps to account for the fact that it will now need to wait longer in the Redemption Queue to process future LST withdrawals. This is assuming the LST Protocol Process Withdrawals FIFO, settled against new stake with priority. Since a large number of pending withdrawals will affect the AMM's withdrawal time, it will want to adjust its fees accordingly for large swap sizes.

Generally, the Stepwise Fee Module is a way for fees to be reactive to both onchain and offchain data sources, without having critical dependencies on offchain infrastructure.

To learn more about the smart contract mechanics and formula behind the Stepwise Swap Fee Module, check out [StepwiseSwapFeeModule.sol](https://docs.valantis.xyz/amms-and-vaults/staked-amm/smart-contracts/stepwisefeemodule.sol.md).
