区块链作为一种新的信息与网络技术,运用加密技术、分布式网络和共识机制来保证网络中每个节点所记录的信息真实有效。区块链正在不断渗透到各行各业中,已经展现出良好的发展态势。
区块链技术可以构建一个高效可靠的价值传输系统,推动互联网成为构建社会信任的网络基础设施,实现价值的有效传递,并将此称为价值互联网。区块链提供了一种新型的社会信任机制,为数字经济的发展奠定了新基石,“区块链+”应用创新,昭示着产业创新和公共服务的新方向。
区块链最常见的定义是:去中心化、分布式、公开的数字账本,主要用于记录交易信息。
与传统方案不同的是,区块链的交易记录存储在很多不同的计算机上。
contract UniswapV2Pair is IUniswapV2Pair,UniswapV2ERC20{
using SafeMath for uint;
using UQ112x112 for uint224;
//最低流动性
uint public constant MINIMUM_LIQUIDITY=10**3;
//获取transfer方法的bytecode前四个字节
bytes4 private constant SELECTOR=bytes4(keccak256(bytes('transfer(address,uint256)')));
address public factory;
address public token0;
address public token1;
uint112 private reserve0;//uses single storage slot,accessible via getReserves==使用单个存储槽,可通过getReserves访问
uint112 private reserve1;//uses single storage slot,accessible via getReserves
uint32 private blockTimestampLast;//uses single storage slot,accessible via getReserves
uint public price0CumulativeLast;//最后价格累计的0价格?
uint public price1CumulativeLast;
//紧接最近一次流动性事件之后
uint public kLast;//reserve0*reserve1,as of immediately after the most recent liquidity event
uint private unlocked=1;
//防止递归迭代出现问题,所以要上锁
//一个锁,使用该modifier的函数在unlocked==1时才可以进入,
//第一个调用者进入后,会将unlocked置为0,此使第二个调用者无法再进入
//执行完_部分的代码后,才会再将unlocked置1,重新将锁打开
modifier lock(){
require(unlocked==1,'UniswapV2:LOCKED');
unlocked=0;
_;
unlocked=1;
}
//获取储备:返回:_reserve0,_reserve1,_blockTimestampLast
//用于获取两个token在池子中的数量和最后更新的时间
function getReserves()public view returns(uint112 _reserve0,uint112 _reserve1,uint32 _blockTimestampLast){
_reserve0=reserve0;
_reserve1=reserve1;
//时间戳
_blockTimestampLast=blockTimestampLast;
}
//转账,安全校验
function _safeTransfer(address token,address to,uint value)private{
//调用transfer方法,把地址token中的value个coin转账给to
(bool success,bytes memory data)=token.call(abi.encodeWithSelector(SELECTOR,to,value));
//检查返回值,必须成功否则报错
require(success&&(data.length==0||abi.decode(data,(bool))),'UniswapV2:TRANSFER_FAILED');
}
event Mint(address indexed sender,uint amount0,uint amount1);
event Burn(address indexed sender,uint amount0,uint amount1,address indexed to);
event Swap(address indexed sender,uint amount0In,uint amount1In,uint amount0Out,uint amount1Out,address indexed to);
event Sync(uint112 reserve0,uint112 reserve1);
//部署此合约时将msg.sender设置为factory,后续初始化时会用到这个值
constructor()public{
factory=msg.sender;
}
//called once by the factory at time of deployment
//在UniswapV2Factory.sol的createPair中调用过
function initialize(address _token0,address _token1)external{
require(msg.sender==factory,'UniswapV2:FORBIDDEN');//sufficient check
token0=_token0;
token1=_token1;
}
//update reserves and,on the first call per block,price accumulators
//更新储备,并在每个区块的第一次调用时更新价格累加器
/**
更新变量:
blockTimestampLast
reserve0
reserve1
price0CumulativeLast
price1CumulativeLast
*/
//这个函数是用来更新价格oracle的,计算累计价格
function _update(uint balance0,uint balance1,uint112 _reserve0,uint112 _reserve1)private{
//溢出校验
require(balance0<=uint112(-1)&&balance1<=uint112(-1),'UniswapV2:OVERFLOW');
uint32 blockTimestamp=uint32(block.timestamp%2**32);
uint32 timeElapsed=blockTimestamp-blockTimestampLast;//overflow is desired
//计算时间加权的累计价格,256位中,前112位用来存整数,后112位用来存小数,多的32位用来存溢出的值
if(timeElapsed>0&&_reserve0!=0&&_reserve1!=0){
//*never overflows,and+overflow is desired
price0CumulativeLast+=uint(UQ112x112.encode(_reserve1).uqdiv(_reserve0))*timeElapsed;
price1CumulativeLast+=uint(UQ112x112.encode(_reserve0).uqdiv(_reserve1))*timeElapsed;
}
//更新reserve值
reserve0=uint112(balance0);
reserve1=uint112(balance1);
blockTimestampLast=blockTimestamp;
emit Sync(reserve0,reserve1);
}