# Architecture

The architecture of Tapio Protocol on Acala network is shown below. Please note that we are actively working with other Parachains, hence the architecture might be updated in the future.

The center of the architecture, i.e. Stable Asset pallet in green, is the core component of Tapio protocol on Acala. It consists of multiple markets and each market consists of assets of the same value peg.

For example, the DOT market consists of DOT-pegged assets including native DOT and DOT derivative assets. The DOT market can be divided into two parts:

- DOT Stable Swap, a DEX for DOT-pegged assets,
- tDOT, a synthetic stable asset back by DOT Stable Swap.

The following sections use DOT market as an example. Other markets such as USD market and BTC market work in similar approaches.

DOT Stable Swap is a DEX for DOT-pegged assets, including native DOT and DOT derivative assets such as staking DOT in Polkadot PoS governance. It is based on Curve's StableSwap invariant which has the following properties:

- When all token prices are close, i.e. all DOT derivative assets are pegged to DOT, it's close to a constant sum curve which means 1:1 swap;
- When one token prices shifts away, i.e. it loses peg with DOT, it's close to constant product curve similar to Uniswap. The more its price shifts away, the more slippage when swap this asset with other DOT-pegged assets.

DOT Stable Swap provides a DOT-pegged DEX for traders. It can also help stabilize DOT-pegged assets' pegging and bootstraps the liquidity for new pegging assets.

The most critical difference between Tapio Protocol and Curve is: When liquidity providers provide liquidity to the StableSwap pool, instead of getting an LP share, they receive a synthetic asset, namely stable asset whose value is backed by the underlying StableSwap. tDOT is the synthetic stable asset which is backed by the DOT Stable Swap.

The value of the synthetic asset can be derived from the StableSwap invariant which is shown below.

In the invariant shown above,

- n represents the number of underlying assets
- x represents the amounts of underlying assets
- A determines "wideness" of the curve
- D represents the intrinsic value of the curve

For a fixed A value, the parameter D can be seen as the total number of underlying tokens when their prices are equal. In such case, the amount of each asset is exactly D/n. Therefore, D is the intrinsic value of the underlying StableSwap with the following reasons:

- D represents the total amount of underlying assets when they are exactly pegged. They are expected to reach such an equilibrium over a long time since they are pegged to the same value;
- D represents the smallest amount of assets when no underlying assets are added/removed. If any asset loses its peg, the total asset amount will exceed D since more assets are needed to keep the intrinsic value of the StableSwap;
- D remains constant when underlying assets are swapped with each other.

When DOT-pegged holders provide liquidity into DOT Stable Swap, the number of tDOT they receive equals the value increase of D. When they remove liquidity from DOT Stable Swap, the number of tDOT burned equals the value decrease of D.

In other words, when all underlying assets are of equal prices, the stable asset is 100% collateralized and 1 tDOT is backed by exactly 1 DOT asset. When the price of any underlying asset shifts from peg, for example, when LDOT loses its peg with DOT, tDOT is over-collateralized and 1 tDOT is backed by more than 1 DOT asset. For fixed A value, the more LDOT shifts away from DOT, the more DOT assets backs 1 tDOT while the total supply of tDOT remains unchanged. The capability of dynamically adjusting collateral ratio is the key that tDOT maintains its peg to DOT.

The parameter A determines "wideness" of the curve, i.e. the curve is close to constant sum when A is big and to constant product when A is small. Therefore, the parameter A can be used to adjust how fast collateral ratio changes regarding to underlying asset shifts.

When LDOT's peg to DOT is volatile, a smaller A is used to ensure 1 tDOT is backed by more DOT assets. tDOT's collateral ratio increases faster when LDOT shifts away from its peg. When LDOT's peg to DOT is stable, a larger A is used to ensure 1 tDOT is backed by less DOT assets. tDOT's collateral ratio increases slower when LDOT shifts away from its peg. tDOT's swap liquidity and capital efficiency are also higher when A is larger.

To adjust the parameter A, we observe the underlying asset proportion over a period of time.

- When the underlying asset proportion is larger than 1:3 over one month, e.g. the amount of DOT is less then 25%, it's a signal that a smaller A is needed since LDOT's peg to DOT is now volatile;
- When the underlying asset proportion is smaller than 1:2 over one month, e.g. the amount of DOT is more than 33%, it means LDOT is relative stable to DOT, and it's the time to increase A in order to provide more swap liquidity and to enhance the capital efficiency of tDOT.

Tapio Protocol is integrated with Acala to leverage the DeFi primitive provided by Acala.

The Stable Swap of Tapio Protocol is integrated with Acala DEX to provide DEX functionalities for assets of the same peg. While Acala DEX supports constant product swap for any pair of assets, Tapio Protocol Stable Swap allows low-slippage swap for multiple assets of the same value peg.

Acala Stable Coin, namely aUSD, is a collateral-based stable coin built in Acala. Tapio Protocol Stable Asset offers enhanced collateral for aUSD:

- Stable asset, e.g. tDOT, provides better value pegging than any of underlying assets;
- Stable asset is synthetized from multiple assets of the same value-pegged so that aUSD does not need to support all these assets. For example, if a new DOT derivative is introduced, its value can be properly contained in tapDOT instead of creating a new vault type for these assets.

Both Tapio Protocol Stable Swap and Stable Assets are exposed to Acala EVM+ as EVM precompile. Therefore, for DeFi smart contracts such as lending and options protocols running on Acala EVM, they can use Tapio Protocol Stable Swap for value-peg swap, and use Tapio Protocol Stable asset as synthetic asset of the specific peg.

Since Tapio Stable Swap and Tapio Stable Asset are built in Acala pallet, they can be accessed by other Polkadot parachains thanks to the cross-chain support provided by Acala and Polkadot.

Last modified 9mo ago