Skip to content

Instantly share code, notes, and snippets.

@epheph
Last active June 20, 2020 21:49
Show Gist options
  • Save epheph/0b115f9f1961c3ac84866ee06a94426c to your computer and use it in GitHub Desktop.
Save epheph/0b115f9f1961c3ac84866ee06a94426c to your computer and use it in GitHub Desktop.
contract UniswapV2Pair {
// Contract Storage Variables:
uint public price0CumulativeLast;
uint public price1CumulativeLast;
...
// The only place these storage variables are updated:
function _update(uint balance0, uint balance1, uint112 _reserve0, uint112 _reserve1) private {
uint32 timeElapsed = blockTimestamp - blockTimestampLast;
if (timeElapsed > 0 && _reserve0 != 0 && _reserve1 != 0) {
price0CumulativeLast += uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0)) * timeElapsed;
price1CumulativeLast += uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1)) * timeElapsed;
}
blockTimestampLast = blockTimestamp;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment