update reserve status of pool
balance โ state when token has already been transferred (latest version)
reserve โ state before token is transferred (pre latest version)
To make reserve catch up with balance
(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) โ none
balance0 = 51224550845936
balance1= 44552339419344963588195
reserve0 = 51224550843936
reserve1= 44552339419344963088195
function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
require(balance0 <= uint112(-1) && balance1 <= uint112(-1), 'UniswapV2: OVERFLOW');
//--------- This code is used to get **TWAP**(I'll explain later) ------------
uint32 blockTimestamp = uint32(block.timestamp % 2**32);
uint32 timeElapsed = blockTimestamp - blockTimestampLast; // overflow is desired
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
// * never overflows, and + overflow is desired
price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}
//---------------------------------------------------------------------
reserve0 = uint112(balance0);
reserve1 = uint112(balance1);
blockTimestampLast = blockTimestamp; // TWAP
emit Sync(reserve0, reserve1);
}
https://github.com/Uniswap/v2-core/blob/master/contracts/UniswapV2Pair.sol#L73