# Constant Product Pools
Constant product pools have swaps dictated by the following constraint equation:
where each term is defined as follows.
Variable | Description |
---|---|
Swap invariant, or liquidity depth. This quantity is strictly positive. | |
and | Reserves in the and assets. In this case, and can be any crypto assets. |
and | Changes (deltas) from the perspective of the AMM in the and reserves when a swap is performed. |
If the asset reserse are not modified by liquidity providers, then remains constant and the price changes solely based on trades. This ensures that the price of the asset bought increases while the price of the asset sold decreases. The arbitrage opportunities guarantee that the prices offered by the pools move in conjunction with the rest of the market.
# Providing Liquidity (Joining a Pool)
Pools on Nibi-Swap fascilitate trades between pairs of fungible coins. Anyone can become a liquidity provider (LP) by adding an equivalent value of tokens to the pool. Here, equivalent value means equivalent ratio of the reserves.
For example, let's say there's a pool with two assets of equal weight. Its reserves are and and its total number of shares are .
In order to add 20 units of tokenA
to the pool, the same proportion of tokenB
needs to be added to the reserves as well. Adding 20 to would grow the reserves by 10%.
Adding liquidity gives an account LP shares, also called LP tokens or pool shares. The total number of shares must grow by the same proportion as the reserves. We'll call this the provided liquidity percentage .
In this example, the liquidity provider receives 100 LP shares, or pool shares, which can be reclaimed for the underlying funds at any point. Pool shares are fungible tokens that quantitatively express how much of a pool's reserves an LP has claim to. They serve a similar purpose to ERC-20 shares of an ERC-4626 tokenized vault (opens new window).
The on-chain message for providing liquidity is called MsgJoinPool
, so we sometimes refer to LPing as "joining the pool".
LPing increases the liquidity depth, or swap invariant, because it increases the reserves.
# Swap Fees
Whenever a trade occurs, a small percentage fee is charged to the sender of the transaction. Because each liquidity provider owns shares of the pool, the swap fee is given pro-rata to all LPs due to the claimable value of their shares. This is, however, an unrealized gain as the LP shares would still need be burned in exchange for coins to end up in a liquidity provider's account.
A Cosmos coin, or sdk.Coin
, defines a token with a denomination and an amount. IBC vouchers, native staking assets, and LP shares are all Cosmos coins.
# Weighted Pools
Pools on Nibi-Swap can include more than two assets with differing token weights. From the quantity and normalized weight of each asset, we compute a swap invariant, .
Variable | Description |
---|---|
normalized weight of token | |
quantity of token in the pool | |
the swap invariant |
For a given pool, the sum of the normalized weights must equal 1. The swap invariant does not change when users swap assets. It only changes when a liquidity provider (LP) adds or removes liquidity. Each pool contains t
tokens. Thus, the number of trading pairs is equal to the number of combinations of size 2:
And between each of these pairs exists a spot price.
# Weighted Pool Spot Price
Spot price is the instantaneous price at which a swap occurs. It is computed as the ratio of the token balances normalized by token weight:
Variable | Description |
---|---|
amount of token in | |
weight of token in | |
amount of token out | |
weight of token out |