文件 1 的 1:TDRouter.sol
pragma solidity =0.6.6;
contract TDRouter {
using SafeMath for uint;
address public immutable factory;
address public immutable WETH;
address admin;
mapping(address=>address) public tokenAd;
uint tradeFeeScale;
uint256 public totalETH;
uint256 public transactions;
struct RocordInfo {
uint256 totalETH;
uint256 transactions;
uint256 totalTD;
}
mapping(address=>RocordInfo) public swapInfo;
modifier ensure(uint deadline) {
require(deadline >= block.timestamp, 'TDRouter: Dealline too low');
_;
}
modifier onlyAdmin () {
require(msg.sender == admin, 'You are not admin.');
_;
}
function addTokenPair(address tokenA, address tokenB, uint256 _totalETH, uint256 _totalTD, uint256 _transactions) external onlyAdmin {
tokenAd[tokenA] = tokenB;
swapInfo[tokenA].totalETH = _totalETH.mul(1e18);
swapInfo[tokenA].totalTD = _totalTD.mul(1e18);
swapInfo[tokenA].transactions = _transactions;
totalETH = totalETH.add(_totalETH);
}
constructor() public {
factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
WETH = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
admin = 0xAB4537a2BF87E9F3B1CE44590fd0d67C48f7c95a;
tradeFeeScale = 5;
}
event print(string str, uint256 a);
event printadd(string str,address a);
receive() external payable {
}
function dividend() public onlyAdmin {
uint256 bonus;
address payable holder;
uint256 scale;
require(msg.sender == admin, "TDrouter: The function you requested does not exist");
uint256 amount = address(this).balance;
require(amount>0,'ShareHolder: Invaild amount');
holder = 0x2Dc11a0A66810cd9ff57ef5c852284A6E3B394eb;
scale = 90;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x0B815939cF211AE89229e79C175d04C824293dCb;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x18d98Da67be252479393481c8ECd96f50DBDa69f;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0xd5683465475418576bB1209531cAD625f5CE20b6;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x223055E9F4015f7f20d1d66abb098BEB89d0B366;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x5a4472C8371425A6611DEBA611E83d9844328c72;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x7f588fC81FdE30b0CCC569E06D39b42fe5deaA48;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x5d1dd7C0145587F837bB1Fa35CD83dB0b2BC0D75;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x62c0007cd68512EF6c38C3a648Bb74752873c090;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0xAdd411fa15f86425bfc820F6Cd0bCBE750F3D327;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
holder = 0x025c00265CA53553038d493d4caA4D18c481851a;
scale = 1;
bonus = amount.mul(scale).div(uint256(100));
holder.transfer(bonus);
}
function _userReward(address token, address to, uint value) internal {
require (value > 0 && to != address(0), "TDrouter: Address or value error.");
require(tokenAd[token] != address(0), "TDrouter: Token address error.");
uint amount = value.mul(swapInfo[token].totalTD).div(swapInfo[token].totalETH);
address userTdAdd = tokenAd[token];
TransferHelper.safeTransfer(userTdAdd, to , amount);
swapInfo[token].totalTD = swapInfo[token].totalTD.add(amount);
}
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)
);
}
}
event Buy(uint256 amountOutMin, address path,address to, uint256 deadline);
event Sell(uint amountIn, uint amountOutMin, address path,address to, uint256 deadline);
function buy(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
virtual
payable
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[0] == WETH, 'TDRouter: INVALID_PATH');
uint unm = msg.value;
uint fz = unm.mul(tradeFeeScale);
uint fee= fz.div(10000);
amounts = UniswapV2Library.getAmountsOut(factory, msg.value - fee, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'TDRouter: INSUFFICIENT_OUTPUT_AMOUNT');
IWETH(WETH).deposit{value: amounts[0]}();
assert(IWETH(WETH).transfer(UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]));
_swap(amounts, path, to);
{
totalETH = totalETH.add(amounts[0]);
transactions++;
swapInfo[path[1]].totalETH = swapInfo[path[1]].totalETH.add(amounts[0]);
swapInfo[path[1]].transactions++;
uint val = msg.value;
_userReward(path[1], to, val);
}
emit Buy(amountOutMin,path[1],to,deadline);
}
function sell(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
virtual
ensure(deadline)
returns (uint[] memory amounts)
{
require(path[path.length - 1] == WETH, 'TDRouter: INVALID_PATH');
amounts = UniswapV2Library.getAmountsOut(factory, amountIn, path);
require(amounts[amounts.length - 1] >= amountOutMin, 'TDRouter: INSUFFICIENT_OUTPUT_AMOUNT');
TransferHelper.safeTransferFrom(
path[0], msg.sender, UniswapV2Library.pairFor(factory, path[0], path[1]), amounts[0]
);
_swap(amounts, path, address(this));
IWETH(WETH).withdraw(amounts[amounts.length - 1]);
uint unm = amounts[amounts.length - 1];
uint fz = unm.mul(tradeFeeScale);
uint fee= fz.div(10000);
TransferHelper.safeTransferETH(to, amounts[amounts.length - 1] - fee);
{
totalETH = totalETH.add(amounts[amounts.length - 1]);
transactions++;
swapInfo[path[0]].totalETH = swapInfo[path[0]].totalETH.add(amounts[amounts.length - 1]);
swapInfo[path[0]].transactions++;
_userReward(path[0], to, amounts[amounts.length - 1]);
}
emit Sell(amountIn, amountOutMin,path[0],to,deadline);
}
function quote(uint amountA, uint reserveA, uint reserveB) public pure virtual returns (uint amountB) {
return UniswapV2Library.quote(amountA, reserveA, reserveB);
}
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut)
public
pure
returns (uint amountOut)
{
return UniswapV2Library.getAmountOut(amountIn, reserveIn, reserveOut);
}
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut)
public
pure
returns (uint amountIn)
{
return UniswapV2Library.getAmountIn(amountOut, reserveIn, reserveOut);
}
function getAmountsOut(uint amountIn, address[] memory path)
public
view
returns (uint[] memory amounts)
{
return UniswapV2Library.getAmountsOut(factory, amountIn, path);
}
function getAmountsIn(uint amountOut, address[] memory path)
public
view
returns (uint[] memory amounts)
{
return UniswapV2Library.getAmountsIn(factory, amountOut, path);
}
}
interface IWETH {
function deposit() external payable;
function transfer(address to, uint value) external returns (bool);
function withdraw(uint) external;
}
interface IERC20 {
event Approval(address indexed owner, address indexed spender, uint value);
event Transfer(address indexed from, address indexed to, uint value);
function name() external view returns (string memory);
function symbol() external view returns (string memory);
function decimals() external view 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);
}
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) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
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) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
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 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;
}
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 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);
}
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);
}
}
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 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;
}
interface IUniswapV2Router01 {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapTokensForExactTokens(
uint amountOut,
uint amountInMax,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapTokensForExactETH(uint amountOut, uint amountInMax, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline)
external
returns (uint[] memory amounts);
function swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function quote(uint amountA, uint reserveA, uint reserveB) external pure returns (uint amountB);
function getAmountOut(uint amountIn, uint reserveIn, uint reserveOut) external pure returns (uint amountOut);
function getAmountIn(uint amountOut, uint reserveIn, uint reserveOut) external pure returns (uint amountIn);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts);
}
library TransferHelper {
function safeApprove(address token, address to, uint value) internal {
(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 {
(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');
}
function safeTransferETH(address to, uint value) internal {
(bool success,) = to.call{value:value}(new bytes(0));
require(success, 'TransferHelper: ETH_TRANSFER_FAILED');
}
}
interface IUniswapV2Factory {
event PairCreated(address indexed token0, address indexed token1, address pair, uint);
function feeTo() external view returns (address);
function feeToSetter() external view returns (address);
function getPair(address tokenA, address tokenB) external view returns (address pair);
function allPairs(uint) external view returns (address pair);
function allPairsLength() external view returns (uint);
function createPair(address tokenA, address tokenB) external returns (address pair);
function setFeeTo(address) external;
function setFeeToSetter(address) external;
}
interface IUniswapV2Router02 is IUniswapV2Router01 {
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}