How adding liquidity could result in a loss of funds

Jun 26
10
min of reading

Adding liquidity to UniswapV2 is trivial:one can simply directly transfer tokens to the pair contract and then call themint function. This function calculates the liquidity tokens you should receivebased on the current balance in the contract and the recorded reserves. Theprocess is be straightforward: your deposited tokens are converted intoliquidity tokens, representing your share of the pool. The pair will just checkhow much was transferred to it:

There is a small problem: If the transferand the mint call are not atomic, you risk losing the deposited tokens.

This is where skim and sync come into play,providing solutions to potential issues arising from non-atomic transactions:

The skim function:

This function is designed to address theissue of tokens being sent to the pair without a corresponding mint operation.This can happen if a user mistakenly does not execute the deposit in a singletransaction. The skim function allows anyone to call it and transfers theexcess tokens (the difference between the current balance and the recordedreserves) out of the pair to the caller. In essence, if there's a discrepancybecause of a non-atomic transaction, skim can be invoked by a bot or anopportunistic user to "drain" these tokens.

The sync function:

Instead of transferring out excess tokens,sync updates the pair's reserves to match the current balances of the tokens inthe contract. This is crucial after direct token transfers or any event thatcould cause the reserves recorded in the contract to diverge from the actualtoken balances. By calling sync, the contract aligns its reserves with reality,ensuring that the liquidity in the pool accurately reflects the tokens held inthe contract. This action doesn't transfer tokens out but rather incorporatesthese into the pair.

Read the original article

Related articles