Created
August 7, 2021 11:09
-
-
Save korrio/0ebd78ac19f7d03be30c5afadd7ed605 to your computer and use it in GitHub Desktop.
Trader_fix
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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.6; | |
library SafeMath { | |
/** | |
* @dev Returns the addition of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `+` operator. | |
* | |
* Requirements: | |
* | |
* - Addition cannot overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
require(c >= a, "SafeMath: addition overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
return sub(a, b, "SafeMath: subtraction overflow"); | |
} | |
/** | |
* @dev Returns the subtraction of two unsigned integers, reverting with custom message on | |
* overflow (when the result is negative). | |
* | |
* Counterpart to Solidity's `-` operator. | |
* | |
* Requirements: | |
* | |
* - Subtraction cannot overflow. | |
*/ | |
function sub( | |
uint256 a, | |
uint256 b, | |
string memory errorMessage | |
) internal pure returns (uint256) { | |
require(b <= a, errorMessage); | |
uint256 c = a - b; | |
return c; | |
} | |
/** | |
* @dev Returns the multiplication of two unsigned integers, reverting on | |
* overflow. | |
* | |
* Counterpart to Solidity's `*` operator. | |
* | |
* Requirements: | |
* | |
* - Multiplication cannot overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522 | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
require(c / a == b, "SafeMath: multiplication overflow"); | |
return c; | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers. Reverts on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
return div(a, b, "SafeMath: division by zero"); | |
} | |
/** | |
* @dev Returns the integer division of two unsigned integers. Reverts with custom message on | |
* division by zero. The result is rounded towards zero. | |
* | |
* Counterpart to Solidity's `/` operator. Note: this function uses a | |
* `revert` opcode (which leaves remaining gas untouched) while Solidity | |
* uses an invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function div( | |
uint256 a, | |
uint256 b, | |
string memory errorMessage | |
) internal pure returns (uint256) { | |
require(b > 0, errorMessage); | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* Reverts when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
return mod(a, b, "SafeMath: modulo by zero"); | |
} | |
/** | |
* @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo), | |
* Reverts with custom message when dividing by zero. | |
* | |
* Counterpart to Solidity's `%` operator. This function uses a `revert` | |
* opcode (which leaves remaining gas untouched) while Solidity uses an | |
* invalid opcode to revert (consuming all remaining gas). | |
* | |
* Requirements: | |
* | |
* - The divisor cannot be zero. | |
*/ | |
function mod( | |
uint256 a, | |
uint256 b, | |
string memory errorMessage | |
) internal pure returns (uint256) { | |
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; | |
} | |
library UniswapV2Library { | |
using SafeMath for uint; | |
// 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, 'UniswapV2Library: IDENTICAL_ADDRESSES'); | |
(token0, token1) = tokenA < tokenB ? (tokenA, tokenB) : (tokenB, tokenA); | |
require(token0 != address(0), 'UniswapV2Library: 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); | |
pair = address(uint(keccak256(abi.encodePacked( | |
hex'ff', | |
factory, | |
keccak256(abi.encodePacked(token0, token1)), | |
hex'5c9b1cf02ea9055b3f4e4613cb44d100a1bbc842d3d277f287be89d053ab35e4' // init code hash | |
)))); | |
} | |
// fetches and sorts the reserves for a pair | |
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); | |
} | |
// given some amount of an asset and pair reserves, returns an equivalent amount of the other asset | |
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; | |
} | |
// given an input amount of an asset and pair reserves, returns the maximum output amount of the other asset | |
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; | |
} | |
// given an output amount of an asset and pair reserves, returns a required input amount of the other asset | |
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); | |
} | |
// performs chained getAmountOut calculations on any number of pairs | |
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); | |
} | |
} | |
// performs chained getAmountIn calculations on any number of pairs | |
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 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); | |
} | |
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; | |
} | |
interface IUniswapV2Factory { | |
event PairCreated(address indexed token0, address indexed token1, address pair, uint); | |
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 feeTo() external view returns (address); | |
function feeToSetter() external view returns (address); | |
function createPair(address tokenA, address tokenB) external 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 Trader { | |
address public pancakeFactory; | |
address public pancakeRouter; // put address in interface | |
address[] path; | |
constructor() public { | |
pancakeFactory = address(0xf21B83178d6e2788213B560aD0a2f895d8a14c66); | |
pancakeRouter = address(0x8149f34D760f3683547b701DcEc4255fed847AD3); | |
} | |
// 1. Trade USDT to token - e.g. USDT -> BUSD, USDT -> SIMP | |
function trade_from_stablecoin(address _stablecoin, address _wantToken, uint256 _amountStable) external { | |
// address pairAddress = IUniswapV2Factory(pancakeFactory).getPair(_stablecoin, _wantToken); | |
address[] memory path = new address[](2); | |
path[0] = _stablecoin; | |
path[1] = _wantToken; | |
uint amountOutMin = IUniswapV2Router02(pancakeRouter).getAmountsOut( | |
_amountStable, | |
path | |
)[1]; | |
uint deadline = now + 10 minutes; | |
// IERC20(_stablecoin).approve(address(pancakeFactory), _amountStable); | |
IERC20(_stablecoin).approve(address(pancakeRouter), _amountStable); | |
// IERC20(_stablecoin).approve(address(this), _amountStable); // <---- | |
// function transferFrom(address from, address to, uint value) external returns (bool); | |
IERC20(_stablecoin).transferFrom(msg.sender,address(this), _amountStable); | |
uint amountReceived = IUniswapV2Router02(pancakeRouter).swapExactTokensForTokens( | |
_amountStable, // 100 | |
amountOutMin, // 99 | |
path, | |
// address(this), | |
msg.sender, | |
deadline | |
)[1]; | |
// logic | |
} | |
fallback() payable external { | |
} | |
receive() payable external { | |
} | |
} | |
// 2. Create LP between BUSD-SIMP | |
// function startArbitrage( | |
// address token0, | |
// address token1, | |
// uint amount0, | |
// uint amount1 | |
// ) external payable{ | |
// address pairAddress = IUniswapV2Factory(pancakeFactory).getPair(token0, token1); | |
// require(pairAddress != address(0), 'This pool does not exist'); | |
// IUniswapV2Pair(pairAddress).swap( | |
// amount0, | |
// amount1, | |
// address(this), | |
// bytes('not empty') | |
// ); | |
// } | |
// function pancakeCall( | |
// address _sender, | |
// uint _amount0, | |
// uint _amount1, | |
// bytes calldata _data | |
// ) external { | |
// address[] memory path = new address[](2); | |
// uint amountToken = _amount0 == 0 ? _amount1 : _amount0; | |
// address token0 = IUniswapV2Pair(msg.sender).token0(); | |
// address token1 = IUniswapV2Pair(msg.sender).token1(); | |
// IERC20(token0).approve(address(this), _amount0); | |
// IERC20(token1).approve(address(this), _amount1); | |
// require( | |
// msg.sender == UniswapV2Library.pairFor(pancakeFactory, token0, token1), | |
// 'Unauthorized' | |
// ); | |
// require(_amount0 == 0 || _amount1 == 0); | |
// path[0] = _amount0 == 0 ? token1 : token0; | |
// path[1] = _amount0 == 0 ? token0 : token1; | |
// IERC20 token = IERC20(_amount0 == 0 ? token1 : token0); | |
// token.approve(address(bakeryRouter), amountToken); | |
// uint amountRequired = UniswapV2Library.getAmountsIn( | |
// pancakeFactory, | |
// amountToken, | |
// path | |
// )[0]; | |
// uint amountReceived = bakeryRouter.swapExactTokensForTokens( | |
// amountToken, | |
// amountRequired, | |
// path, | |
// msg.sender, | |
// deadline | |
// )[1]; | |
// IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1); | |
// otherToken.transfer(msg.sender, amountRequired); | |
// otherToken.transfer(tx.origin, amountReceived - amountRequired); | |
// } | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment