Last active
January 21, 2023 15:51
-
-
Save az0mb13/b16d4ab5678a66118dd1acbf795c5c55 to your computer and use it in GitHub Desktop.
This file contains 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
pragma solidity ^0.8.0; | |
import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; | |
import "@openzeppelin/contracts/utils/Address.sol"; | |
contract NaiveReceiverLenderPool is ReentrancyGuard { | |
using Address for address; | |
uint256 private constant FIXED_FEE = 1 ether; // not the cheapest flash loan | |
function fixedFee() external pure returns (uint256) { | |
return FIXED_FEE; | |
} | |
function flashLoan(address borrower, uint256 borrowAmount) external nonReentrant { | |
uint256 balanceBefore = address(this).balance; | |
require(balanceBefore >= borrowAmount, "Not enough ETH in pool"); | |
require(borrower.isContract(), "Borrower must be a deployed contract"); | |
// Transfer ETH and handle control to receiver | |
borrower.functionCallWithValue( | |
abi.encodeWithSignature( | |
"receiveEther(uint256)", | |
FIXED_FEE | |
), | |
borrowAmount | |
); | |
require( | |
address(this).balance >= balanceBefore + FIXED_FEE, | |
"Flash loan hasn't been paid back" | |
); | |
} | |
// Allow deposits of ETH | |
receive () external payable {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment