Uniswap liquidity pools are autonomous and use the Constant Product Market Maker (x * y = k
). This model was formalized and the smart contract implementation passed a lightweight formal verification.
Create Exchange
The createExchange
function is used to deploy exchange contracts for ERC20 tokens that do not yet have one.
factory.methods.createExchange(tokenAddress).send()
Once an exchange is created the address can be retrieved with getExchange
.
Exchange Reserves
Each exchange contract holds a liquidity reserve of ETH and its associated ERC20 token.
ETH Reserve
The ETH reserve associated with an ERC20 token exchange is the ETH balance of the exchange smart contract.
const ethReserve = web3.eth.getBalance(exchangeAddress)
ERC20 Reserve
The ERC20 reserve associated with an ERC20 token exchange is the ERC20 balance of the exchange smart contract.
const tokenReserve = tokenContract.methods.balanceOf(exchangeAddress)
Add Liquidity
Anyone who wants can join a Uniswap liquidity pool by calling the addLiquidity
function.
exchange.methods.addLiquidity(min_liquidity, max_tokens, deadline).send({ value: ethAmount })
Adding liquidity requires depositing an equivalent value of ETH and ERC20 tokens into the ERC20 token's associated exchange contract.
The first liquidity provider to join a pool sets the initial exchange rate by depositing what they believe to be an equivalent value of ETH and ERC20 tokens. If this ratio is off, arbitrage traders will bring the prices to equilibrium at the expense of the initial liquidity provider.
All future liquidity providers deposit ETH and ERC20's using the exchange rate at the moment of their deposit. If the exchange rate is bad there is a profitable arbitrage opportunity that will correct the price.
Parameters
The ethAmount
sent to addLiquidity
is the exact amount of ETH that will be deposited into the liquidity reserves. It should be 50% of the total value a liquidity provider wishes to deposit into the reserves.
Since liquidity providers must deposit at the current exchange rate, the Uniswap smart contracts use ethAmount
to determine the amount of ERC20 tokens that must be deposited. This token amount is the remaining 50% of total value a liquidity provider wishes to deposit. Since exchange rate can change between when a transaction is signed and when it is executed on Ethereum, max_tokens
is used to bound the amount this rate can fluctuate. For the first liquidity provider, max_tokens
is the exact amount of tokens deposited.
Liquidity tokens are minted to track the relative proportion of total reserves that each liquidity provider has contributed. min_liquidity
is used in combination with max_tokens
and ethAmount
to bound the rate at which liquidity tokens are minted. For the first liquidity provider, min_liquidity
does not do anything and can be set to 0.
Transaction deadline
is used to set a time after which a transaction can no longer be executed. This limits the "free option" problem, where Ethereum miners can hold signed transactions and execute them based off market movements.
Remove Liquidity
Liquidity providers use the removeLiquidity
function to withdraw their portion of the reserves.
exchange.methods.removeLiquidity(amount, min_eth, min_tokens, deadline).send()
Liquidity is withdrawn at the same ratio as the reserves at the time of withdrawal. If the exchange rate is bad there is a profitable arbitrage opportunity that will correct the price.
Parameters
amount
specifies the number of liquidity tokens that will be burned. Dividing this amount by the total liquidity token supply gives the percentage of both the ETH and ER20 reserves the provider is withdrawing.
Since exchange rate can change between when a transaction is signed and when it is executed on Ethereum, min_eth
and min_tokens
are used to bound the amount this rate can fluctuate.
Same as in addLiquidity
, deadline
is used to set a time after which a transaction can no longer be executed.
FAQs
Liquidity pools make it possible to trade crypto without the need for a central intermediary maintaining an order book. This allows traders to swap tokens directly from their wallets, reducing counterparty risk and exposure to certain risks that centralized exchanges may face, like employee theft.
Do liquidity pools make money? ›
Liquidity pools pave a way for liquidity providers to earn interest on their digital assets. By locking their tokens into a smart contract, users can earn a portion of the transaction fees generated from trading activity in the pool.
How do you calculate liquidity pool? ›
For one, in v2 the liquidity of a pool and the liquidity of a position are conceptually the same, the L = sqrt(x*y) formula works for both. The liquidity of a position is uniform in the range [p_a, p_b] , and zero for prices outside of this range.
Can you lose in liquidity pool? ›
Impermanent loss occurs when the price of a token rises or falls after you deposit it in a liquidity pool. It indicates a loss when the dollar value of your token at the time of withdrawal is less than the amount deposited.
How do you fund a liquidity pool? ›
To start a liquidity pool, users deposit equal values of two tokens into the pool, creating what is known as a trading pair. For instance, if you want to create a liquidity pool for BTC and ETH, you'd deposit an equal value of both assets. Liquidity pools leverage algorithms to determine token prices.
How do you get paid in a liquidity pool? ›
Users who provide liquidity are called 'liquidity providers' and are rewarded with a percentage of the fees that buyers and sellers pay for using the liquidity pool to trade tokens. The fees are distributed proportionally to liquidity providers based on the amount of capital they contributed to the pool.
What is better staking or liquidity pool? ›
Liquidity pools offer potentially higher rewards but require advanced knowledge, understanding of market dynamics, and active participation. Liquid staking is a simpler and more accessible strategy, suitable for beginners or individuals seeking a conservative and consistent approach to earning passive income.
How are liquidity pool fees paid? ›
Fees are collected by burning liquidity tokens to remove a proportional share of the underlying reserves. Since fees are added to liquidity pools, the invariant increases at the end of every trade.
Is liquidity pool safe? ›
Some common vulnerabilities and risks associated with liquidity pools include: Impermanent Loss: Impermanent loss occurs when the price of the assets in the liquidity pool changes relative to the price outside of the pool. Liquidity providers can experience financial losses when withdrawing their assets.
Can you withdraw from liquidity pool? ›
Select or search for a liquidity pool you'd like to withdraw liquidity from. In the "Withdraw Liquidity" panel, enter the amount of tokens you would like to withdraw from the liquidity pool (or use the slider!) and click “Withdraw Liquidity” at the bottom.
A regular user can create a liquidity pool on 1inch with the Balancer protocol in just a click, configuring its size and the weight of each currency. One way to earn an income in the crypto space is by creating and running a liquidity pool — a pool of tokens locked on a smart contract.
Is liquidity pool profitable? ›
Liquidity pools are primarily in pairs e.g. ETH/USD. Providing liquidity for DEXs is a type of yield farming and some investors see it as more profitable than just buying and holding because LPs receive rewards from trading fees. However, LPs lose money due to Impermanent Loss (IL).
What is a liquidity pool? ›
A liquidity pool is a smart contract where tokens are locked for the purpose of providing liquidity. Some of the important concepts required to understand how liquidity pools and decentralised exchanges work include liquidity providers, liquidity tokens and automated market makers.
What assets are in a liquidity pool? ›
Liquidity pools are typically composed of two or more assets that are paired together in a specific ratio, such as 50% ETH and 50% DAI. The ratio is maintained by an automated market maker (AMM) algorithm, which adjusts the price of the assets in the pool based on the relative supply and demand.
What is the liquidity of a pooled fund? ›
Liquidity -the ease with which the assets can be bought and sold. Pooled investment funds are highly liquid and usually trade a number of times per day. Diversification - investing in a large number of assets to reduce overall risk.
What is the difference between liquidity and liquidity pool? ›
Liquidity provision is done through crypto liquidity pools, whereby users contribute assets and play a direct role in facilitating the market. A decentralized exchange (DEX) is a two-sided marketplace and liquidity providers are only one side. Traders (the demand to buy or sell) are on the other side.
How does a liquidity pool balance? ›
Remember that the assets in a liquidity pool are always balanced in value. This means that the pool automatically 'gives away' a portion of the price appreciation to make sure the constant product formula stays true.
What does it mean to burn a liquidity pool? ›
The burning of pools (liquidity burning) on Solana refers to the destruction or removal of liquidity tokens (LP tokens) from a wallet.