Solidity語言/LP/DAPP合約代幣質押流動性挖礦系統技術開發詳情

丸子qy發表於2023-05-17

  

  智慧合約dapp開發技術主要由以太坊區塊鏈網路提供支援,該網路提供了一系列的智慧合約技術,這些智慧合約可以讓開發者快速、安全地構建出功能強大的dapp。智慧合約dapp開發技術主要包括以太坊智慧合約語言Solidity,以太坊智慧合約框架Truffle,Web3.js,以太坊區塊鏈瀏覽器Mist等

  

  智慧合約:它們是儲存在區塊鏈上的計算機程式,在滿足預定條件時執行,智慧合約是用Solidity語言編寫的。

  

  web3.js是一個JavaScript API庫。要讓DApp在以太坊上執行,我們可以使用web3.js庫提供的web3物件。web3.js透過RPC呼叫與本地節點通訊,詳細唯:wwqqyy420 它可以與任何公開RPC層的以太坊節點一起使用。web3包含eth物件-web3.eth(用於與以太坊區塊鏈互動)和shh物件-web3.shh(用於與Whisper互動)

  

  //this low-level function should be called from a contract which performs important safety checks

  

  function burn(address to)external lock returns(uint amount0,uint amount1){

  

  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings

  

  address _token0=token0;//gas savings

  

  address _token1=token1;//gas savings

  

  uint balance0=IERC20(_token0).balanceOf(address(this));

  

           開發功能176詳情0206系統5616

  

  uint balance1=IERC20(_token1).balanceOf(address(this));

  

  uint liquidity=balanceOf[address(this)];

  

  bool feeOn=_mintFee(_reserve0,_reserve1);

  

  uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee

  

  amount0=liquidity.mul(balance0)/_totalSupply;//using balances ensures pro-rata distribution

  

  amount1=liquidity.mul(balance1)/_totalSupply;//using balances ensures pro-rata distribution

  

  require(amount0>0&&amount1>0,'UniswapV2:INSUFFICIENT_LIQUIDITY_BURNED');

  

  _burn(address(this),liquidity);

  

  _safeTransfer(_token0,to,amount0);

  

  _safeTransfer(_token1,to,amount1);

  

  balance0=IERC20(_token0).balanceOf(address(this));

  

  balance1=IERC20(_token1).balanceOf(address(this));

  

  _update(balance0,balance1,_reserve0,_reserve1);

  

  if(feeOn)kLast=uint(reserve0).mul(reserve1);//reserve0 and reserve1 are up-to-date

  

  emit Burn(msg.sender,amount0,amount1,to);

  

  }

  

  //this low-level function should be called from a contract which performs important safety checks

  

  function swap(uint amount0Out,uint amount1Out,address to,bytes calldata data)external lock{

  

  require(amount0Out>0||amount1Out>0,'UniswapV2:INSUFFICIENT_OUTPUT_AMOUNT');

  

  (uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings

  

  require(amount0Out<_reserve0&&amount1Out<_reserve1,'UniswapV2:INSUFFICIENT_LIQUIDITY');

  

  uint balance0;

  

  uint balance1;

  

  {//scope for _token{0,1},avoids stack too deep errors

  

  address _token0=token0;

  

  address _token1=token1;

  

  require(to!=_token0&&to!=_token1,'UniswapV2:INVALID_TO');

  

  if(amount0Out>0)_safeTransfer(_token0,to,amount0Out);//optimistically transfer tokens

  

  if(amount1Out>0)_safeTransfer(_token1,to,amount1Out);//optimistically transfer tokens

  

  if(data.length>0)IUniswapV2Callee(to).uniswapV2Call(msg.sender,amount0Out,amount1Out,data);

  

  balance0=IERC20(_token0).balanceOf(address(this));

  

  balance1=IERC20(_token1).balanceOf(address(this));

  

  }

  

  uint amount0In=balance0>_reserve0-amount0Out?balance0-(_reserve0-amount0Out):0;

  

  uint amount1In=balance1>_reserve1-amount1Out?balance1-(_reserve1-amount1Out):0;

  

  require(amount0In>0||amount1In>0,'UniswapV2:INSUFFICIENT_INPUT_AMOUNT');

  

  {//scope for reserve{0,1}Adjusted,avoids stack too deep errors

  

  uint balance0Adjusted=balance0.mul(1000).sub(amount0In.mul(3));

  

  uint balance1Adjusted=balance1.mul(1000).sub(amount1In.mul(3));

  

  require(balance0Adjusted.mul(balance1Adjusted)>=uint(_reserve0).mul(_reserve1).mul(1000**2),'UniswapV2:K');

  

  }

  

  _update(balance0,balance1,_reserve0,_reserve1);

  

  emit Swap(msg.sender,amount0In,amount1In,amount0Out,amount1Out,to);

  

  }

  

  //force balances to match reserves

  

  function skim(address to)external lock{

  

  address _token0=token0;//gas savings

  

  address _token1=token1;//gas savings

  

  _safeTransfer(_token0,to,IERC20(_token0).balanceOf(address(this)).sub(reserve0));

  

  _safeTransfer(_token1,to,IERC20(_token1).balanceOf(address(this)).sub(reserve1));

  

  }

  

  //force reserves to match balances

  

  function sync()external lock{

  

  _update(IERC20(token0).balanceOf(address(this)),IERC20(token1).balanceOf(address(this)),reserve0,reserve1);


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

相關文章