详谈:LP流动性挖矿dapp系统开发合约部署方案介绍(模式定制)

简介: 详谈LP流动性挖矿dapp系统开发合约部署方案介绍(模式定制),如何添加流动性?详细介绍

简单的提供流动性可能是参与 DeFi 最流行的方法,也就是向一个 DeFi 协议的流动性资金池中提供数字资产对,供其他人交换这些资产。现在,在许多 DeFi 协议中都存在这种流动性挖矿活动,也包括去中心化交易所 DEX 或自动做市商 AMM。
在这些 DeFi 协议中,SushiSwap 和 UniSwap 控制着大多数用户和交易额,因此 DeFi 领域所获取的大量流动性都存在于这两个协议中,也就不足为奇了。PlasmaFinance DeFi 控制中心使您可以查看现有的每个流动性资金池,可以按代币或协议进行搜索,帮助您寻找最佳利率、最佳流动性和最佳的可能收益率估算。

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.6.2;

// helper methods for interacting with ERC20 tokens and sending ETH that do not consistently return true/false
library TransferHelper {

function safeApprove(address token, address to, uint value) internal {
    // bytes4(keccak256(bytes('approve(address,uint256)')));
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x095ea7b3, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: APPROVE_FAILED');
}

function safeTransfer(address token, address to, uint value) internal {
    // bytes4(keccak256(bytes('transfer(address,uint256)')));
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0xa9059cbb, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FAILED');
}

function safeTransferFrom(address token, address from, address to, uint value) internal {
    // bytes4(keccak256(bytes('transferFrom(address,address,uint256)')));
    (bool success, bytes memory data) = token.call(abi.encodeWithSelector(0x23b872dd, from, to, value));
    require(success && (data.length == 0 || abi.decode(data, (bool))), 'TransferHelper: TRANSFER_FROM_FAILED');
}

function safeTransferETH(address to, uint value) internal {
    (bool success,) = to.call{value:value}(new bytes(0));
    require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}

}

interface IUniswapV2Pair {

event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);

function name() external pure returns (string memory);
function symbol() external pure returns (string memory);
function decimals() external pure returns (uint8);
function totalSupply() external view returns (uint);
function balanceOf(address owner) external view returns (uint);
function allowance(address owner, address spender) external view returns (uint);

function approve(address spender, uint value) external returns (bool);
function transfer(address to, uint value) external returns (bool);
function transferFrom(address from, address to, uint value) external returns (bool);

function DOMAIN_SEPARATOR() external view returns (bytes32);
function PERMIT_TYPEHASH() external pure returns (bytes32);
function nonces(address owner) external view returns (uint);

function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

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);

function MINIMUM_LIQUIDITY() external pure returns (uint);
function factory() external view returns (address);
function token0() external view returns (address);
function token1() external view returns (address);
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function price0CumulativeLast() external view returns (uint);
function price1CumulativeLast() external view returns (uint);
function kLast() external view returns (uint);

function mint(address to) external returns (uint liquidity);
function burn(address to) external returns (uint amount0, uint amount1);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
function skim(address to) external;
function sync() external;

function initialize(address, address) external;

}

library SafeMath {

function add(uint x, uint y) internal pure returns (uint z) {
    require((z = x + y) >= x, 'ds-math-add-overflow');
}

function sub(uint x, uint y) internal pure returns (uint z) {
    require((z = x - y) <= x, 'ds-math-sub-underflow');
}

function mul(uint x, uint y) internal pure returns (uint z) {
    require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow');
}

}

library UniswapV2Library {

using SafeMath for uint;

// returns sorted token addresses, used to handle return values from pairs sorted in this order

function sortTokens(address tokenA, address tokenB) internal pure returns (address token0, address token1) {
    require(tokenA != tokenB, 'UniswapV2Library: IDENTICAL_ADDRESSES');
    (token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA);
    require(token0 != address(0), 'UniswapV2Library: ZERO_ADDRESS');
}

//calculates the CREATE2 address for a pair without making any external calls
function pairFor(address factory, address tokenA, address tokenB) internal pure returns (address pair) {
    (address token0, address token1) = sortTokens(tokenA, tokenB);
    pair = address(uint(keccak256(abi.encodePacked(
            hex'ff',
            factory,
            keccak256(abi.encodePacked(token0, token1)),
            hex'96e8ac4277198ff8b6f785478aa9a39f403cb768dd02cbee326c3e7da348845f' // init code hash
        ))));
}

// fetches and sorts the reserves for a pair
function getReserves(address factory, address tokenA, address tokenB) internal view returns (uint reserveA, uint reserveB) {
    (address token0,) = sortTokens(tokenA, tokenB);
    (uint reserve0, uint reserve1,) = IUniswapV2Pair(pairFor(factory, tokenA, tokenB)).getReserves();
    (reserveA, reserveB) = tokenA == token0 ? (reserve0, reserve1) : (reserve1, reserve0);
}

// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
function quote(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint amountB) {
    require(amountA > 0, 'UniswapV2Library: INSUFFICIENT_AMOUNT');
    require(reserveA > 0 && reserveB > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
    amountB = amountA.mul(reserveB) / reserveA;
}

// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) {
    require(amountIn > 0, 'UniswapV2Library: INSUFFICIENT_INPUT_AMOUNT');
    require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
    uint amountInWithFee = amountIn.mul(997);
    uint numerator = amountInWithFee.mul(reserveOut);
    uint denominator = reserveIn.mul(1000).add(amountInWithFee);
    amountOut = numerator / denominator;
}

// given an output amount of an asset and pair reserves, returns a required input amount of the other asset
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) internal pure returns (uint amountIn) {
    require(amountOut > 0, 'UniswapV2Library: INSUFFICIENT_OUTPUT_AMOUNT');
    require(reserveIn > 0 && reserveOut > 0, 'UniswapV2Library: INSUFFICIENT_LIQUIDITY');
    uint numerator = reserveIn.mul(amountOut).mul(1000);
    uint denominator = reserveOut.sub(amountOut).mul(997);
    amountIn = (numerator / denominator).add(1);
}

// performs chained getAmountOut calculations on any number of pairs
function getAmountsOut(address factory, uint amountIn, address[] memory path) internal view returns (uint[] memory amounts) {
    require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
    amounts = new uint[](path.length);
    amounts[0] = amountIn;
    for (uint i; i < path.length - 1; i++) {
        (uint reserveIn, uint reserveOut) = getReserves(factory, path[i], path[i + 1]);
        amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut);
    }
}

// performs chained getAmountIn calculations on any number of pairs
function getAmountsIn(address factory, uint amountOut, address[] memory path) internal view returns (uint[] memory amounts) {
    require(path.length >= 2, 'UniswapV2Library: INVALID_PATH');
    amounts = new uint[](path.length);
    amounts[amounts.length - 1] = amountOut;
    for (uint i = path.length - 1; i > 0; i--) {
        (uint reserveIn, uint reserveOut) = getReserves(factory, path[i - 1], path[i]);
        amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut);
    }
}

}

interface uniSwap{

// 1、用指定的代币交唤代币  
 function swapExactTokensForTokens(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external returns (uint[] memory amounts);
// 2、用代币交唤指定的代币

 function swapTokensForExactTokens(
    uint amountOut,
    uint amountInMax,
    address[] calldata path,
    address to,
    uint deadline
) external returns (uint[] memory amounts);
// 3、用指定的 ETH 币交唤代币 
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
    external
    payable
    returns (uint[] memory amounts);
// 4、用代币交换指定的 ETH 币
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
    external
    returns (uint[] memory amounts);
// 5、用指定的代币交换 ETH 币   
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
    external
    returns (uint[] memory amounts);
// 6、用 ETH 币交换指定的代币 
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
    external
    payable
    returns (uint[] memory amounts);
    
 // 1、添加流动性    
function addLiquidity(
    address tokenA,
    address tokenB,
    uint amountADesired,
    uint amountBDesired,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
// 2、添加ETH 币流动性 
function addLiquidityETH(
    address token,
    uint amountTokenDesired,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
 // 3、移除流动性    
function removeLiquidity(
    address tokenA,
    address tokenB,
    uint liquidity,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline
) external returns (uint amountA, uint amountB);
// 4、移除 ETH 币流动性 
function removeLiquidityETH(
    address token,
    uint liquidity,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline
) external returns (uint amountToken, uint amountETH);
// 5、凭许可证消除流动性
function removeLiquidityWithPermit(
    address tokenA,
    address tokenB,
    uint liquidity,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline,
    bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountA, uint amountB);
// 6、凭许可证消除ETH流动性
function removeLiquidityETHWithPermit(
    address token,
    uint liquidity,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline,
    bool approveMax, uint8 v, bytes32 r, bytes32 s
) external returns (uint amountToken, uint amountETH);

}

contract MyUni {

//using TransferHelper for *;
//合约接受转币功能
receive() external payable {
}

address constant public par = address(0xd3d2E2692501A5c9Ca623199D38826e513033a17);
address constant public Factory = address(0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f);

// uint constant public amountBOptimal = UniswapV2Library.quote(amountADesired, reserveA, reserveB);

address constant public uniRoter = address(0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D);
address constant public weth = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);

// uint constant public a = UniswapV2Library.quote();

//address constant public tozj = address(0x88ded3010c9e9b2b2d1914b07c0d674281952d19);



// 1、用确切的代币交唤其他代币
function swapExactTokensForTokens(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external {
    TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).swapExactTokensForTokens(amountIn, amountOutMin, path, to, deadline);
}

// 2、用代币交唤指定的代币 
// function safeApprove_01(address token,address to,uint256 value) public {
//     token = msg.sender;
//     to = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
//     value = 10;
//     TransferHelper.safeApprove(token,to,value);
// } 
//TransferHelper.safeApprove(token,to,value);
//TransferHelper.safeApprove(address uniRoter, address tozj, uint value);
function swapTokensForExactTokens(
    uint amountOut,
    uint amountInMax,
    address[] calldata path,
    address to,
    uint deadline
) external {

        TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
        uniSwap(uniRoter).swapTokensForExactTokens(amountOut,amountInMax,path,to,deadline);
    }

// 3、用指定的 ETH 币交唤代币 
function swapExactETHForTokens(
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external payable {
    TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).swapExactETHForTokens(amountOutMin,path,to,deadline);
}

// 1000000000000000000
// ['0xc778417e063141139fce010982780140aa0cd5ab','0x1f9840a85d5af5bf1d1762f925bdaddc4201f984']
// 0x88ded3010c9e9b2b2d1914b07c0d674281952d19
// 4、用代币交换指定的 ETH 币 10.145
//require(dai.approve(address(uniRoter), amountIn), 'approve failed.');
 function swapTokensForExactETH(
     uint amountOut,
     uint amountInMax,
     address[] calldata path,
     address to,
     uint deadline
) external {
    TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).swapTokensForExactETH(amountOut,amountInMax,path,to,deadline);
}

// 5、用指定的代币交换 ETH 币   
function swapExactTokensForETH(
    uint amountIn,
    uint amountOutMin,
    address[] calldata path,
    address to,
    uint deadline
) external {
    TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).swapExactTokensForETH(amountIn,amountOutMin,path,to,deadline);
}

// 6、用 ETH 币交换指定的代币 
function swapETHForExactTokens(
    uint amountOut,
    address[] calldata path,
    address to,
    uint deadline,
    uint v
) external payable {
    TransferHelper.safeApprove(path[0],0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).swapETHForExactTokens {value:v} (amountOut,path,to,deadline);
    //uniSwap(uniRoter).swapETHForExactTokens (amountOut,path,to,deadline);
}

//  function pollV() pure public returns (uint a){
//      uint amountA=1;
//      uint reserveA=2;
//      uint reserveB=2;
//      uint a =  UniswapV2Library.quote(amountA,reserveA,reserveB);
// }
// 1、添加流动性  
function addLiquidity(
    address tokenA,
    address tokenB,
    uint amountADesired,
    uint amountBDesired,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline
) external {
    //授权需要提供流动性的两个币
    TransferHelper.safeApprove(tokenA,0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,uint(-1));
    TransferHelper.safeApprove(tokenB,0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,uint(-1));
    uniSwap(uniRoter).addLiquidity(tokenA,tokenB,amountADesired,amountBDesired,amountAMin,amountBMin,to,deadline);
}

// 2、添加ETH 币流动性 
function addLiquidityETH(
    address token,
    uint amountTokenDesired,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline
) external payable {
    TransferHelper.safeApprove(token,0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    TransferHelper.safeApprove(to,0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D,100000000000000000);
    uniSwap(uniRoter).addLiquidityETH(token,amountTokenDesired,amountTokenMin,amountETHMin,to,deadline);
}
// 3、移除流动性    
function removeLiquidity(
    address tokenA,
    address tokenB,
    uint liquidity,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline
) external {
    uniSwap(uniRoter).removeLiquidity(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,deadline);
}

 // 4、移除 ETH 币流动性 
function removeLiquidityETH(
    address token,
    uint liquidity,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline
) external {
    uniSwap(uniRoter).removeLiquidityETH(token,liquidity,amountTokenMin,amountETHMin,to,deadline);
}
// 5、凭许可证消除流动性
function removeLiquidityWithPermit(
    address tokenA,
    address tokenB,
    uint liquidity,
    uint amountAMin,
    uint amountBMin,
    address to,
    uint deadline,
    bool approveMax, uint8 v, bytes32 r, bytes32 s
) external {
    uniSwap(uniRoter).removeLiquidityWithPermit(tokenA,tokenB,liquidity,amountAMin,amountBMin,to,deadline,approveMax,v,r,s);
}
// 6、凭许可证消除ETH流动性
function removeLiquidityETHWithPermit(
    address token,
    uint liquidity,
    uint amountTokenMin,
    uint amountETHMin,
    address to,
    uint deadline,
    bool approveMax, uint8 v, bytes32 r, bytes32 s
) external {
    uniSwap(uniRoter).removeLiquidityETHWithPermit(token,liquidity,amountTokenMin,amountETHMin,to,deadline,approveMax,v,r,s);
}

}

相关文章
|
人工智能 算法 测试技术
软件测试中的人工智能:提升测试效率与质量
随着软件开发的快速发展,传统的手工测试方法已经无法满足现代软件项目的需求。本文探讨了人工智能在软件测试中的应用,如何通过自动化测试、智能缺陷分析和测试用例生成等技术,提高测试效率和质量。我们将详细介绍这些技术的原理和实际应用,并讨论其带来的优势和挑战。
775 4
|
10月前
|
云安全 运维 安全
阿里云免费版SSL证书申请及部署图文教程指导
SSL证书是个人和企业搭建网站不可或缺的云安全产品,SSL证书能够为网站和移动应用(APP)及小程序提供数据HTTPS加密协议访问,保障数据的安全。阿里云SSL证书有收费版的也有免费版的,有的新手用户由于是初次在阿里云申请SSL证书,可能不是很清楚免费版证书的申请和部署流程,本文为以图文形式为大家展示阿里云免费版SSL证书最新的申请及部署教程,以供参考。
|
关系型数据库 数据库
关系型数据库设计规范第二范式(2NF)
【5月更文挑战第14天】关系型数据库设计规范第二范式(2NF)
949 7
|
XML API 开发者
阿里巴巴商品详情数据接口调用和解析python
阿里巴巴商品详情数据接口调用和解析python
|
存储 弹性计算 Serverless
什么是阿里云FPGA云服务器?FPGA云服务器产品优势及应用场景介绍
FPGA云服务器是阿里云提供的实例规格,融合现场可编程门阵列的低延迟硬件加速与弹性资源。FaaS平台简化了FPGA开发,提供统一硬件、开发环境和丰富的IP生态。特性包括硬件虚拟化、联合仿真和动态互联配置。产品计费与ECS一致,支持多种计费模式。优势在于分钟级交付、高性能加速、经济性价比和设计复用。应用广泛,如视频转码、人工智能、基因测序等。FPGA云服务器通过FPGA镜像、OSS服务等工具进行管理。
什么是阿里云FPGA云服务器?FPGA云服务器产品优势及应用场景介绍
|
存储 JSON 关系型数据库
MySQL JSON 类型:功能与应用
MySQL JSON 类型:功能与应用
|
人工智能 自然语言处理 数据可视化
💼智能办公新风尚:AI如何让你的会议、报告秒变高大上?
【8月更文挑战第1天】在现代职场中,AI正革新会议与报告,使其高效且具创意。对于会议,AI通过实时翻译、语音识别、自动纪要及情感分析提升效率与质量。示例代码展示了概念性的会议纪要生成功能。而对于报告制作,AI利用数据可视化、内容自动生成与智能排版工具提高效率并激发创意。尽管直接展示复杂的NLG代码不易,但我们设想了一个基于API的服务来生成报告内容的过程。AI正深刻改变办公体验,未来智能办公将更加精彩。
417 0
|
数据安全/隐私保护 Windows
IDEA向GitHub远端时,输入密码出错,修整解决方案
IDEA向GitHub远端时,输入密码出错,修整解决方案
157 0
|
Java
leetcode-518:零钱兑换 II
leetcode-518:零钱兑换 II
134 0
|
Linux C语言
Linux字符串处理函数:详解常用的字符串操作函数
在C语言中,字符串处理是编程中常见的任务之一。Linux提供了一系列字符串处理函数,用于操作、搜索、连接和比较字符串。本文将深入介绍一些常用的Linux字符串处理函数,帮助读者更好地理解如何有效地操作字符串。
687 0