Uniswap is a DeFi app that enables traders to swap one token for another in a trustless manner. It was one of the early automated market makers for trading (though not the first).
Automated market makers are an alternative to an order book, which the reader is assumed to already be familiar with.
How AMMs work
An automated market maker holds two tokens (token X and token Y) in the pool (a smart contract). It allows anyone to withdraw token X from the pool, but they must deposit an amount of token Y such that the ”total” of assets in the pool does not decrease, where we consider the “total” to be the product of the amounts of the two assets.
𝒳𝒴 ≤ 𝒳′𝒴′
Here 𝒳′ and 𝒴′ are the token balances of the pool after the trade and 𝒳 and 𝒴 token balances of the pool before the trade.
This guarantees that the pool’s asset holdings can only stay the same or increase.
Most pools enforce some kind of a fee. Not only should the product of the balances increase, but it should increase by at least a certain amount to account for a fee.
Assets are provided to the pool by liquidity providers, who receive so-called LP tokens to represent their share of the pool. Liquidity provider balances are tracked in a manner similar to how ERC 4626 works. The difference between an AMM and ERC 4626 is that ERC 4626 only supports one asset but an AMM has two tokens. Just like a vault, the liquidity providers’ share of the pool stays the same, but the product 𝒳𝒴 gets larger, so their slice is larger.
Advantages of AMMs
AMMs do not have a bid-ask spread
In an AMM, price discovery is automatic. It’s determined by the ratio of assets in the pool. Specifically, if we have token 𝒳 and token 𝒴, price is determined as follows:
And vice-versa for 𝒴. Specifically, the more of asset 𝒳 that is put into the pool, the more “abundant” it is, and the price of 𝒳 goes down.
There is no need to wait for a suitable “bid” or “ask” order to show up. It always exists.
If there is a mismatch between the price in an AMM and another exchange, then a trader will arbitrage the difference, bringing the prices back into balance.
We should emphasize that this is the “spot” or “marginal” price. If you buy any amount of 𝒳, the actual price you pay will be worse than the result of this calculation.
AMMs doubled as an oracle
Since the price of the assets is automatically determined, other smart contracts can use an AMM as a price oracle. However, AMM prices can be manipulated with flash loans, so safeguards need to be put in place when using AMMs in this manner. Nonetheless, it is valuable that price data is provided for free.
AMMs are highly gas efficient compared to order books
Order books requires a significant amount of bookkeeping (no pun intended). An AMM only needs to hold two tokens and transfer them according to simple rules. This makes them more efficient to implement.
Disadvantages of AMMs
There are two major drawbacks to automated market makers: 1) the price always moves and 2) impermanent loss for liquidity providers.
Even small orders move the price in AMMs
If you place an order to buy 100 shares of Apple, your order will not cause the price to move because there are thousands of shares available for sale at the price you specify. This is not the case with an automated market maker. Every trade, no matter how small, moves the price.
This has two implications. A buy or sell order will generally encounter more slippage than in an order book model, and the mechanism of swapping invites sandwich attacks.
Sandwich attacks are largely unavoidable in AMMs
Since every order is going to move the price, MEV (Maximal Extractable Value) traders will wait for a sufficiently large order to come in, then place a buy order right behind it and a sell order right after it. The leading buy order will drive up the price for the original trader, which gives them worse execution. It’s called a sandwich attack, since the victim’s trade is “sandwiched” between the attackers.
1) Attacker’s first buy (front run): drives up price for victim
2) Victim’s buy: drive up price even further
3) Attacker’s sell: sell the first buy at a profit
Using a bot like this: https://defix.shop/portfolio/frontrunning-sandwich-bot/
Add a Comment