Skip to content

Instantly share code, notes, and snippets.

@ilamanov
Created May 18, 2022 14:41
Show Gist options
  • Save ilamanov/be7234eed5963cda85b884265163129e to your computer and use it in GitHub Desktop.
Save ilamanov/be7234eed5963cda85b884265163129e to your computer and use it in GitHub Desktop.
Flashloan
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