Constant product
Last updated
Last updated
When people buy assets on the DEX, it means they are are giving something and are receiving something in return. This articles covers what they are receiving and how much of it.
The following steps are done to calculate how much a trading user receives of that currency while providing funds of another currency. In this case, A could be kUSD and B could be XKP. The trading user provides kUSD into the liquidity pool and the Constant Product mechanism is used to determine how much XKP the user will receive.
The formula reads as follows: A and B represent the supply of two currencies, k represents the so-called Constant Product. In this example, it's the result of the multiplication of the two amounts. When trading A for B, a user provides new funds, indicated by x, to A and wants to receive funds from B. When doing that, the Constant Product k needs to stay constant. Therefore, when increasing the supply of A, it has to be calculated by how much the supply of B has to be lowered; that new size of B represented by B'. The difference between the original size of B and the lowered supply of B, represented by y, is what is given to the trading user.
The following image further visualizes the effects of trading using the Constant Product formula.
The curve represents the Constant Product. When the amount of A changes, B has to change as well. Point 1 indicates the amount of A and B before the trade, point 2 represents the amounts after the trade. When looking at the graph, we notice one critical aspect: The more funds of currency A are provided, the less funds of currency B ar received. This resembles the mechanism of supply and demand. Buying currency B indicates a high demand for that currency, meaning more of A must be provided to receive the same amount of B. It is important to note that it is not possible to buy all of B because one would need to provide an infinite amount of A.
One of Kopi's unique feature is virtual liquidity. When trading a currency pair with very low liquidity, even small trade amounts can have large impact on the prices. Compare these two examples:
// Example 1
Liquidity kUSD: 10
Liquidity USDC: 10
Amount given into the pool: 10 kUSD
Amount received in return: 5 USDC
Price paid: 2 kUSD per USDC
// Example 2
Liquidity kUSD: 1000
Liquidity USDC: 1000
Amount given into the pool: 10 kUSD
Amount received in return: 9.9 USDC
Price paid: 1.01 kUSD per USDC
The two examples show that trading in a pool with more liquidity is more attractive due to the smaller spread. At the same time, adding liquidity to a pool with already low liquidity is not attractive: If there are no trades, provided liquidity won't be used and thus won't generate any trading fees.
To tackle that issue, each currency listed on the DEX has a parameter defining a minimum amount of liquidity that shall be used when trading. If the available liquidity is below the set threshold, the lacking amount will be filled with so-called virtual liquidity.
// Example 3
Liquidity kUSD: 10
Virtual Liquidity kUSD: 990
Liquidity USDC: 1000
Amount given into the pool: 10 kUSD
Amount received in return: 9.9 USDC
Price paid: 1.01 kUSD per USDC
In example 3, traders trading from kUSD to USDC won't notice a different compared to example 2. However, there is a limitation when trading from USDC to kUSD: Since only 10 kUSD are present as liquidity, trades that result in buying more than those 10 kUSD will be declined.
When looking it from the perspective of the constant product, the slope of the curve does not change regardless of whether the liquidity is actually present or filled virtually. What changes though is the location of the curve: If there is virtual liquidity, the curves moves so that it crosses the Y-axis. In it's original form, the constant products works such that it never touches the Y-axis, indicating that infinitely many funds have to be given into the funds to buy all what's available. When virtual liquidity is used, it is possible to buy all available liquiditiy.
The constant product is used when calculating how much users receive when giving something into the pool or how much they have to give when wanting to buy a given amount, respectively. The constant product works under the assumption that the liquidity amounts are of the same value. The amounts of available liquidity don't necessarily go together though: kUSD and USDC could be traded at 1:1, but there could be 1,000 kUSD and 100,000 USDC available.
For any trade, the protocol converts the value of the available liquidity to XKP and then compares the two values. If 1 XKP = 0.25 kUSD/USDC, then the provided kUSD could be worth 4,000 XKP while the provided USDC could be worth 400,000 XKP. If the two values differ (which basically is the case always), the protocol uses the smaller value as reference: When using the constant product, the protocol will use the equivilant of 4,000 XKP of both currencies, in this case 1000 kUSD and 1000 USDC. The effect is that trading between currencies for which one currency only has little liquidity, the spread will be high. If both currencies have large amounts of liquidity, the spread will be small.