Questions about mint, burn function in UniswapV2Pair.sol

Viewed 605

I'm studying UniswapV2Pair.sol https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol and I have some question about 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:

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 includes the feature which token pair are send to the exchange contract. However, burn function uses _safeTransfer which send the token pair back to user.

I'm confused why did they designed assymetrically.

1 Answers

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

Related