Skip to content

Instantly share code, notes, and snippets.

@JuanXavier
Last active July 9, 2022 20:29
Show Gist options
  • Select an option

  • Save JuanXavier/3a7f2421f8ef8db171fe3c93bee659bf to your computer and use it in GitHub Desktop.

Select an option

Save JuanXavier/3a7f2421f8ef8db171fe3c93bee659bf to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
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