Avatar阿凡達泰山眾籌商城開發上線版丨Avatar阿凡達泰山眾籌商城系統開發(開發原始碼)

xiaofufu發表於2023-03-01

  New retail refers to a new retail model in which individuals and enterprises,relying on the Internet,upgrade and transform the production,circulation and sales process of goods by using advanced technical means such as big data,artificial intelligence and psychological knowledge,thereby reshaping the business structure and ecosystem,and deeply integrate online services,offline experience and modern logistics.


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


  //****REMOVE LIQUIDITY****


  function removeLiquidity(//移除流動性,該方法需要先將lp代幣授權給路由合約,才能代扣lp


  address tokenA,


  address tokenB,


  uint liquidity,//移除lp的數量,轉入lp得另外兩個幣


  uint amountAMin,//A的最小輸出量


  uint amountBMin,//B的最小輸出量


  address to,//接收兩個幣的地址


  uint deadline


  )public virtual override ensure(deadline)returns(uint amountA,uint amountB){


  address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);//獲取pair地址


  IUniswapV2Pair(pair).transferFrom(msg.sender,pair,liquidity);//將lp轉到pair地址


  //send liquidity to pair


  (uint amount0,uint amount1)=IUniswapV2Pair(pair).burn(to);//呼叫pair的burn方法,內部會將兩個幣的數量轉給to,返回值就是兩個代幣的輸出數量


  (address token0,)=UniswapV2Library.sortTokens(tokenA,tokenB);//透過排序確認兩個amountA/B


  (amountA,amountB)=tokenA==token0?(amount0,amount1):(amount1,amount0);


  //校驗A/B的輸出量需要小於引數中要求的最小量,否則交易失敗


  require(amountA>=amountAMin,'UniswapV2Router:INSUFFICIENT_A_AMOUNT');


  require(amountB>=amountBMin,'UniswapV2Router:INSUFFICIENT_B_AMOUNT');


  }案例及設計:MrsFu123


  function removeLiquidityETH(//移除流動性(其中一個返還幣是ETH),該方法需要先將lp代幣授權給路由合約,才能代扣lp


  address token,


  uint liquidity,


  uint amountTokenMin,


  uint amountETHMin,//eth最小輸出量


  address to,


  uint deadline


  )public virtual override ensure(deadline)returns(uint amountToken,uint amountETH){


  //呼叫上面的removeLiquidity方法,傳入的是WETH


  (amountToken,amountETH)=removeLiquidity(


  token,


  WETH,


  liquidity,


  amountTokenMin,


  amountETHMin,


  address(this),//注意!接收幣的地址是路由


  deadline


  );


  //將代幣轉給to


  TransferHelper.safeTransfer(token,to,amountToken);


  IWETH(WETH).withdraw(amountETH);//將weth轉換成eth


  TransferHelper.safeTransferETH(to,amountETH);//將eth轉給to


  }


  //WithPermit的方法可以先了解下approveAndCall連結


  /*


  實際使用EIP-712


  連結https://soliditydeveloper.com/erc20-permit


  


  


  permit在前端的使用場景,就是移除流動性的時候,有個授權實際沒有傳送交易,只是要求籤名,簽名會得到引數中的v/r/s


  在實際呼叫該移除的方法傳進來,內部驗籤,確認是該使用者,就將移除的lp的數量,授權給路由,可以代扣lp


  */


  function removeLiquidityWithPermit(//移除流動性,approve+transferFrom


  address tokenA,


  address tokenB,


  uint liquidity,


  uint amountAMin,


  uint amountBMin,


  address to,


  uint deadline,


  bool approveMax,uint8 v,bytes32 r,bytes32 s//v,r,s驗籤,透過就授權給路由


  )external virtual override returns(uint amountA,uint amountB){


  /*


  獲取到pair,呼叫pair的permit(內部實際就是授權給路由),


  */


  address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);


  uint value=approveMax?uint(-1):liquidity;


  IUniswapV2Pair(pair).permit(msg.sender,address(this),value,deadline,v,r,s);


  //最終還是呼叫上面的removeLiquidity方法!


  (amountA,amountB)=removeLiquidity(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,deadline);


  }


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

相關文章