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
contract UnstoppableLender is ReentrancyGuard { | |
... | |
function depositTokens(uint256 amount) external nonReentrant { | |
require(amount > 0, "Must deposit at least one token"); | |
// Transfer token from sender. Sender must have first approved them. | |
damnValuableToken.transferFrom(msg.sender, address(this), amount); | |
poolBalance = poolBalance + amount; | |
} | |
function flashLoan(uint256 borrowAmount) external nonReentrant { | |
require(borrowAmount > 0, "Must borrow at least one token"); | |
uint256 balanceBefore = damnValuableToken.balanceOf(address(this)); | |
require(balanceBefore >= borrowAmount, "Not enough tokens in pool"); | |
// Ensured by the protocol via the `depositTokens` function | |
assert(poolBalance == balanceBefore); | |
damnValuableToken.transfer(msg.sender, borrowAmount); | |
IReceiver(msg.sender).receiveTokens(address(damnValuableToken), borrowAmount); | |
uint256 balanceAfter = damnValuableToken.balanceOf(address(this)); | |
require(balanceAfter >= balanceBefore, "Flash loan hasn't been paid back"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment