Skip to content

Instantly share code, notes, and snippets.

@this-post
Created August 14, 2021 18:34
Show Gist options
  • Save this-post/1f164d4543ad4fcb36f1043583fc751d to your computer and use it in GitHub Desktop.
Save this-post/1f164d4543ad4fcb36f1043583fc751d to your computer and use it in GitHub Desktop.
pragma solidity ^0.6.0;
import './Libraries/SafeMath.sol';
import './Interfaces/IPancakeERC20.sol';
import './Interfaces/IPancakeFactory.sol';
import './Interfaces/IPancakePair.sol';
import './Interfaces/IPancakeRouter01.sol';
import './Libraries/PancakeLibrary.sol';
import './Interfaces/IBakerySwapRouter.sol';
contract TestFlashLoan{
address owner;
//token
address wbnbAddr;
address busdAddr;
//Pancake
address wbnb_busd_pancakeLPAddr;
address pancakeRouterAddr;
address pancakeFactoryAddr;
IPancakeRouter01 pancakeRouter;
//Bakery
address wbnb_busd_bakeryLPAddr;
address bakeryRouterAddr;
address bakeryFactoryAddr;
event Log(uint amout);
constructor() public {
//me
owner = msg.sender;
//Bakery
//wbnb_busd_bakeryLPAddr = ;
bakeryRouterAddr = 0xCDe540d7eAFE93aC5fE6233Bee57E1270D3E330F;
bakeryFactoryAddr = 0x01bF7C66c6BD861915CdaaE475042d3c4BaE16A7;
//Pancake
wbnb_busd_pancakeLPAddr = 0x0126D4Bbb6f6059128869E49B25F6dA9121D8C75;
pancakeRouterAddr = 0x9Ac64Cc6e4415144C455BD8E4837Fea55603e5c3;
pancakeFactoryAddr = 0xB7926C0430Afb07AA7DEfDE6DA862aE0Bde767bc;
//token
busdAddr = 0xCb096FA97d970ac01857cB76186e228262dE2999;
wbnbAddr = 0xbA4756Ef6f19E75EAfF9e20Ed69d913C7eec4176;
}
// flash swap for busd, busd is amount1, wbnb is amount0
function pancakeFlashLoan(
uint amount0,
uint amount1
) external {
IPancakePair(wbnb_busd_pancakeLPAddr).swap(
amount0,
amount1,
address(this),
bytes("something")
);
}
function pancakeCall(
address _sender,
uint _amount0,
uint _amount1,
bytes calldata _data
) external {
uint amountToken = _amount0 == 0 ? _amount1 : _amount0;
address[] memory path = new address[](2);
path[0] = busdAddr;
path[1] = wbnbAddr;
uint amountOutMin = IBakerySwapRouter(bakeryRouterAddr).getAmountsOut(
amountToken,
path
)[1];
uint deadline = now + 20 minutes;
IPancakeERC20 busd = IPancakeERC20(busdAddr);
busd.approve(address(bakeryRouterAddr), amountToken);
uint amountReceived = IBakerySwapRouter(bakeryRouterAddr).swapExactTokensForTokens(
amountToken,
amountOutMin,
path,
address(this),
deadline
)[1];
address[] memory pathRepay = new address[](2);
pathRepay[0] = wbnbAddr; //y
pathRepay[1] = busdAddr; //x
uint amountRepay = PancakeLibrary.getAmountsIn(
pancakeFactoryAddr,
amountToken,
pathRepay
)[0];
IPancakeERC20 otherToken = IPancakeERC20(wbnbAddr);
otherToken.transfer(wbnb_busd_pancakeLPAddr, amountRepay);
uint profit = IPancakeERC20(wbnbAddr).balanceOf(address(this));
otherToken.transfer(owner, profit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment