Buffl

Produkte

PN
by Patrick N.

What is the CPF AMM?

The Constant Product Formula (CPF)


  • CPF is the most widely used AMM algorithm, pioneered by Bancor and Uniswap: k = x * y

    Where x and y are the reserves of each token in the LP; k, their product, must always remain constant


  • Assuming no transaction fees, all trades change the balance of tokens reserves so that their product (k) remains unchanged. In practice, each trade increases the k slightly – proportionally to the transaction fee and the amount traded.


Pros:

  • CPF ensures sufficient liquidity for a trade, as when the quantity of an asset approaches zero, its price approaches infinity.

Cons:

  • CPF can result in high slippage for traders and impermanent loss (IL) for liquidity providers


Example:

Suppose an ETH/DAI LP, with total reserves of 10 ETH and 15,000 DAI (assuming 1 ETH = 1,500 DAI – remember that the total token values must be balanced).

  • Now imagine that you wanted to swap DAI to receive 2 ETH. How much DAI would you have to pay?

  • The initial k of the LP is 150,000 (k=x*y=10*15,000)

  • This value needs to remain constant after your trade

  • The new amount of ETH in the pool would be 8 (you have taken away 2 ETH) and the new amount of DAI would be 15,000+z (where z the amount of DAI you paid to receive 2 ETH).

  • So, 8x15,000+z must be equal to 150,000 or z=3,750.

  • In other words, you would have to pay a total 3,750 DAI for 2 ETH, or 1,875 DAI per ETH.

Your trade in this (very illiquid) pool would result in a big difference in the new price of ETH (1 ETH=1,875 DAI) compared to other markets (where ETH would continue to trade at 1,500 DAI). This would attract arbitrageurs who would lower the price again by selling ETH to the pool until there was no more difference.

What is IL and why is it imporant?

Liquidity providers face impermanent loss (IL) when supplying LPs


  • Impermanent Loss (IL) is the loss caused to a liquidity provider due to the price volatility of the tokens they provide to an LP

    • IL is essentially the difference between providing tokens to an LP vs holding them

    • The more volatile the tokens, the higher the exposure to impermanent loss

  • Why ‘impermanent’?

    • While the liquidity provider keeps their tokens in the LP, they face no loss. The loss is realized when tokens are withdrawn. The amount of loss depends on the price difference between deposit and withdrawal

    • If the exchange rate between the tokens is the same, there is no loss

    • If the LP chooses to withdraw their funds when the exchange rate is different, the impermanent loss is realized and becomes permanent

  • Impermanent loss can be mitigated by supplying stablecoins, opting for one-sided liquidity pools, and by participating in uneven (weighted) pools.


Example:

Consider the ETH/DAI LP with an initial balance of 10 ETH and 15,000 DAI by a single liquidity provider. The total value at stake is $30,000 at the time of deposit.


Now, suppose that the price of ETH increases to $1,600. This would incentivize arbitrageurs to buy the “cheaper ” ETH in the pool until it there is no price discrepancy.

  • They would be able to buy ~ 0.32 ETH in exchange for ~496 DAI before the price of ETH in the pool reaches $1,600 and is at equilibrium with the market (you can calculate this through the constant product formula).

  • The LP would still realize profits, but not as much as if they simply held the tokens.



Risks of DEXes?

Hacks and Exploits:

  • DeFi protocols are as secure as the code that supports them. That is typically code on the application/protocol layer of the DeFi stack, but risks may also exist in the settlement/asset layers.

  • Settlement level exploits refer to security flaws of the blockchain where the DeFi application is deployed. While this kind of exploits is rarer, it the most critical (as it affects all DeFi protocols on a particular blockchain).

  • Protocol and application exploits refer to security flaws of the smart contract or web interface of the DeFi app. Such exploits can be mitigated to an extent through security audits.


Admin Key Risk:

  • It refers to the risk that the DeFi protocol owner(s) will modify the protocol in a way that will benefit them, at the expense of the users.

  • It can be mitigated through multi-signature deployments and by employing a DAO governing protocol updates

Price Slippage:

  • Slippage refers to the difference between the expected and actual price of a trade. It is highly related to the volume of a swap against the depth of liquidity in a DEX pool.

  • There is a positive relationship between the size of the order and slippage: the larger the order, the larger the price slippage that a trader will have to face.

  • There is a negative relationship between the size of the pool and slippage: the larger the pool the lower the price slippage: It can be mitigated by setting slippage limits (at the risk of the trade not being executed).

Front-running:

  • DEX transactions are visible to everyone before they settle on the blockchain. Bots are continuously monitoring unconfirmed transactions for profitable opportunities.

  • When they identify one, they submit the same transaction, but with higher fees. As miners want to maximize their profitability, they are incentivized to confirm the bot’s transaction first.

  • Then, the bot will either offload the newly acquired tokens to the user (trade executed at less favorable price) or simply cause the user ’s transaction to fail.

Vampire Attack:

  • It involves draining liquidity from a DEX by incentivising liquidity providers to move their liquidity to a competitive DEX

Rug Pulls:

  • Refers to the act of maliciously removing liquidity from a pool to leave users holding tokens with no utility or value.

  • Can be more effectively executed with thinly traded tokens.

  • It is also common for malicious actors to create clone tokens to trick users into thinking they are trading legitimate ones. This can be mitigated by confirming the address of the token being traded.

Oracle Manipulation:

  • DeFi protocols rely on oracles to receive information from the real-world. In the case of DEXs this information usually relates to token prices. There is the risk that either the oracle goes bad, or its data is manipulated.

  • This can be mitigated by applications relying on reliable oracles or a plethora of oracle solutions.


Author

Patrick N.

Information

Last changed