Last active
October 29, 2021 16:24
-
-
Save Desgard/e80f464096e198de8e3560f0020d95bc to your computer and use it in GitHub Desktop.
shua_mdx.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity ^0.8.0; | |
| pragma experimental ABIEncoderV2; | |
| contract StrategySwapFunction { | |
| using SafeERC20 for IERC20; | |
| using Address for address; | |
| using SafeMath for uint256; | |
| address public uniRouter = 0xED7d5F38C79115ca12fe6C0041abb22F0A06C300; | |
| address public swapMiner = 0x7373c42502874C88954bDd6D50b53061F018422e; | |
| address public constant WHT = 0x5545153CCFcA01fbd7Dd11C0b23ba694D9509A6F; | |
| address public constant MDX = 0x25D2e80cB6B86881Fd7e07dd263Fb79f4AbE033c; | |
| address public governance; | |
| constructor() { | |
| // admin | |
| governance = msg.sender; | |
| } | |
| function setRouter(address _one) external { | |
| require(governance == msg.sender, "!gov"); | |
| uniRouter = _one; | |
| } | |
| function setSwapMiner(address _one) external { | |
| require(governance == msg.sender, "!gov"); | |
| swapMiner = _one; | |
| } | |
| function oneTask(address myToken, uint256 price) public { | |
| address[] memory path1 = new address[](2); | |
| path1[0] = myToken; | |
| path1[1] = WHT; | |
| uint256 amount = IERC20(myToken).balanceOf(address(this)); | |
| IMdexRouter(uniRouter).swapExactTokensForTokens( | |
| amount, | |
| amount * price * 995 / 1000, | |
| path1, | |
| address(this), | |
| block.timestamp.add(1800) | |
| ); | |
| path1[0] = WHT; | |
| path1[1] = myToken; | |
| uint256 htAmount = IERC20(WHT).balanceOf(address(this)); | |
| IMdexRouter(uniRouter).swapExactTokensForTokens( | |
| htAmount, | |
| htAmount * 99500 / price / 100000, | |
| path1, | |
| address(this), | |
| block.timestamp.add(1800) | |
| ); | |
| } | |
| function shua(address myToken, uint256 _amount, uint tt, bool needMineReward, uint256 price) public payable { | |
| IERC20(myToken).safeTransferFrom(msg.sender, address(this), _amount); | |
| IERC20(myToken).safeApprove(uniRouter, uint256(0)); | |
| IERC20(myToken).safeApprove(uniRouter, uint256(2**255)); | |
| IERC20(WHT).safeApprove(uniRouter, uint256(0)); | |
| IERC20(WHT).safeApprove(uniRouter, uint256(2**255)); | |
| for (uint i = 0; i < tt; i ++) { | |
| oneTask(myToken, price); | |
| } | |
| uint256 existAmount = IERC20(myToken).balanceOf(address(this)); | |
| IERC20(myToken).safeTransferFrom(address(this), msg.sender, existAmount); | |
| if (needMineReward) { | |
| ISwapMining(swapMiner).takerWithdraw(); | |
| uint256 mdxAmount = IERC20(MDX).balanceOf(address(this)); | |
| IERC20(MDX).safeTransferFrom(address(this), msg.sender, mdxAmount); | |
| } | |
| } | |
| function shuaAll(address myToken, uint tt, bool needMineReward, uint256 price) public payable { | |
| uint256 am = IERC20(myToken).balanceOf(msg.sender); | |
| shua(myToken, am, tt, needMineReward, price); | |
| } | |
| } | |
| interface IMdexRouter { | |
| function factory() external pure returns (address); | |
| function WHT() external pure returns (address); | |
| function swapMining() 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(uint256 amountA, uint256 reserveA, uint256 reserveB) external view returns (uint256 amountB); | |
| function getAmountOut(uint256 amountIn, uint256 reserveIn, uint256 reserveOut) external view returns (uint256 amountOut); | |
| function getAmountIn(uint256 amountOut, uint256 reserveIn, uint256 reserveOut) external view returns (uint256 amountIn); | |
| function getAmountsOut(uint256 amountIn, address[] calldata path) external view returns (uint256[] memory amounts); | |
| function getAmountsIn(uint256 amountOut, address[] calldata path) external view returns (uint256[] memory amounts); | |
| 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; | |
| } | |
| interface ISwapMining { | |
| function takerWithdraw() external; | |
| } | |
| 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 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; | |
| } | |
| } | |
| library Address { | |
| function isContract(address account) internal view returns (bool) { | |
| bytes32 codehash; | |
| bytes32 accountHash = | |
| 0xc5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470; | |
| assembly { | |
| codehash := extcodehash(account) | |
| } | |
| return (codehash != accountHash && codehash != 0x0); | |
| } | |
| } | |
| library SafeERC20 { | |
| using SafeMath for uint256; | |
| using Address for address; | |
| function safeTransfer( | |
| IERC20 token, | |
| address to, | |
| uint256 value | |
| ) internal { | |
| callOptionalReturn( | |
| token, | |
| abi.encodeWithSelector(token.transfer.selector, to, value) | |
| ); | |
| } | |
| function safeTransferFrom( | |
| IERC20 token, | |
| address from, | |
| address to, | |
| uint256 value | |
| ) internal { | |
| callOptionalReturn( | |
| token, | |
| abi.encodeWithSelector(token.transferFrom.selector, from, to, value) | |
| ); | |
| } | |
| function safeApprove( | |
| IERC20 token, | |
| address spender, | |
| uint256 value | |
| ) internal { | |
| require( | |
| (value == 0) || (token.allowance(address(this), spender) == 0), | |
| "SafeERC20: approve from non-zero to non-zero allowance" | |
| ); | |
| callOptionalReturn( | |
| token, | |
| abi.encodeWithSelector(token.approve.selector, spender, value) | |
| ); | |
| } | |
| function safeIncreaseAllowance( | |
| IERC20 token, | |
| address spender, | |
| uint256 value | |
| ) internal { | |
| uint256 newAllowance = | |
| token.allowance(address(this), spender).add(value); | |
| callOptionalReturn( | |
| token, | |
| abi.encodeWithSelector( | |
| token.approve.selector, | |
| spender, | |
| newAllowance | |
| ) | |
| ); | |
| } | |
| function safeDecreaseAllowance( | |
| IERC20 token, | |
| address spender, | |
| uint256 value | |
| ) internal { | |
| uint256 newAllowance = | |
| token.allowance(address(this), spender).sub( | |
| value, | |
| "SafeERC20: decreased allowance below zero" | |
| ); | |
| callOptionalReturn( | |
| token, | |
| abi.encodeWithSelector( | |
| token.approve.selector, | |
| spender, | |
| newAllowance | |
| ) | |
| ); | |
| } | |
| function callOptionalReturn(IERC20 token, bytes memory data) private { | |
| require(address(token).isContract(), "SafeERC20: call to non-contract"); | |
| (bool success, bytes memory returndata) = address(token).call(data); | |
| require(success, "SafeERC20: low-level call failed"); | |
| if (returndata.length > 0) { | |
| require( | |
| abi.decode(returndata, (bool)), | |
| "SafeERC20: ERC20 operation did not succeed" | |
| ); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment