|
|
|
|
|
|
|
|
|
pragma solidity >=0.6.2;
|
|
|
|
interface IUniswapV2Router01 {
|
|
function factory() external pure returns (address);
|
|
function WETH() external pure returns (address);
|
|
|
|
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);
|
|
function addLiquidityETH(
|
|
address token,
|
|
uint amountTokenDesired,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
|
|
function removeLiquidity(
|
|
address tokenA,
|
|
address tokenB,
|
|
uint liquidity,
|
|
uint amountAMin,
|
|
uint amountBMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountA, uint amountB);
|
|
function removeLiquidityETH(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountToken, uint amountETH);
|
|
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);
|
|
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);
|
|
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);
|
|
}
|
|
|
|
|
|
|
|
pragma solidity >=0.6.2;
|
|
|
|
|
|
interface IUniswapV2Router02 is IUniswapV2Router01 {
|
|
function removeLiquidityETHSupportingFeeOnTransferTokens(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline
|
|
) external returns (uint amountETH);
|
|
function removeLiquidityETHWithPermitSupportingFeeOnTransferTokens(
|
|
address token,
|
|
uint liquidity,
|
|
uint amountTokenMin,
|
|
uint amountETHMin,
|
|
address to,
|
|
uint deadline,
|
|
bool approveMax, uint8 v, bytes32 r, bytes32 s
|
|
) external returns (uint amountETH);
|
|
|
|
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;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
interface IERC20 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
|
|
|
|
|
|
|
|
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
|
|
|
|
|
|
|
|
function totalSupply() external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transfer(address to, 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 from,
|
|
address to,
|
|
uint256 amount
|
|
) external returns (bool);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Context {
|
|
function _msgSender() internal view virtual returns (address) {
|
|
return msg.sender;
|
|
}
|
|
|
|
function _msgData() internal view virtual returns (bytes calldata) {
|
|
return msg.data;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract Ownable is Context {
|
|
address private _owner;
|
|
|
|
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
_transferOwnership(_msgSender());
|
|
}
|
|
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
_checkOwner();
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function owner() public view virtual returns (address) {
|
|
return _owner;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _checkOwner() internal view virtual {
|
|
require(owner() == _msgSender(), "Ownable: caller is not the owner");
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function renounceOwnership() public virtual onlyOwner {
|
|
_transferOwnership(address(0));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function transferOwnership(address newOwner) public virtual onlyOwner {
|
|
require(newOwner != address(0), "Ownable: new owner is the zero address");
|
|
_transferOwnership(newOwner);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function _transferOwnership(address newOwner) internal virtual {
|
|
address oldOwner = _owner;
|
|
_owner = newOwner;
|
|
emit OwnershipTransferred(oldOwner, newOwner);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
pragma solidity 0.8.17;
|
|
|
|
|
|
|
|
|
|
contract Router is Ownable {
|
|
address public feeRecipient;
|
|
address public uniswapRouterV2Address;
|
|
|
|
event FeeRecipientChanged(address indexed feeRecipient);
|
|
event UniRouterAddressChanged(address indexed uniRouterAddress);
|
|
event RouterSwap(address indexed swapper, address indexed feeRecipient, uint256 fee);
|
|
|
|
constructor(address initialFeeRecipient, address initialUniswapRouterV2Address)
|
|
noZeroAddress(initialFeeRecipient)
|
|
noZeroAddress(initialUniswapRouterV2Address)
|
|
{
|
|
feeRecipient = initialFeeRecipient;
|
|
uniswapRouterV2Address = initialUniswapRouterV2Address;
|
|
}
|
|
|
|
modifier noZeroAddress(address addr) {
|
|
require(addr != address(0), "No zero address");
|
|
_;
|
|
}
|
|
|
|
function changeFeeRecipient(address newFeeRecipient) external onlyOwner noZeroAddress(newFeeRecipient) {
|
|
feeRecipient = newFeeRecipient;
|
|
emit FeeRecipientChanged(newFeeRecipient);
|
|
}
|
|
|
|
function changeUniswapV2RouterAddress(address newUniswapV2RouterAddress)
|
|
external
|
|
onlyOwner
|
|
noZeroAddress(newUniswapV2RouterAddress)
|
|
{
|
|
uniswapRouterV2Address = newUniswapV2RouterAddress;
|
|
emit UniRouterAddressChanged(newUniswapV2RouterAddress);
|
|
}
|
|
|
|
function swapExactTokensForTokens(
|
|
uint256 amountIn,
|
|
uint256 amountOutMin,
|
|
address[] memory path,
|
|
address to,
|
|
uint256 deadline,
|
|
uint256 fee
|
|
) external payable returns (uint256[] memory amounts) {
|
|
IERC20 token = IERC20(path[0]);
|
|
|
|
token.transferFrom(msg.sender, address(this), amountIn + fee);
|
|
_checkAllowance(token, amountIn);
|
|
|
|
amounts = IUniswapV2Router02(uniswapRouterV2Address).swapExactTokensForTokens(
|
|
amountIn,
|
|
amountOutMin,
|
|
path,
|
|
to,
|
|
deadline
|
|
);
|
|
|
|
bool success = token.transfer(feeRecipient, fee);
|
|
require(success, "Transfer failed");
|
|
|
|
emit RouterSwap(msg.sender, feeRecipient, fee);
|
|
}
|
|
|
|
function swapExactTokensForETH(
|
|
uint256 amountIn,
|
|
uint256 amountOutMin,
|
|
address[] memory path,
|
|
address to,
|
|
uint256 deadline,
|
|
uint256 fee
|
|
) external payable returns (uint256[] memory amounts) {
|
|
IERC20 token = IERC20(path[0]);
|
|
|
|
token.transferFrom(msg.sender, address(this), amountIn + fee);
|
|
_checkAllowance(token, amountIn);
|
|
|
|
amounts = IUniswapV2Router02(uniswapRouterV2Address).swapExactTokensForETH(
|
|
amountIn,
|
|
amountOutMin,
|
|
path,
|
|
to,
|
|
deadline
|
|
);
|
|
|
|
bool success = token.transfer(feeRecipient, fee);
|
|
require(success, "Transfer failed");
|
|
|
|
emit RouterSwap(msg.sender, feeRecipient, fee);
|
|
}
|
|
|
|
function swapExactETHForTokens(
|
|
uint256 amountOutMin,
|
|
address[] memory path,
|
|
address to,
|
|
uint256 deadline,
|
|
uint256 fee
|
|
) external payable returns (uint256[] memory amounts) {
|
|
amounts = IUniswapV2Router02(uniswapRouterV2Address).swapExactETHForTokens{ value: msg.value - fee }(
|
|
amountOutMin,
|
|
path,
|
|
to,
|
|
deadline
|
|
);
|
|
|
|
|
|
(bool success, ) = feeRecipient.call{ value: fee }("");
|
|
require(success, "Transfer failed");
|
|
|
|
emit RouterSwap(msg.sender, feeRecipient, fee);
|
|
}
|
|
|
|
function _checkAllowance(IERC20 inputToken, uint256 amount) private {
|
|
if (inputToken.allowance(address(this), uniswapRouterV2Address) < amount) {
|
|
inputToken.approve(uniswapRouterV2Address, type(uint256).max);
|
|
}
|
|
}
|
|
} |