泰山眾籌(阿凡達)系統開發成熟及方案丨泰山眾籌阿凡達開發原始碼功能

xiaofufu發表於2023-02-20

  ...


  {


  //需要balance0和blanace1不超過uint112的上限


  require(balance0<=uint112(-1)&&balance1<=uint112(-1),'UniswapV2:OVERFLOW');


  //blockTimestamp只取最後32位


  uint32 blockTimestamp=uint32(block.timestamp%2**32);


  //計算時間差timeElapsed


  uint32 timeElapsed=blockTimestamp-blockTimestampLast;


  //如果timeElapsed>0&&_reserve0!=0&&_reserve1!=0


  if(timeElapsed>0&&_reserve0!=0&&_reserve1!=0){


  //對_reserve1/_reserve0*timeElapsed的結果在price0CumulativeLast上累加


  price0CumulativeLast+=uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0))*timeElapsed;


  //對_reserve0/_reserve1*timeElapsed的結果在price1CumulativeLast上累加


  price1CumulativeLast+=uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1))*timeElapsed;


  }


  //reserve0=balance0


  reserve0=uint112(balance0);


  //reserve1=balance1


  reserve1=uint112(balance1);


  //blockTimestampLast=blockTimestamp


  blockTimestampLast=blockTimestamp;


  //


  emit Sync(reserve0,reserve1);


  }


  函式_update中對price0CumulativeLast和price1CumulativeLast進行了與時間成反比的數值累加,可以透過這兩個變數計算出相對平衡的市場價格。


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69956839/viewspace-2936026/,如需轉載,請註明出處,否則將追究法律責任。

相關文章