Created
August 29, 2021 07:08
-
-
Save korrio/5830535f1117b6ab7737edf567a7405e to your computer and use it in GitHub Desktop.
PostFlashloan.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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.6.12; | |
library SafeMath { | |
function add(uint x, uint y) internal pure returns (uint z) { | |
require((z = x + y) >= x, 'ds-math-add-overflow'); | |
} | |
function sub(uint x, uint y) internal pure returns (uint z) { | |
require((z = x - y) <= x, 'ds-math-sub-underflow'); | |
} | |
function mul(uint x, uint y) internal pure returns (uint z) { | |
require(y == 0 || (z = x * y) / y == x, 'ds-math-mul-overflow'); | |
} | |
} | |
library PancakeLibrary { | |
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, '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); | |
pair = address(uint(keccak256(abi.encodePacked( | |
hex'ff', | |
factory, | |
keccak256(abi.encodePacked(token0, token1)), | |
hex'f098791c4153da2a5ea692d792b24b7cf1a55fb2be3686f804304b50d0a9b5e3' // 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); | |
pairFor(factory, tokenA, tokenB); | |
(uint reserve0, uint 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(uint amountA, uint reserveA, uint reserveB) internal pure returns (uint 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(uint amountIn, uint reserveIn, uint reserveOut) internal pure returns (uint amountOut) { | |
require(amountIn > 0, 'PancakeLibrary: INSUFFICIENT_INPUT_AMOUNT'); | |
require(reserveIn > 0 && reserveOut > 0, 'PancakeLibrary: INSUFFICIENT_LIQUIDITY'); | |
uint amountInWithFee = amountIn.mul(998); | |
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, 'PancakeLibrary: INSUFFICIENT_OUTPUT_AMOUNT'); | |
require(reserveIn > 0 && reserveOut > 0, 'PancakeLibrary: INSUFFICIENT_LIQUIDITY'); | |
uint numerator = reserveIn.mul(amountOut).mul(1000); | |
uint denominator = reserveOut.sub(amountOut).mul(998); | |
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, 'PancakeLibrary: 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, 'PancakeLibrary: 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 IPancakePair { | |
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 IERC20 { | |
/** | |
* @dev Returns the amount of tokens in existence. | |
*/ | |
function totalSupply() external view returns (uint256); | |
/** | |
* @dev Returns the amount of tokens owned by `account`. | |
*/ | |
function balanceOf(address account) external view returns (uint256); | |
/** | |
* @dev Moves `amount` tokens from the caller's account to `recipient`. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
/** | |
* @dev Returns the remaining number of tokens that `spender` will be | |
* allowed to spend on behalf of `owner` through {transferFrom}. This is | |
* zero by default. | |
* | |
* This value changes when {approve} or {transferFrom} are called. | |
*/ | |
function allowance(address owner, address spender) external view returns (uint256); | |
/** | |
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* IMPORTANT: Beware that changing an allowance with this method brings the risk | |
* that someone may use both the old and the new allowance by unfortunate | |
* transaction ordering. One possible solution to mitigate this race | |
* condition is to first reduce the spender's allowance to 0 and set the | |
* desired value afterwards: | |
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729 | |
* | |
* Emits an {Approval} event. | |
*/ | |
function approve(address spender, uint256 amount) external returns (bool); | |
/** | |
* @dev Moves `amount` tokens from `sender` to `recipient` using the | |
* allowance mechanism. `amount` is then deducted from the caller's | |
* allowance. | |
* | |
* Returns a boolean value indicating whether the operation succeeded. | |
* | |
* Emits a {Transfer} event. | |
*/ | |
function transferFrom( | |
address sender, | |
address recipient, | |
uint256 amount | |
) external returns (bool); | |
/** | |
* @dev Emitted when `value` tokens are moved from one account (`from`) to | |
* another (`to`). | |
* | |
* Note that `value` may be zero. | |
*/ | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
/** | |
* @dev Emitted when the allowance of a `spender` for an `owner` is set by | |
* a call to {approve}. `value` is the new allowance. | |
*/ | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
} | |
interface IMySwapRouter { | |
function swapExactTokensForTokensSupportingFeeOnTransferTokens( | |
uint amountIn, | |
uint amountOutMin, | |
address[] calldata path, | |
address to, | |
uint deadline | |
) external; | |
function swapExactTokensForTokens( | |
uint amountIn, | |
uint amountOutMin, | |
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 getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts); | |
} | |
contract Arbitrage { | |
address public nickRouter; | |
address public dreamRouter; | |
address public nickFactory; | |
address public dreamFactory; | |
address public busd; | |
address public wbnb; | |
address public lpAddress; | |
constructor() public { | |
nickRouter = address(0x4012d79E02C3BE34552B835E8F4736cBaFfa121D); | |
dreamRouter = address(0x05cf160b080348CC35A0CF331e0e5811E5310a2A); | |
busd = address(0xB760bDf88a3ABfa4fa5D1217b8AaEe7893A6ecEB); | |
wbnb = address(0xB8c703826dC26B8dfb2d4F2AA4652A430c363212); | |
lpAddress = address(0xaf9c5209affc384d0ca0c3097ca3a4aa1f6e480900); | |
nickFactory = address(0x30038395A038B70D28838bc1D99c41E53c342d6f); | |
} | |
function performTask( | |
uint amount0, | |
uint amount1 | |
) external { | |
// Lend Pancake Protocol: WBWB = 2000 * 10 ** 18 | |
IPancakePair(lpAddress).swap( | |
amount0, // BUSD = 0 | |
amount1, // WBWB = 2000 * 10 ** 18 | |
address(this), | |
bytes("something") | |
); | |
} | |
function pancakeCall(address owner, uint256 amount0, uint256 amount1, bytes calldata data) external { | |
// arbritageLogic | |
arbritageLogic(amount1); | |
// Repay WBNB to Pancake LP | |
address[] memory pathRepay = new address[](2); | |
pathRepay[0] = busd; //y | |
pathRepay[1] = wbnb; //x | |
uint amountRepay = PancakeLibrary.getAmountsIn( | |
nickFactory, | |
amount1, | |
pathRepay | |
)[0]; | |
IERC20 otherToken = IERC20(wbnb); | |
otherToken.transfer(lpAddress, amountRepay); | |
// payable(address(msg.sender)).transfer(getBalance()); | |
} | |
function arbritageLogic(uint256 amountBUSD) public { // amount of BUSD | |
uint256 amountBNB = swapAtNickRouter(amountBUSD); | |
swapAtDreamRouter(amountBNB); | |
} | |
function swapAtNickRouter(uint256 amount) public returns(uint256) { // SWAP 1 BUSD -> 0.098 WBNB => ถูกกว่า | |
address[] memory path = new address[](2); | |
path[0] = address(busd); | |
path[1] = address(wbnb); | |
uint amountOutMin = IMySwapRouter(nickRouter).getAmountsOut( | |
amount, | |
path | |
)[1]; | |
IERC20(busd).approve(address(nickRouter),amount); | |
uint256[] memory amounts = IMySwapRouter(nickRouter).swapExactTokensForTokens( | |
amount, | |
amountOutMin, | |
path, | |
address(this), | |
block.timestamp | |
); | |
uint256 amountWbnb = amounts[1]; | |
return amountWbnb; | |
} | |
function swapAtDreamRouter(uint256 amountBNB) public { // SWAP 1 BUSD -> 0.096 WBNB | |
address[] memory path = new address[](2); | |
path[0] = address(wbnb); | |
path[1] = address(busd); | |
uint amountOutMin = IMySwapRouter(dreamRouter).getAmountsOut( | |
amountBNB, | |
path | |
)[1]; | |
IERC20(wbnb).approve(address(dreamRouter),amountBNB); | |
IMySwapRouter(dreamRouter).swapExactTokensForTokens( | |
amountBNB, | |
amountOutMin, | |
path, | |
address(this), | |
block.timestamp | |
); | |
} | |
function transferBUSD(address _to) public { | |
uint _balance = IERC20(busd).balanceOf(address(this)); | |
IERC20(busd).transfer(_to, _balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment