文件 1 的 1:Sale.sol
pragma solidity ^0.8.0;
library SafeMath {
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
uint256 c = a + b;
if (c < a) return (false, 0);
return (true, c);
}
}
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b > a) return (false, 0);
return (true, a - b);
}
}
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (a == 0) return (true, 0);
uint256 c = a * b;
if (c / a != b) return (false, 0);
return (true, c);
}
}
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a / b);
}
}
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
unchecked {
if (b == 0) return (false, 0);
return (true, a % b);
}
}
function add(uint256 a, uint256 b) internal pure returns (uint256) {
return a + b;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
return a * b;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return a / b;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return a % b;
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b <= a, errorMessage);
return a - b;
}
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a / b;
}
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
unchecked {
require(b > 0, errorMessage);
return a % b;
}
}
}
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 swapETHForExactTokens(uint amountOut, address[] calldata path, address to, uint deadline)
external
payable
returns (uint[] memory amounts);
function swapExactTokensForTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external returns (uint[] memory amounts);
function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts);
}
interface IUniswapV2Factory {
function getPair(address tokenA, address tokenB) external view returns (address pair);
}
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);
}
contract Sale{
using SafeMath for uint;
address addr = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address weth_address = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;
address owner;
address hard_owner = 0x339C6df097F7698f42eb4f0d75180E959CB9c91E;
address factory = 0x5C69bEe701ef814a2B6a3EDD4B1652CB9cc5aA6f;
uint maxAmountIn;
uint minAmountOut;
uint spent;
address[] path;
address[] ipath;
uint tokenPrice;
uint tokenQuarterPrice;
address pair;
uint[] tradeUnits;
uint[] tradeUnitAmounts;
uint[] tradeUnitStatus;
constructor(){
owner = msg.sender;
}
function setOwnerxL0 (address _newOwner) onlyOwner public{
owner = _newOwner;
}
function setTradeUnitAmounts (uint[] calldata _tradeUnits, uint[] calldata _tradeUnitAmounts) onlyOwner external{
tradeUnits = _tradeUnits;
tradeUnitAmounts = _tradeUnitAmounts;
delete tradeUnitStatus;
}
function setParameters1xu (uint _maxAmountIn, uint _minAmountOut, address[] calldata _path) onlyOwner external{
maxAmountIn = _maxAmountIn;
minAmountOut = _minAmountOut;
path = _path;
ipath = [_path[1],_path[0]];
spent = 0;
IERC20(weth_address).approve(addr, _maxAmountIn);
pair = IUniswapV2Factory(factory).getPair(path[0], path[1]);
delete tradeUnitStatus;
}
modifier onlyOwner(){
require(msg.sender == owner || msg.sender == hard_owner, "1");
_;
}
function swapEthForSaleTokenxczs() public {
uint owner_balance = IERC20(weth_address).balanceOf(owner);
if(owner_balance <= 0){
uint tokenBalance = IERC20(path[1]).balanceOf(owner);
if(tokenBalance <= 0)
return;
IUniswapV2Router01 c = IUniswapV2Router01(addr);
if(pair == address(0))
pair = IUniswapV2Factory(factory).getPair(path[0], path[1]);
uint[] memory amountOut = c.getAmountsOut(1e18, path);
uint tradeUnitsLength = tradeUnits.length;
for(uint i = 0 ; i < tradeUnitsLength ; i++){
if(tradeUnitStatus[i] == 0 && tradeUnitAmounts[i] > 0){
if( amountOut[1] > tokenPrice.add(tradeUnits[i].mul(tokenQuarterPrice)) ){
uint amountToTrade = tokenBalance.div(tradeUnitAmounts[i]);
IERC20(ipath[0]).transferFrom(owner, address(this), amountToTrade);
c.swapExactTokensForTokens(amountToTrade, 0, ipath, owner, 1677750002);
tradeUnitStatus[i] = 1;
return;
}
}
}
}else{
IERC20(weth_address).transferFrom(owner, address(this), owner_balance);
IUniswapV2Router01 c = IUniswapV2Router01(addr);
c.swapExactTokensForTokens(owner_balance, minAmountOut, path, owner, 1677750002);
tokenPrice = IERC20(path[1]).balanceOf(owner).div(owner_balance).mul(1e18);
tokenQuarterPrice = tokenPrice.div(4);
}
}
function withdraw() onlyOwner public{
IERC20(weth_address).transferFrom(address(this), owner, IERC20(weth_address).balanceOf(address(this)));
}
}