Understanding Uniswap and Automated Market Makers (AMMs)
Uniswap is a leading DeFi application that allows traders to exchange tokens without any control of a centralized authority. As one of the established automated market makers (AMMs), Uniswap dynamically changed the process of buying and selling tokens by eliminating the need for conventional order books.
What Are Automated Market Makers (AMMs)?
The Automated Market Makers are quite different from traditional exchanges which used order books to record transactions. Despite this, AMMs use smart contacts to manage the trading of tokens. An AMM maintains a pool of two tokens (Token X and Token Y) allowing traders to withdraw one token of their choice. However, before doing this they have to maintain enough funds in their account, ensuring the balance of one token remains equivalent to the value of two tokens held by him. This is expressed as:
xy ≤ x’y’
Where x and y represent token balances before the trade and x’ and y’ represent balances after the trade.
To increase the balance of each token, many AMMs also include transaction fees at the time of trading. This slightly increases the value of each token.
Role of Liquidity Providers (LPs)
Liquidity providers (LPs) supply tokens to these pools and, get LP tokens in return to represent their share in the pool. These tokens work similarly to ERC 4626 tokens, the only difference is that ERC tokens support a single asset, whereas AMMs manage two tokens at a time. The value of xy increases on every transaction, and the value of xy increases which indirectly increases the value of LP’s share.
Advantages of AMMs
Price of X = Pool Holdings of Y / Pool Holdings of X
The use of this model ensures the availability of price and eliminates the need to wait for matching bids or ask orders. The presence of arbitrage traders helps in keeping AMM prices similar to prices on other exchanges.
Disadvantages of AMMs
The price of tokens always moves on:This can be understood by an example. If you buy 100 shares of Microsoft for instance, the payment for 100 shares will not affect the price of shares to increase. This is because there are thousands of buyers buying this much of shares. But, it doesn’t happen in AMMS, here every little transaction, despite its size increases the price. This will have two affects on trading.1. Buy or sell order will witness more slippage than an order book model.
2. The mechanism of swapping will attract more sandwich attacks.
Uniswap V2 Architecture
The architecture of Uniswap V2 is quite simple. The base of architecture is a UniswapV2Pair contract holding two ERC 20 tokens which traders can swap or can be used by liquidity providers for providing liquidity. The three main smart contracts of Uniswap V2 Architecture are:
- UniswapV2Factory:This architecture is used to create new pairs of tokens.
- UniswapV2Pair:It is used to manage liquidity pools for particular token pairs and is used as an ERC 20 token.
- UniswapV2Router:Using this architecture users can interact in a user-friendly manner with each other, thus allowing the facility of multi-token swapping in a single transaction.
Advanced users or smart contracts can interact directly with the Pair contract, however, all users prefer to use the Router due to its simplicity.
Core-Periphery Design Pattern
Uniswap V2 works on a core-periphery structure, which stores essential logic (e.g. Pair contracts) and is optional. The user-friendly feature is stored in the periphery (e.g. Router contracts). This difference minimizes the chances of expected errors in the main code.
Efficient Pool Addressing
Instead of storing a mapping of token pairs to pool addresses, Uniswap uses the CREATE2 factory contract, rather than storing a mapping of token pairs to pool addresses. This practice saves gas by ignoring storage access. The calculation is as follows:
solidity
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
(address token0, address token1) = sortTokens(tokenA, tokenB);
pair = address(uint(keccak256(abi.encodePacked(
hex'ff',
factory,
keccak256(abi.encodePacked(token0, token1)),
hex'96e8ac4277198f8fbbf785487aa39f430f63b76db002cb326e37da348845f' // init code hash
))));
}
Why Not Use Clones?
Although the EIP 1167 clone pattern allows cheap deployment of contracts, at the same time, it also includes additional gas cost per transaction, due to the presence of delegatecall operations. As Uniswap pools are created to use frequently, the savings will not affect the impact of the long-term transaction costs and make the deployment of full contracts more efficient.
Final Thoughts:
Uniswap and AMMs have transformed decentralized trading by simplifying token swaps and offering continuous liquidity. Although they offer benefits like gas efficiency and automated pricing, challenges like slippage of price and impermanent loss remain constant. Understanding these dynamics helps traders and liquidity providers work effectively in a DeFi environment.