简单的提供流动性可能是参与 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);
}
}