Created
May 18, 2022 14:41
-
-
Save ilamanov/be7234eed5963cda85b884265163129e to your computer and use it in GitHub Desktop.
Flashloan
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
contract YourContract { | |
address aaveContractAddress; | |
function initiateFlashLoan(address erc20token, uint256 amount) { | |
Aave(aaveContractAddress).flashLoan(address(this), erc20token, amount); | |
} | |
function executeOperation(address erc20token, uint256 amount, uint256 fee) { | |
// you're now approved for the "amount". do whatever you want with it | |
// but don't forget to return it at the end | |
ERC20(erc20token).transfer(aaveContractAddress, amount + fee); | |
} | |
} | |
contract Aave { | |
function flashLoan(address receiver, address erc20token, uint256 amount) { | |
ERC20(erc20token).transfer(receiver, amount); | |
uint256 fee = amount * 9 / 10000; | |
YourContract(receiver).executeOperation(erc20token, amount, fee); | |
// Get the funds back. If receiver contract did not "approve" the amount, | |
// next line will fail and transaction will revert | |
ERC20(erc20token).transferFrom(receiver, address(this), amount + fee); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment