区块链的核心——分布式和存储不依赖于中心化的硬件或管理机构,在区块链中的所有节点的权限和义务都是对等的,Therefore,each result can also participate in the recording and maintenance of data.It differs from traditional data structures that rely on the"center"and can achieve point-to-point data transmission and real-time data recording,with higher efficiency and faster speed.
区块链的核心技术之一就是公开且透明的交易信息。一般情况下,在区块链内产生、流转和存储的信息是对所有节点用户开放的,其高度的透明化也使得区块内的所有人都能够查看数据的所有相关信息同时使用其应用。Therefore,blockchain technology has certain advantages in the fields of information sharing and data exchange.
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');
//其中一个不是0的转出
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);
}