//参考自:https://blog.csdn.net/weixin_39430411/article/details/108965855
//指定版本
pragma solidity =0.5.16;
//导入Pair接口
import './interfaces/IUniswapV2Pair.sol';
//导入V2ERC20合约
import './UniswapV2ERC20.sol';
//导入一个数学库,包含一个min()函数,一个sqrt()函数
import './libraries/Math.sol';
//?
import './libraries/UQ112x112.sol';
//导入IERC20合约
import './interfaces/IERC20.sol';
//导入Factory,?
import './interfaces/IUniswapV2Factory.sol';
//?
import './interfaces/IUniswapV2Callee.sol';
//?
contract UniswapV2Pair is IUniswapV2Pair, UniswapV2ERC20 {
using SafeMath for uint;
using UQ112x112 for uint224;
//设定最小流动性,用于燃烧
uint public constant MINIMUM_LIQUIDITY = 10**3;
//一个选择器,不懂
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
uint112 private reserve1; // uses single storage slot, accessible via getReserves
//记录交易时的区块链时间
uint32 private blockTimestampLast; // uses single storage slot, accessible via getReserves
//记录价格最新的累计值
uint public price0CumulativeLast;
uint public price1CumulativeLast;
//记录token0和token1储备量的乘积,在最新的流动性事件发生之后记录
uint public kLast; // reserve0 * reserve1, as of immediately after the most recent liquidity event
//防重入攻击的设计
uint private unlocked = 1;
modifier lock() {
require(unlocked == 1, 'UniswapV2: LOCKED');
unlocked = 0;
_;
unlocked = 1;
}
//获得交易对的储备量,返回值包含了两种代币的储备量、交易时区块的时间
function getReserves() public view returns (uint112 _reserve0, uint112 _reserve1, uint32 _blockTimestampLast) {
_reserve0 = reserve0;
_reserve1 = reserve1;
_blockTimestampLast = blockTimestampLast;
}
//一个safe版的transfer,实现函数调用者将token币转给to用户的功能,转账数量为value
function _safeTransfer(address token, address to, uint value) private {
//对已编码的transfer函数、选择器、转账目标的地址、转账数量进行call(),call成功返回true,其余的目前还不懂
//总之是一个safe版的transfer
(bool success, bytes memory data) = token.call(abi.encodeWithSelector(SELECTOR, to, value));
require(success && (data.length == 0 || abi.decode(data, (bool))), 'UniswapV2: TRANSFER_FAILED');
}
//挖矿事件:sender挖了两种币?
event Mint(address indexed sender, uint amount0, uint amount1);
//烧币事件:sender烧了to两种币?
event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
//交换事件:sender注入和取出两种币的数量,取出到to?
event Swap(
address indexed sender,
uint amount0In,
uint amount1In,
uint amount0Out,
uint amount1Out,
address indexed to
);
//同步事件:更新了两种币的储备量
event Sync(uint
UniswapV2Pair.sol
最新推荐文章于 2022-05-21 00:55:55 发布