Created
October 3, 2021 06:55
-
-
Save yuyasugano/773469032c30805325debd1c0d551d05 to your computer and use it in GitHub Desktop.
Modified Flashswap.sol for PancakeSwap & ApeSwap
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: UNLICENSED | |
pragma solidity >=0.6.6 <0.8.0; | |
import './utils/SafeMath.sol'; | |
import './UniswapV2Library.sol'; | |
import './interfaces/IERC20.sol'; | |
import './interfaces/IUniswapV2Pair.sol'; | |
import './interfaces/IUniswapV2Factory.sol'; | |
import './interfaces/IUniswapV2Router02.sol'; | |
contract Flashswap { | |
using SafeMath for uint; | |
address private owner; | |
address private constant pancakeFactory = 0xBCfCcbde45cE874adCB698cC183deBcF17952812; | |
address private constant bakery = 0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F; | |
address private constant ape = 0xcF0feBd3f17CEf5b47b0cD257aCf6025c5BFf3b7; | |
IUniswapV2Router02 bakeryRouter = IUniswapV2Router02(bakery); | |
IUniswapV2Router02 apeRouter = IUniswapV2Router02(ape); | |
constructor() { | |
owner = msg.sender; | |
} | |
function startArbitrage( | |
address token0, | |
address token1, | |
uint amount0, | |
uint amount1 | |
) external { | |
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); | |
// obtain an amout of token that you exchanged | |
uint amountToken = _amount0 == 0 ? _amount1 : _amount0; | |
address token0 = IUniswapV2Pair(msg.sender).token0(); | |
address token1 = IUniswapV2Pair(msg.sender).token1(); | |
require(msg.sender == UniswapV2Library.pairFor(pancakeFactory, token0, token1)); | |
require(_amount0 == 0 || _amount1 == 0); | |
// if _amount0 is zero sell token1 for token0 | |
// else sell token0 for token1 as a result | |
path[0] = _amount0 == 0 ? token1 : token0; | |
path[1] = _amount0 == 0 ? token0 : token1; | |
// IERC20 token that we will sell for otherToken | |
IERC20 token = IERC20(_amount0 == 0 ? token1 : token0); | |
// token.approve(address(bakeryRouter), amountToken); | |
token.approve(address(apeRouter), amountToken); | |
// calculate the amount of token how much input token should be reimbursed | |
uint amountRequired = UniswapV2Library.getAmountsIn( | |
pancakeFactory, | |
amountToken, | |
path | |
)[0]; | |
// swap token and obtain equivalent otherToken amountRequired as a result | |
// need to receive amountRequired at minimum amount to pay back | |
// uint amountReceived = bakeryRouter.swapExactTokensForTokens( | |
uint amountReceived = apeRouter.swapExactTokensForTokens( | |
amountToken, | |
amountRequired, | |
path, | |
msg.sender, | |
block.timestamp | |
)[1]; | |
require(amountReceived > amountRequired); // fail if we didn't get enough tokens | |
IERC20 otherToken = IERC20(_amount0 == 0 ? token0 : token1); | |
// otherToken.transfer(msg.sender, amountRequired); | |
otherToken.transfer(owner, amountReceived.sub(amountRequired)); | |
} | |
receive() external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment