Reputation: 53
I'm studying UniswapV2Pair.sol https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol and I have some question about the mint
and burn
function.
What I understand:
mint
function mints the new liquidity token and send to the userburn
function burns the new liquidity token and sends the deposited token pair back to user .What I'm confused about:
I'm confused about the bold part of burn
function I mentioned above. I think that mint
and burn
function is like mirror(opposite) function, but mint
function doesn't include the feature which token pair are send to the exchange contract. However, burn
function uses _safeTransfer
which sends the token pair back to user.
I'm confused why did they designed assymetrically.
Upvotes: 1
Views: 1622
Reputation: 49551
but mint function doesn't include the feature which token pair are send to the exchange contract.
You showed the UniswapV2Pair.sol. mint
function is used when we add liquidity which is defined in UniswapV2Router02.sol
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external virtual override ensure(deadline) returns (uint amountA, uint amountB, uint liquidity) {
(amountA, amountB) = _addLiquidity(tokenA, tokenB, amountADesired, amountBDesired, amountAMin, amountBMin);
address pair = UniswapV2Library.pairFor(factory, tokenA, tokenB);
TransferHelper.safeTransferFrom(tokenA, msg.sender, pair, amountA);
TransferHelper.safeTransferFrom(tokenB, msg.sender, pair, amountB);
liquidity = IUniswapV2Pair(pair).mint(to);
}
we are passing the amount of tokens that we want to add liquidity, since we cannot add an arbitrary amount, adding liquidity should not affect on the price, so the function itself is calculating the proper amounts and then those amounts are transferred to the UniswapV2Router02
contract. After those amounts are transferred, we are minting a new LP token and sending it to the to
address
IUniswapV2Pair(pair).mint(to)
Inside mint
function how much token will be minted is calculated and then _mint
function called
_mint(to, liquidity);
Upvotes: 0
Reputation: 43561
The mint()
function calculates the amount of minted LP tokens from the difference of
_reserve0
and _reserve1
)balance0
and balance1
)So theoretically, if Alice just sent the underlying tokens to the pair contract without invoking the mint()
function, that would make the accounting difference described above. And Bob would be able to invoke the mint()
function and mint the LP tokens for himself profiting off Alice.
But that's not the usual process flow. Usually, the liquidity provider (Alice), invokes the addLiquidity() function of the router contract that performs both actions at once:
mint()
function on the pair contract calculating the difference created in this transactionWhich removes the possibility for Bob to intercept the Alice's minting process.
And having the mint()
function executable by itself also allows anyone to claim unclaimed tokens that were sent to the pair contract by mistake for example.
However, if you want to transfer the underlying tokens out of the pair contract (i.e. burn()
the LP tokens), there needs to be check already in the burn()
function so that you can't claim more of the underlying tokens than you're eligible to.
No matter if you're invoking the pair burn()
function directly or from the router removeLiquidity()
(that's normally invoked from the Uniswap UI).
Upvotes: 1