Created
March 22, 2022 15:35
-
-
Save htkcodes/dbd10de2d8a96e0fcc095bcbdf11726e to your computer and use it in GitHub Desktop.
..
This file contains 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.6.6; | |
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 x, uint256 y) internal pure returns (uint256 z) { | |
require((z = x + y) >= x, "ds-math-add-overflow"); | |
} | |
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) { | |
require((z = x - y) <= x, "ds-math-sub-underflow"); | |
} | |
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) { | |
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow"); | |
} | |
} | |
library PancakeLibrary { | |
using SafeMath for uint256; | |
// returns sorted token addresses, used to handle return values from pairs sorted in this order | |
function sortTokens(address tokenA, address tokenB) | |
internal | |
pure | |
returns (address token0, address token1) | |
{ | |
require(tokenA != tokenB, "PancakeLibrary: IDENTICAL_ADDRESSES"); | |
(token0, token1) = tokenA < tokenB | |
? (tokenA, tokenB) | |
: (tokenB, tokenA); | |
require(token0 != address(0), "PancakeLibrary: ZERO_ADDRESS"); | |
} | |
// calculates the CREATE2 address for a pair without making any external calls | |
function pairFor( | |
address factory, | |
address tokenA, | |
address tokenB | |
) internal pure returns (address pair) { | |
(address token0, address token1) = sortTokens(tokenA, tokenB); | |
bytes32 hash = keccak256( | |
abi.encodePacked( | |
hex"ff", | |
factory, | |
keccak256(abi.encodePacked(token0, token1)), | |
hex"00fb7f630766e6a796048ea87d01acd3068e8ff67d078148a3fa3f4a84f69bd5" //TESTNET | |
) | |
); | |
pair = address(uint160(uint256(hash))); | |
} | |
// fetches and sorts the reserves for a pair | |
function getReserves( | |
address factory, | |
address tokenA, | |
address tokenB | |
) internal view returns (uint256 reserveA, uint256 reserveB) { | |
(address token0, ) = sortTokens(tokenA, tokenB); | |
pairFor(factory, tokenA, tokenB); | |
(uint256 reserve0, uint256 reserve1, ) = IPancakePair( | |
pairFor(factory, tokenA, tokenB) | |
).getReserves(); | |
(reserveA, reserveB) = tokenA == token0 | |
? (reserve0, reserve1) | |
: (reserve1, reserve0); | |
} | |
// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset | |
function quote( | |
uint256 amountA, | |
uint256 reserveA, | |
uint256 reserveB | |
) internal pure returns (uint256 amountB) { | |
require(amountA > 0, "PancakeLibrary: INSUFFICIENT_AMOUNT"); | |
require( | |
reserveA > 0 && reserveB > 0, | |
"PancakeLibrary: INSUFFICIENT_LIQUIDITY" | |
); | |
amountB = amountA.mul(reserveB) / reserveA; | |
} | |
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset | |
function getAmountOut( | |
uint256 amountIn, | |
uint256 reserveIn, | |
uint256 reserveOut | |
) internal pure returns (uint256 amountOut) { | |
require(amountIn > 0, "PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT"); | |
require( | |
reserveIn > 0 && reserveOut > 0, | |
"PancakeLibrary: INSUFFICIENT_LIQUIDITY" | |
); | |
uint256 amountInWithFee = amountIn.mul(9975); | |
uint256 numerator = amountInWithFee.mul(reserveOut); | |
uint256 denominator = reserveIn.mul(10000).add(amountInWithFee); | |
amountOut = numerator / denominator; | |
} | |
// given an output amount of an asset and pair reserves, returns a required input amount of the other asset | |
function getAmountIn( | |
uint256 amountOut, | |
uint256 reserveIn, | |
uint256 reserveOut | |
) internal pure returns (uint256 amountIn) { | |
require(amountOut > 0, "PancakeLibrary: INSUFFICIENT_OUTPUT_AMOUNT"); | |
require( | |
reserveIn > 0 && reserveOut > 0, | |
"PancakeLibrary: INSUFFICIENT_LIQUIDITY" | |
); | |
uint256 numerator = reserveIn.mul(amountOut).mul(10000); | |
uint256 denominator = reserveOut.sub(amountOut).mul(9975); | |
amountIn = (numerator / denominator).add(1); | |
} | |
// performs chained getAmountOut calculations on any number of pairs | |
function getAmountsOut( | |
address factory, | |
uint256 amountIn, | |
address[] memory path | |
) internal view returns (uint256[] memory amounts) { | |
require(path.length >= 2, "PancakeLibrary: INVALID_PATH"); | |
amounts = new uint256[](path.length); | |
amounts[0] = amountIn; | |
for (uint256 i; i < path.length - 1; i++) { | |
(uint256 reserveIn, uint256 reserveOut) = getReserves( | |
factory, | |
path[i], | |
path[i + 1] | |
); | |
amounts[i + 1] = getAmountOut(amounts[i], reserveIn, reserveOut); | |
} | |
} | |
// performs chained getAmountIn calculations on any number of pairs | |
function getAmountsIn( | |
address factory, | |
uint256 amountOut, | |
address[] memory path | |
) internal view returns (uint256[] memory amounts) { | |
require(path.length >= 2, "PancakeLibrary: INVALID_PATH"); | |
amounts = new uint256[](path.length); | |
amounts[amounts.length - 1] = amountOut; | |
for (uint256 i = path.length - 1; i > 0; i--) { | |
(uint256 reserveIn, uint256 reserveOut) = getReserves( | |
factory, | |
path[i - 1], | |
path[i] | |
); | |
amounts[i - 1] = getAmountIn(amounts[i], reserveIn, reserveOut); | |
} | |
} | |
} | |
interface IPancakePair { | |
//SPDX-License-Identifier: UNLICENSED | |
event Approval( | |
address indexed owner, | |
address indexed spender, | |
uint256 value | |
); | |
event Transfer(address indexed from, address indexed to, uint256 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 (uint256); | |
function balanceOf(address owner) external view returns (uint256); | |
function allowance(address owner, address spender) | |
external | |
view | |
returns (uint256); | |
function approve(address spender, uint256 value) external returns (bool); | |
function transfer(address to, uint256 value) external returns (bool); | |
function transferFrom( | |
address from, | |
address to, | |
uint256 value | |
) external returns (bool); | |
event Swap( | |
address indexed sender, | |
uint256 amount0In, | |
uint256 amount1In, | |
uint256 amount0Out, | |
uint256 amount1Out, | |
address indexed to | |
); | |
function getReserves() | |
external | |
view | |
returns ( | |
uint112 reserve0, | |
uint112 reserve1, | |
uint32 blockTimestampLast | |
); | |
function swap( | |
uint256 amount0Out, | |
uint256 amount1Out, | |
address to, | |
bytes calldata data | |
) external; | |
} | |
interface IPancakeRouter02 { | |
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; | |
function factory() external pure returns (address); | |
function WETH() external pure returns (address); | |
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); | |
} | |
contract ToleranceCheckOverride { | |
uint256 constant MAX_UINT = 2**256 - 1; | |
function buyTax(uint256 amountOut,uint256 mount) private returns(uint256) { | |
} | |
function checkToken( | |
address routerAddress, | |
address tokenAddress | |
) external returns (uint256 buyMul, uint256 sellTaxMul){ | |
IPancakeRouter02 router = IPancakeRouter02(routerAddress); | |
address routerAddy = routerAddress; | |
address tokenAddy = tokenAddress; | |
address[] memory pathBuy = new address[](2); | |
uint256[] memory amounts; | |
pathBuy[0] = router.WETH(); | |
pathBuy[1] = tokenAddress; | |
IERC20 token = IERC20(tokenAddress); | |
uint256 initialEth = address(this).balance; | |
amounts = PancakeLibrary.getAmountsOut( | |
router.factory(), | |
initialEth, | |
pathBuy | |
); | |
uint256 buyTokenAmount = amounts[amounts.length - 1]; | |
//Buy tokens | |
uint256 scrapTokenBalance = token.balanceOf(address(this)); | |
router.swapETHForExactTokens{value: initialEth}( | |
buyTokenAmount, | |
pathBuy, | |
address(this), | |
block.timestamp | |
); | |
uint256 tokenAmountOut = token.balanceOf(address(this)) - | |
scrapTokenBalance; | |
buyMul = tokenAmountOut/buyTokenAmount; | |
//Sell token | |
require(tokenAmountOut > 0, "Can't sell this."); | |
address[] memory pathSell = new address[](2); | |
pathSell[0] = tokenAddy; | |
pathSell[1] = router.WETH(); | |
uint256 ethBefore = address(this).balance; | |
token.approve(routerAddy, MAX_UINT); | |
router.swapExactTokensForETHSupportingFeeOnTransferTokens( | |
tokenAmountOut, | |
0, | |
pathSell, | |
address(this), | |
block.timestamp | |
); | |
uint256 ethAfter = address(this).balance; | |
sellTaxMul = ethAfter/ethBefore; | |
//Check tolerance | |
//put a return here?idk :p | |
//bool result = initialEth - ethOut <= tolerance; | |
//assembly { | |
// return result (1 byte) | |
//mstore(0x0, result) | |
//return(0x1f, 0x1) | |
//} | |
} | |
fallback() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment