Skip to content

Instantly share code, notes, and snippets.

@nazariyv
Created September 13, 2020 13:18
Show Gist options
  • Save nazariyv/17ba68d4a0d448a541839c42d67e3b42 to your computer and use it in GitHub Desktop.
Save nazariyv/17ba68d4a0d448a541839c42d67e3b42 to your computer and use it in GitHub Desktop.
Flash loan contract
pragma solidity ^0.6.6;
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/FlashLoanReceiverBase.sol";
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPoolAddressesProvider.sol";
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPool.sol";
contract Flashloan is FlashLoanReceiverBase(0x2bdf0d8b4c67C1e9CC5989bf507E2a307B1aE3e1) {
constructor(address _addressProvider) public {}
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address _reserve,
uint256 _amount,
uint256 _fee,
bytes calldata _params
)
external
override
{
require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?");
//
// Your logic goes here.
// !! Ensure that *this contract* has enough of `_reserve` funds to payback the `_fee` !!
//
uint totalDebt = _amount.add(_fee);
transferFundsBackToPoolInternal(_reserve, totalDebt);
}
// Rest of your code goes here
/**
Flash loan 1000000000000000000 wei (1 ether) worth of `_asset`
*/
function flashloan(address _asset) public onlyOwner {
bytes memory data = "";
uint amount = 1 ether;
ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool());
lendingPool.flashLoan(address(this), _asset, amount, data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment