UniswapV2Pair.sol

//参考自: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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值