智慧合約盲盒遊戲開發上線版丨智慧合約盲盒遊戲系統開發(開發案例)

xiaofufu發表於2023-03-01

  Blockchain technology can thus empower enterprises in many ways:providing reliable shared data and building trust between all parties;Eliminate data islands,that is,integrate data into a system through centralized ledgers that are shared in a network and support the access of licensees;Give high security to data;


  interface IWETH{


  function deposit()external payable;


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


  function withdraw(uint)external;


  }


  contract UniswapV2Router02 is IUniswapV2Router02{


  using SafeMath for uint;


  address public immutable override factory;


  address public immutable override WETH;


  //交易時間是否過期,dapp中預設設定時20分鐘內成交有效


  modifier ensure(uint deadline){


  require(deadline>=block.timestamp,'UniswapV2Router:EXPIRED');


  _;


  }


  constructor(address _factory,address _WETH)public{


  factory=_factory;


  WETH=_WETH;


  }


  receive()external payable{


  assert(msg.sender==WETH);//only accept ETH via fallback from the WETH contract


  }


  //****ADD LIQUIDITY****


  //新增流動性內部方法,透過該方法計算出兩個幣的實際所需數量


  function _addLiquidity(


  address tokenA,//代幣地址A


  address tokenB,//代幣地址B


  uint amountADesired,//代幣A期望新增量


  uint amountBDesired,//代幣B期望新增量


  uint amountAMin,//代幣A最小新增量(這兩個min,收益新增的時候可以和Desired一樣,二次新增的時候,一般都是小於Desired,具體小多少,演演算法可以檢視uniswap前端程式碼)


  uint amountBMin//代幣B最小新增量


  )internal virtual returns(uint amountA,uint amountB){//返回值是兩個


  //create the pair if it doesn't exist yet


  //透過factory,查詢pair,如果等於0地址,就表示還沒有該交易對,呼叫建立方法


  if(IUniswapV2Factory(factory).getPair(tokenA,tokenB)==address(0)){


  IUniswapV2Factory(factory).createPair(tokenA,tokenB);//建立交易對


  }功能及詳細:MrsFu123


  //可以先了解下UniswapV2Library中相關方法的意思


  //如果查詢兩個值都是0,首次新增,直接使用期望值


  (uint reserveA,uint reserveB)=UniswapV2Library.getReserves(factory,tokenA,tokenB);


  if(reserveA==0&&reserveB==0){


  (amountA,amountB)=(amountADesired,amountBDesired);//直接使用這兩個值,比例就是相互的幣價


  }else{


  //如果兩個儲備量不為0,需要根據當前的價格/比例去新增流動性


  //先透過quote計算如果輸入A的數量,得出B的實際輸入量


  uint amountBOptimal=UniswapV2Library.quote(amountADesired,reserveA,reserveB);


  //如果B的實際輸入量<=B的期望輸入數量,


  if(amountBOptimal<=amountBDesired){


  //實際輸入量需要大於等於引數中的最小數量


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


  (amountA,amountB)=(amountADesired,amountBOptimal);//得到兩個的實際新增量


  }else{


  //如果上面計算的B的實際輸入量大於期望輸入量,就說明使用者得B數量不夠,需要反過來,透過B計算A的數量,看A的數量是否滿足,


  //透過B計算A的數量


  uint amountAOptimal=UniswapV2Library.quote(amountBDesired,reserveB,reserveA);//


  assert(amountAOptimal<=amountADesired);//需要計算得來的A量小於等於A的預期輸入量


  require(amountAOptimal>=amountAMin,'UniswapV2Router:INSUFFICIENT_A_AMOUNT');//且實際輸入量,需要大於等於最小數量


  (amountA,amountB)=(amountAOptimal,amountBDesired);//得到兩個的實際新增量


  }


  }


  }


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

相關文章