简体   繁体   中英

Questions about mint, burn function in UniswapV2Pair.sol

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:

  • When user deposit the token pair, mint function mints the new liquidity token and send to the user
  • When user withdraw the token pair, burn 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.

The mint() function calculates the amount of minted LP tokens from the difference of

  • recorded reserves of the underlying tokens ( _reserve0 and _reserve1 )
  • and the actual underlying token balance owned by the pair contract ( 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:

  • transfers the (approved) tokens from the user to the pair contract
  • invokes the mint() function on the pair contract calculating the difference created in this transaction

Which 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 (ie 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).

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM