智能合约是运行在区块链系统可复制、共享账本上的计算机程序,可以处理信息,接收、储存和发送价值。基于区块链技术的智能合约,不仅可以发挥智能合约在成本效率方面的优势,而且可以避免恶意行为对合约正常执行的干扰。将智能合约以数字化的形式写入区块链中,由区块链技术的特性保障存储、读取、执行整个过程透明可跟踪、不可篡改。同时,由区块链自带的共识算法构建出一套状态机系统,使智能合约能够高效地运行。
//if fee is on,mint liquidity equivalent to 1/6th of the growth in sqrt(k)
function _mintFee(uint112 _reserve0,uint112 _reserve1)private returns(bool feeOn){
address feeTo=IUniswapV2Factory(factory).feeTo();
feeOn=feeTo!=address(0);
uint _kLast=kLast;//gas savings
if(feeOn){
if(_kLast!=0){
uint rootK=Math.sqrt(uint(_reserve0).mul(_reserve1));
uint rootKLast=Math.sqrt(_kLast);
if(rootK>rootKLast){
uint numerator=totalSupply.mul(rootK.sub(rootKLast));
uint denominator=rootK.mul(5).add(rootKLast);
uint liquidity=numerator/denominator;
if(liquidity>0)_mint(feeTo,liquidity);
}
}
}else if(_kLast!=0){
kLast=0;
}
}
//this low-level function should be called from a contract which performs important safety checks
function mint(address to)external lock returns(uint liquidity){
(uint112 _reserve0,uint112 _reserve1,)=getReserves();//gas savings
uint balance0=IERC20(token0).balanceOf(address(this));
uint balance1=IERC20(token1).balanceOf(address(this));
uint amount0=balance0.sub(_reserve0);
uint amount1=balance1.sub(_reserve1);
bool feeOn=_mintFee(_reserve0,_reserve1);
uint _totalSupply=totalSupply;//gas savings,must be defined here since totalSupply can update in _mintFee
if(_totalSupply==0){
liquidity=Math.sqrt(amount0.mul(amount1)).sub(MINIMUM_LIQUIDITY);
_mint(address(0),MINIMUM_LIQUIDITY);//permanently lock the first MINIMUM_LIQUIDITY tokens
}else{
liquidity=Math.min(amount0.mul(_totalSupply)/_reserve0,amount1.mul(_totalSupply)/_reserve1);
}
require(liquidity>0,