PAXG算力挖礦/流動性質押挖礦節點/系統開發/DAPP合約/方案詳情/成熟技術/原始碼

搭建MrsFu123發表於2023-05-19

  通俗地說,可以把區塊鏈比作一種“賬本”。傳統賬本由一方“集中記賬”,這種新式“賬本”則可以在網際網路上由多方參與、共享,各參與方都可以“記賬”並備份,而每個備份就是一個“區塊”。每個“區塊”與下一個“區塊”按時間順序線性相連,其結構特徵使記錄無法被篡改和偽造。


  區塊鏈(Blockchain)是由節點參與的分散式資料庫系統,V+++mrsfu123 也可以將其理解為賬簿系統(ledger),其實就是用來記賬,用來記錄每一筆交易的,且能保證每一筆交易記錄公開透明,不可篡改偽造。由於是去中心化的網路,在區塊鏈上的每一筆交易都需要“礦工”去“挖礦”來記下這筆帳,儲存到鏈上去。


  以太坊採用了Solidity作為智慧合約語言,I35開閥7O98模式O7I8,Solidity是一門為實現智慧合約而建立的gao.級程式語言,能在允許以太坊程式的節點上執行。該語言吸收了C++、JavaScript的一些特性,例如它是靜態型別語言,支援繼承庫等。


  //fetches and sorts the reserves for a pair


  function getReserves(address factory,address tokenA,address tokenB)internal view returns(uint reserveA,uint reserveB){


  (address token0,)=sortTokens(tokenA,tokenB);


  (uint reserve0,uint reserve1,)=IUniswapV2Pair(pairFor(factory,tokenA,tokenB)).getReserves();


  (reserveA,reserveB)=tokenA==token0?(reserve0,reserve1):(reserve1,reserve0);


  }


  //given some amount of an asset and pair reserves,returns an equivalent amount of the other asset


  function quote(uint amountA,uint reserveA,uint reserveB)internal pure returns(uint amountB){


  require(amountA>0,'UniswapV2Library:INSUFFICIENT_AMOUNT');


  require(reserveA>0&&reserveB>0,'UniswapV2Library:INSUFFICIENT_LIQUIDITY');


  amountB=amountA.mul(reserveB)/reserveA;


  }


  //given an input amount of an asset and pair reserves,returns the maximum output amount of the other asset


  function getAmountOut(uint amountIn,uint reserveIn,uint reserveOut)internal pure returns(uint amountOut){


  require(amountIn>0,'UniswapV2Library:INSUFFICIENT_INPUT_AMOUNT');


  require(reserveIn>0&&reserveOut>0,'UniswapV2Library:INSUFFICIENT_LIQUIDITY');


  uint amountInWithFee=amountIn.mul(997);


  uint numerator=amountInWithFee.mul(reserveOut);


  uint denominator=reserveIn.mul(1000).add(amountInWithFee);


  amountOut=numerator/denominator;


  }


  //given an output amount of an asset and pair reserves,returns a required input amount of the other asset


  function getAmountIn(uint amountOut,uint reserveIn,uint reserveOut)internal pure returns(uint amountIn){


  require(amountOut>0,'UniswapV2Library:INSUFFICIENT_OUTPUT_AMOUNT');


  require(reserveIn>0&&reserveOut>0,'UniswapV2Library:INSUFFICIENT_LIQUIDITY');


  uint numerator=reserveIn.mul(amountOut).mul(1000);


  uint denominator=reserveOut.sub(amountOut).mul(997);


  amountIn=(numerator/denominator).add(1);


  }


  //performs chained getAmountOut calculations on any number of pairs


  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;


  for(uint i;i<path.length-1;i++){


  (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


  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;


  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);


  }


  }


  }


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

相關文章