UniswapV2应用案例分析-beefyUniswapZap.sol

// SPDX-License-Identifier: GPLv2

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.

// @author Wivern for Beefy.Finance
// @notice This contract adds liquidity to Uniswap V2 compatible liquidity pair pools and stake.

pragma solidity >=0.6.2;
//导入Pair接口
import '@uniswap/v2-core/contracts/interfaces/IUniswapV2Pair.sol';
//导入一个平方根计算工具
import '@uniswap/lib/contracts/libraries/Babylonian.sol';

//导入Router02接口 
import 'https://github.com/Uniswap/uniswap-v2-periphery/blob/master/contracts/interfaces/IUniswapV2Router02.sol';

//导入SafeERC20
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v3.3.0/contracts/token/ERC20/SafeERC20.sol';
//导入LowGasSafeMath
import 'https://github.com/Uniswap/uniswap-v3-core/blob/main/contracts/libraries/LowGasSafeMath.sol';

//定义一个WETH池的接口
interface IWETH is IERC20 {
    function deposit() external payable;
    function withdraw(uint256 wad) external;
}

//定义一个Beefu池的接口
interface IBeefyVaultV6 is IERC20 {
    function deposit(uint256 amount) external;
    function withdraw(uint256 shares) external;
    function want() external pure returns (address);
}

contract BeefyUniV2Zap {
    using LowGasSafeMath for uint256;
    using SafeERC20 for IERC20;
    using SafeERC20 for IBeefyVaultV6;

    //提供一个不可变的router
    IUniswapV2Router02 public immutable router;
    //提供一个不可变的WETH池地址
    address public immutable WETH;
    //提供一个不可变的最小交易量
    uint256 public constant minimumAmount = 1000;

    //部署合约时需提供一个不可更改的router地址、一个不可更改的WETH池地址
    constructor(address _router, address _WETH) {
        router = IUniswapV2Router02(_router);
        WETH = _WETH;
    }

    //当外界与本合约进行交互,但并未调用函数或传递数据时,receive()被触发,
    //使得本合约可以接受外界转账,但必须保证转账人==WETH池,
    //即不接收非WETH池以外的对象的莫名其妙的转账
    receive() external payable {
        assert(msg.sender == WETH);
    }

    //用户调用该函数,注入ETH,提交其要访问的beefy池地址、最终兑换到的流动性代币的最小量(后台自动为用户提供)
    function beefInETH (address beefyVault, uint256 tokenAmountOutMin) external payable {
        //保证用户注入的ETH>=最小交易量
        require(msg.value >= minimumAmount, 'Beefy: Insignificant input amount');

        //将ETH注入WETH池,兑换WETH
        IWETH(WETH).deposit{value: msg.value}();

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值