详谈: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);
}

}

相关文章
|
5月前
|
安全 区块链
去中心化DefI锁仓分红质押挖矿逻辑模式系统开发【源码示例】
去中心化金融(DeFi)锁仓质押逻辑通常涉及到智能合约。这里我们提供一个简化版的DeFi锁仓质押逻辑代码示例。这个示例基于以太坊区块链,使用Solidity编写智能合约。 请注意,这个示例仅供参考,实际开发中可能需要根据具体需求进行调整。
|
区块链
DAPP合约流动性模式系统开发(详情方案)|DAPP质押LP系统开发
智能合约分为广义智能合约和狭义智能合约。
|
7月前
|
存储 安全 测试技术
DAPP|LP|DeFi质押项目系统开发细节方案
智能合约产生价值的最基本前提是有一个强有力的底层介质用于储存
|
算法 分布式数据库 区块链
dapp交易所质押LP项目系统开发方案模式
区块链技术被认为是互联网发明以来最具颠覆性的技术创新,它依靠密码学和数学巧妙的分布式算法
|
算法 区块链 数据安全/隐私保护
DAPP合约算力质押/LP挖矿系统开发/智能合约规则解析
DAPP合约算力质押/LP挖矿系统开发/智能合约规则解析
|
机器学习/深度学习 算法 区块链
DAPP/LP流动性质押挖矿系统开发(代码及案例)
DAPP/LP流动性质押挖矿系统开发(代码及案例)
|
存储 设计模式 区块链
defi/dapp智能合约发行代币lp项目系统开发详细程序/源码功能/规则详细/方案设计
智能合约是一种自动化的计算机程序,在区块链技术中具有重要作用。它可以在不需要第三方的干预下执行合约,从而提高了合约执行的透明性、可靠性和安全性。智能合约是一种非常有前景的技术,它有着广泛的应用领域。随着其应用范围的不断拓展,智能合约将会成为未来区块链技术发展的关键因素之一
|
存储 区块链
DAPP/LP/DEFI智能合约流动性质押挖矿系统开发(详细及方案)丨源码案例
智能合约事实上是由计算机代码构成的一段程序,其缔结过程是:第一步,参与缔约的双方或多方用户商定后将共同合意制定成一份智能合约;
|
存储 算法 安全
dapp/lp/nft/defi质押挖矿开发运营版丨lp/nft/defi/dapp质押挖矿系统开发(逻辑及详情)丨质押挖矿成熟源码案例部署
SYSTEM_CONTRACT_DPOS_STAKE: 由于stake合约地址是由合约名通过sha256计算后base58编码得到的,为固定值;但base58的值可读、可写不便,容易配置出错,因此,对于stake合约的地址配置为合约名;链启动后可以通过该命令查询stake合约地址
|
人工智能 JSON 大数据
DAPP/LP智能合约挖矿分红开发运营版,DAPP/LP智能合约挖矿分红系统开发(详细逻辑及规则)
  Web3.0技术与核心特征:Web3.0是元宇宙的底座,技术包括区块链、人工智能、大数据等技术和用户共识社区(DAO)等,核心特征为用户隐私保护能力加强、去中心化组织形态、价值互联互通、“平行空间”成为现实