阿凡達(Avatar)泰山眾籌商城系統開發技術詳細及案例

xiaofufu發表於2023-03-01

  The"new retail"model has broken the respective closed state of online and offline.Online and offline can be integrated,complement each other and rely on each other.Online and offline more perform the functions of transaction and payment.Offline is usually used as a platform for screening and experience,while efficient logistics connects online and offline and forms a commercial closed loop with its interaction.


  interface IERC20{


  event Approval(address indexed owner,address indexed spender,uint value);


  event Transfer(address indexed from,address indexed to,uint value);


  function name()external view returns(string memory);


  function symbol()external view returns(string memory);


  function decimals()external view returns(uint8);


  function totalSupply()external view returns(uint);


  function balanceOf(address owner)external view returns(uint);


  function allowance(address owner,address spender)external view returns(uint);


  function approve(address spender,uint value)external returns(bool);


  function transfer(address to,uint value)external returns(bool);


  function transferFrom(address from,address to,uint value)external returns(bool);


  }


  function addLiquidity(//新增流動性,兩個代幣


  address tokenA,


  address tokenB,


  uint amountADesired,


  uint amountBDesired,


  uint amountAMin,


  uint amountBMin,


  address to,//lp接收人,新版的uniswap前端好像不支援設定這個了


  uint deadline//交易的成交時間,預設是當前時間+20分鐘後的時間的秒值


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


  //呼叫內部方法_addLiquidity獲取到兩個幣實際所需要的數量


  (amountA,amountB)=_addLiquidity(tokenA,tokenB,amountADesired,amountBDesired,amountAMin,amountBMin);


  address pair=UniswapV2Library.pairFor(factory,tokenA,tokenB);//查詢到pair地址


  TransferHelper.safeTransferFrom(tokenA,msg.sender,pair,amountA);//給pair轉A數量


  TransferHelper.safeTransferFrom(tokenB,msg.sender,pair,amountB);//給pair轉B數量


  liquidity=IUniswapV2Pair(pair).mint(to);//呼叫pair的mint方法,會有新增的lp數量返回


  }模式及案例:MrsFu123


  function addLiquidityETH(//新增流動性,其中一個幣種是eth


  address token,


  uint amountTokenDesired,


  uint amountTokenMin,


  uint amountETHMin,//eth最小輸入量;對應的Desired在msg.value


  address to,


  uint deadline


  )external virtual override payable ensure(deadline)returns(uint amountToken,uint amountETH,uint liquidity){


  //呼叫內部方法_addLiquidity獲取到兩個幣實際所需要的數量


  //eth使用weth代幣替代


  (amountToken,amountETH)=_addLiquidity(


  token,


  WETH,


  amountTokenDesired,


  msg.value,//ethDesired


  amountTokenMin,


  amountETHMin


  );


  address pair=UniswapV2Library.pairFor(factory,token,WETH);//獲取到pair地址


  TransferHelper.safeTransferFrom(token,msg.sender,pair,amountToken);//給pair轉代幣數量


  IWETH(WETH).deposit{value:amountETH}();//呼叫weth的兌換方法,透過eth換weth


  assert(IWETH(WETH).transfer(pair,amountETH));//給pair轉weth數量


  liquidity=IUniswapV2Pair(pair).mint(to);//呼叫pair的mint方法,會有新增的lp數量返回


  //refund dust eth,if any


  //如果傳入的eth數量,大於實際所需的eth數量,將剩餘的eth返還給使用者


  if(msg.value>amountETH)TransferHelper.safeTransferETH(msg.sender,msg.value-amountETH);


  }


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

相關文章