文件 1 的 1:RouterConfigurableSell-28.03.sol
pragma solidity ^0.7.2;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath: subtraction overflow");
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath: division by zero");
uint256 c = a / b;
return c;
}
}
interface IWETH {
function balanceOf(address) external view returns (uint);
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}
interface IUniswapV2Factory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
}
interface IUniswapV2Pair {
function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
}
interface IRouter{
function WETH() external view returns (address);
}
interface IFreeFromUpTo {
function freeFromUpTo(address from, uint256 value) external returns (uint256 freed);
}
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
library UniswapV2Library {
using SafeMath for uint;
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');
}
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'
))));
}
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);
}
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;
}
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);
}
}
}
library TransferHelper {
function safeTransfer(address token, address to, uint value) internal {
(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 {
(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');
}
}
contract Owned {
address public owner;
constructor(address _owner) {
owner = _owner;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
}
abstract contract Whitelist is Owned {
event MemberAdded(address member);
event MemberRemoved(address member);
mapping (address => bool) public members;
address[] whitelisted;
modifier onlyWhitelisted()
{
require(isMember(msg.sender));
_;
}
function isMember(address _member)
public
view
returns(bool)
{
return members[_member];
}
function getAllMember()
external view
onlyOwner
returns (address[] memory )
{
return whitelisted;
}
function addMember(address _member)
public
onlyOwner
{
require(
!isMember(_member),
"Address is member already."
);
members[_member] = true;
whitelisted.push(_member);
emit MemberAdded(_member);
}
function removeMember(address _member)
public
onlyOwner
{
require(
isMember(_member),
"Not member of whitelist."
);
for(uint i = 0; i<whitelisted.length; i++){
if(_member == whitelisted[i]){
delete whitelisted[i];
break;
}
}
delete members[_member];
emit MemberRemoved(_member);
}
}
contract Router is IRouter, Whitelist{
address public immutable override WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
IFreeFromUpTo public constant chi = IFreeFromUpTo(0x0000000000004946c0e9F43F4Dee607b0eF1fA1c);
modifier ensure(uint deadline) {
require(deadline >= block.timestamp, 'Router: EXPIRED');
_;
}
modifier discountCHI {
uint256 gasStart = gasleft();
_;
uint256 gasSpent = 21000 + gasStart - gasleft() + 16 * msg.data.length;
chi.freeFromUpTo(address(this), (gasSpent + 14154) / 41947);
}
constructor() Owned(msg.sender) {
}
function _swap(uint[] memory amounts, address[] memory path, address _to) internal virtual {
for (uint i; i < path.length - 1; i++) {
(address input, address output) = (path[i], path[i + 1]);
(address token0,) = UniswapV2Library.sortTokens(input, output);
uint amountOut = amounts[i + 1];
(uint amount0Out, uint amount1Out) = input == token0 ? (uint(0), amountOut) : (amountOut, uint(0));
address to = i < path.length - 2 ? UniswapV2Library.pairFor(factory, output, path[i + 2]) : _to;
IUniswapV2Pair(UniswapV2Library.pairFor(factory, input, output)).swap(
amount0Out, amount1Out, to, new bytes(0)
);
}
}
function swapExactETHForTokens(uint amountIn, uint amountOutMin, address[] calldata path, uint deadline)
external
ensure(deadline)
discountCHI
onlyWhitelisted returns (uint[] memory amounts){
require(path[0] == WETH, 'Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'Router: INSUFFICIENT_OUTPUT_AMOUNT');
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, address(this));
}
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, uint deadline)
external
ensure(deadline)
discountCHI
onlyWhitelisted returns (uint[] memory amounts){
require(path[path.length - 1] == WETH, 'Router: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'Router: INSUFFICIENT_OUTPUT_AMOUNT');
TransferHelper.safeTransfer(
path[0], UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]
);
_swap(amounts, path, address(this));
}
function swapETsFETH(address[] calldata path, uint deadline)
external
ensure(deadline)
discountCHI
onlyWhitelisted returns (uint[] memory amounts){
require(path[path.length - 1] == WETH, 'Router: INVALID_PATH');
uint amountIn = IERC20(path[0]).balanceOf(address(this));
amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] > 0, 'Router: INSUFFICIENT_OUTPUT_AMOUNT');
TransferHelper.safeTransfer(
path[0], UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]
);
_swap(amounts, path, address(this));
}
function calculatePercentage( uint theNumber, uint bp) public pure returns (uint) {
return theNumber * bp / 10000;
}
function swapETsFETHConfigurableAmount(address[] calldata path, uint bp, uint deadline)
external
ensure(deadline)
discountCHI
onlyWhitelisted returns (uint[] memory amounts){
require(path[path.length - 1] == WETH, 'Router: INVALID_PATH');
uint amountIn = IERC20(path[0]).balanceOf(address(this));
uint inputAmount = calculatePercentage(amountIn, bp);
amounts = UniswapV2Library.getAmountsOut(factory, inputAmount, path);
require(amounts[amounts.length - 1] > 0, 'Router: INSUFFICIENT_OUTPUT_AMOUNT');
TransferHelper.safeTransfer(
path[0], UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]
);
_swap(amounts, path, address(this));
}
function depositWETH() external payable{
IWETH(WETH).deposit{value: msg.value}();
}
receive() payable external {}
function transferWETH(address payable to, uint amountIn) external onlyOwner returns (bool){
uint wethBalance = IWETH(WETH).balanceOf(address(this));
require(wethBalance > 0 , 'Router: INSUFFICIENT_BALANCE');
require(wethBalance >= amountIn, 'Router: INSUFFICIENT_BALANCE');
if (wethBalance > 0 && wethBalance >= amountIn){
IWETH(WETH).withdraw(amountIn);
(bool success,) = to.call{value: amountIn}('');
require(success, 'WITHDRAW_TO_CALL_FAILED');
}
return true;
}
function balanceOfWETH() external view onlyOwner returns (uint){
uint wethBalance = IWETH(WETH).balanceOf(address(this));
return wethBalance;
}
function checkErc20TokenBalance(address token) external view onlyWhitelisted returns(uint) {
IERC20 erc20Token = IERC20(token);
return erc20Token.balanceOf(address(this));
}
function transferErc20Token(address token, address to) external onlyOwner returns (bool){
IERC20 erc20Token = IERC20(token);
uint tokenBalance = erc20Token.balanceOf(address(this));
require(tokenBalance > 0 , 'Router: INSUFFICIENT_BALANCE');
if (tokenBalance > 0){
bool success= erc20Token.transfer(to, tokenBalance);
require(success, 'WITHDRAW_TO_CALL_FAILED');
}
return true;
}
}