Skip to content

Instantly share code, notes, and snippets.

@yuyasugano
Created January 24, 2021 08:24
Show Gist options
  • Save yuyasugano/b86a03d8fe9c37525f8a933976277da4 to your computer and use it in GitHub Desktop.
Save yuyasugano/b86a03d8fe9c37525f8a933976277da4 to your computer and use it in GitHub Desktop.
callFunction for Uniswap & Sushiswap arbitrage
// This is the function that will be called postLoan
// i.e. Encode the logic to handle your flashloaned funds here
function callFunction(
address sender,
Account.Info memory account,
bytes memory data
) public {
DexArbitrage memory dex = abi.decode(data, (DexArbitrage));
address[] memory path1 = new address[](2);
address[] memory path2 = new address[](3);
uint256 amountIna = IERC20(dex.a).balanceOf(address(this));
IERC20(dex.a).approve(address(uniswap), amountIna);
path1[0] = address(dex.a);
path1[1] = address(dex.b);
uniswap.swapExactTokensForTokens(amountIna, 0, path1, address(this), block.timestamp);
uint256 amountInb = IERC20(dex.b).balanceOf(address(this));
IERC20(dex.b).approve(address(sushiswap), amountInb);
path2[0] = address(dex.b);
path2[1] = address(address(weth));
path2[2] = address(dex.c);
sushiswap.swapExactTokensForTokens(amountInb, 0, path2, address(this), block.timestamp);
uint256 amountInc = IERC20(dex.c).balanceOf(address(this));
IERC20(dex.c).approve(address(uniswap), amountInc);
path1[0] = address(dex.c);
path1[1] = address(dex.a);
uniswap.swapExactTokensForTokens(amountInc, 0, path1, address(this), block.timestamp);
uint256 profit = IERC20(dex.a).balanceOf(address(this)) - dex.repayAmount;
// Note that you can ignore the line below
// if your dydx account (this contract in this case)
// has deposited at least ~2 Wei of assets into the account
// to balance out the collaterization ratio
require(
profit > 0,
"Not enough funds to repay dydx loan!"
);
// transfer the profit to the owner address
IERC20(dex.a).transfer(owner, profit);
// TODO: Encode your logic here
// E.g. arbitrage, liquidate accounts, etc
// revert("Hello, you haven't encoded your logic");
emit FlashLoanEmitted(dex.a, dex.b, dex.c, dex.repayAmount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment