火幣鏈/波場鏈/OK鏈/幣安鏈/馬蹄鏈盲盒遊戲系統開發實現技術原理及分析

xiaofufu發表於2023-03-01

  真正實現以大資料、雲端計算、人工智慧、沉浸現實等諸多新技術實現聯通,並且真正構建起一個完整的全新世界的,正是區塊鏈技術。這便是區塊鏈的功能。透過元宇宙,我們真正看到了區塊鏈技術的這樣一種聯通、融合的角色與作用;透過元宇宙,我們看到了區塊鏈技術的這樣一種建構於生態之上的強大的能力。


  //performs chained getAmountOut calculations on any number of pairs


  //根據path,計算出每個交易對的輸入/輸出量(如果path>2,前一個交易對的輸出量,就是下一個交易對交易的輸入量)


  //內部實際還是呼叫的上面getAmountOut方法,返回值amounts長度和path的長度一致,


  function getAmountsOut(address factory,uint amountIn,address[]memory path)internal view returns(uint[]memory amounts){


  require(path.length>=2,'UniswapV2Library:INVALID_PATH');


  amounts=new uint[](path.length);//建立陣列


  amounts[0]=amountIn;//0位置是輸入量


  for(uint i;i<path.length-1;i++){//每兩個token組成一個交易對,計算out


  (uint reserveIn,uint reserveOut)=getReserves(factory,path<i>,path[i+1]);


  amounts[i+1]=getAmountOut(amounts<i>,reserveIn,reserveOut);


  }

  }


  //performs chained getAmountIn calculations on any number of pairs


  //根據path,計算出每個交易對的輸入/輸出量(如果path>2,前一個交易對的輸出量,就是下一個交易對交易的輸入量)


  //內部實際還是呼叫的上面getAmountIn方法,返回值amounts長度和path的長度一致,


  function getAmountsIn(address factory,uint amountOut,address[]memory path)internal view returns(uint[]memory amounts){


  require(path.length>=2,'UniswapV2Library:INVALID_PATH');


  amounts=new uint[](path.length);


  amounts[amounts.length-1]=amountOut;//最後一個是入參out,


  for(uint i=path.length-1;i>0;i--){//倒序遍歷計算


  (uint reserveIn,uint reserveOut)=getReserves(factory,path[i-1],path<i>);


  amounts[i-1]=getAmountIn(amounts<i>,reserveIn,reserveOut);


  }


  }


  }設計方案:MrsFu123


  //helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false


  //轉賬工具類


  library TransferHelper{


  function safeApprove(address token,address to,uint value)internal{


  //bytes4(keccak256(bytes('approve(address,uint256)')));


  (bool success,bytes memory data)=token.call(abi.encodeWithSelector(0x095ea7b3,to,value));


  require(success&&(data.length==0||abi.decode(data,(bool))),'TransferHelper:APPROVE_FAILED');


  }


  //注:data.length==0,主要針對的是usdt,同時!該方法在波場不適用!!波場的的U有返回data,但是一直是false!!


  function safeTransfer(address token,address to,uint value)internal{


  //bytes4(keccak256(bytes('transfer(address,uint256)')));


  (bool success,bytes memory data)=token.call(abi.encodeWithSelector(0xa9059cbb,to,value));


  require(success&&(data.length==0||abi.decode(data,(bool))),'TransferHelper:TRANSFER_FAILED');


  }


  function safeTransferFrom(address token,address from,address to,uint value)internal{


  //bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));


  (bool success,bytes memory data)=token.call(abi.encodeWithSelector(0x23b872dd,from,to,value));


  require(success&&(data.length==0||abi.decode(data,(bool))),'TransferHelper:TRANSFER_FROM_FAILED');


  }


  function safeTransferETH(address to,uint value)internal{


  (bool success,)=to.call{value:value}(new bytes(0));


  require(success,'TransferHelper:ETH_TRANSFER_FAILED');


  }


  }


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

相關文章