区块链是一种将数据区块按照时间顺序组合成的链式结构,是去中心化系统中各节点共享且共同维护的分布式数据账本,具体的:各节点由P2P组网方式相互连通和交互,受激励机制激励贡献自身算力,
区块链技术可以构建一个高效可靠的价值传输系统,推动互联网成为构建社会信任的网络基础设施,实现价值的有效传递,并将此称为价值互联网。区块链提供了一种新型的社会信任机制,为数字经济的发展奠定了新基石,
什么是智能合约DApp。智能合约DApp是使用区块链技术实现去中心化应用(DApp)的核心技术。所谓智能合约是指以数字代码的形式编写的自动执行计算机程序,A smart contract system that automatically executes and manages the rights and interests of all parties that collaborate with each other.
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