泰山眾籌(阿凡達)開發邏輯丨泰山眾籌(阿凡達)系統開發成熟案例及原始碼分析

xiaofufu發表於2023-02-25

  新零售是指個人、企業以網際網路為依託,透過運用大資料、人工智慧等先進技術手段並運用心理學知識,對商品的生產、流通與銷售過程進行升級改造,進而重塑業態結構與生態圈,並對線上服務、線下體驗以及現代物流進行深度融合的零售新模式。


  “新零售”模式打破了線上和線下之前的各自封閉狀態,線上線下得以相互融合、取長補短且相互依賴,線上更多履行交易與支付的職能,線下通常作為篩選與體驗的平臺,高效物流則將線上線下相連線並與其共同作用形成商業閉環。在“新零售”模式下,消費者可以任意暢遊在智慧、高效、快捷、平價、愉悅的購物環境之中,購物體驗獲得大幅提升,年輕群體對消費升級的強烈意願也由此得到較好滿足。


  //this low-level function should be called from a contract which performs important safety checks


  //這個低階函式應該從執行重要安全檢查的合約中呼叫


  function mint(address to)external lock returns(uint liquidity){


  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings


  //合約裡兩種token的當前的balance


  uint balance0=IERC20(token0).balanceOf(address(this));


  uint balance1=IERC20(token1).balanceOf(address(this));


  //獲得當前balance和上一次快取的餘額的差值


  //因為balance是動態變化的,reserve是靜態變化的


  uint amount0=balance0.sub(_reserve0);


  uint amount1=balance1.sub(_reserve1);


  //計算手續費


  bool feeOn=_mintFee(_reserve0,_reserve1);


  //gas節省,必須在此處定義,因為totalSupply可以在_mintFee中更新


  //totalSupply是pair的憑證


  uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee


  if(_totalSupply==0){


  //第一次鑄幣,也就是第一次注入流動性,值為根號k減去MINIMUM_LIQUIDITY,防止資料溢位


  liquidity=Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);


  //把MINIMUM_LIQUIDITY賦給地址0,永久鎖住


  _mint(address(0),MINIMUM_LIQUIDITY);//permanently lock the first MINIMUM_LIQUIDITY tokens


  }else{


  //計算增量的token佔總池子的比例,作為新鑄幣的數量


  //木桶法則,按最少的來,按當前投入的佔池子總的比例增發


  liquidity=Math.min(amount0.mul(_totalSupply)/_reserve0,amount1.mul(_totalSupply)/_reserve1);


  }


  require(liquidity>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_MINTED');


  //鑄幣,修改to的token數量及totalsupply


  //給to地址發憑證,同時pair合約的totalSupply增發同等的憑證


  _mint(to,liquidity);


  //更新時間加權平均價格


  _update(balance0,balance1,_reserve0,_reserve1);


  if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date


  emit Mint(msg.sender,amount0,amount1);


  }


  //this low-level function should be called from a contract which performs important safety checks


  function burn(address to)external lock returns(uint amount0,uint amount1){


  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings


  address _token0=token0;//gas savings


  address _token1=token1;//gas savings


  uint balance0=IERC20(_token0).balanceOf(address(this));


  uint balance1=IERC20(_token1).balanceOf(address(this));


  uint liquidity=balanceOf[address(this)];


  bool feeOn=_mintFee(_reserve0,_reserve1);


  uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee


  //計算返回的amount0/1


  amount0=liquidity.mul(balance0)/_totalSupply;//using balances ensures pro-rata distribution


  amount1=liquidity.mul(balance1)/_totalSupply;//using balances ensures pro-rata distribution


  require(amount0>0&&amount1>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_BURNED');


  _burn(address(this),liquidity);


  //_token0/1給to轉amount0/1


  _safeTransfer(_token0,to,amount0);


  _safeTransfer(_token1,to,amount1);


  //獲取轉賬後的balance


  balance0=IERC20(_token0).balanceOf(address(this));


  balance1=IERC20(_token1).balanceOf(address(this));


  //更新reserve0,reserve1和時間戳


  _update(balance0,balance1,_reserve0,_reserve1);


  if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date


  emit Burn(msg.sender,amount0,amount1,to);


  }


  //this low-level function should be called from a contract which performs important safety checks


  //交易函式


  //可以是token0-->token1,


  //也可以是token1-->token0


  //但引數中:amount0Out和amount1Out中有一個值是0


  function swap(


  uint amount0Out,


  uint amount1Out,


  address to,


  bytes calldata data


  )external lock


  {


  require(amount0Out>0||amount1Out>0,'UniswapV2:INSUFFICIENT_OUTPUT_AMOUNT');


  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings


  require(amount0Out<_reserve0&&amount1Out<_reserve1,'UniswapV2:INSUFFICIENT_LIQUIDITY');


  uint balance0;


  uint balance1;


  {//scope for _token{0,1},avoids stack too deep errors


  address _token0=token0;


  address _token1=token1;


  require(to!=_token0&&to!=_token1,'UniswapV2:INVALID_TO');


  //劃轉操作


  if(amount0Out>0)_safeTransfer(_token0,to,amount0Out);//optimistically transfer tokens


  if(amount1Out>0)_safeTransfer(_token1,to,amount1Out);//optimistically transfer tokens


  if(data.length>0)IUniswapV2Callee(to).uniswapV2Call(msg.sender,amount0Out,amount1Out,data);


  balance0=IERC20(_token0).balanceOf(address(this));


  balance1=IERC20(_token1).balanceOf(address(this));


  }


  uint amount0In=balance0>_reserve0-amount0Out?balance0-(_reserve0-amount0Out):0;


  uint amount1In=balance1>_reserve1-amount1Out?balance1-(_reserve1-amount1Out):0;


  require(amount0In>0||amount1In>0,'UniswapV2:INSUFFICIENT_INPUT_AMOUNT');


  {//scope for reserve{0,1}Adjusted,avoids stack too deep errors


  //防止資料溢位校驗


  uint balance0Adjusted=balance0.mul(1000).sub(amount0In.mul(3));


  uint balance1Adjusted=balance1.mul(1000).sub(amount1In.mul(3));


  require(balance0Adjusted.mul(balance1Adjusted)>=uint(_reserve0).mul(_reserve1).mul(1000**2),'UniswapV2:K');


  }


  //更新


  _update(balance0,balance1,_reserve0,_reserve1);


  emit Swap(msg.sender,amount0In,amount1In,amount0Out,amount1Out,to);


  }


  //force balances to match reserves


  //強制balance以匹配儲備


  function skim(address to)external lock{


  address _token0=token0;//gas savings


  address _token1=token1;//gas savings


  _safeTransfer(_token0,to,IERC20(_token0).balanceOf(address(this)).sub(reserve0));


  _safeTransfer(_token1,to,IERC20(_token1).balanceOf(address(this)).sub(reserve1));


  }


  //force reserves to match balances


  //強制儲備以匹配balance


  function sync()external lock{


  _update(IERC20(token0).balanceOf(address(this)),IERC20(token1).balanceOf(address(this)),reserve0,reserve1);


  }


  }


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

相關文章