Skip to content

Instantly share code, notes, and snippets.

@zaryab2000
Last active January 16, 2021 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaryab2000/9b7034a45d6faf19c65453bf121a64ac to your computer and use it in GitHub Desktop.
Save zaryab2000/9b7034a45d6faf19c65453bf121a64ac to your computer and use it in GitHub Desktop.
contract UnstoppableLender is ReentrancyGuard {
using SafeMath for uint256;
IERC20 public damnValuableToken;
uint256 public poolBalance;
constructor(address tokenAddress) public {
require(tokenAddress != address(0), "Token address cannot be zero");
damnValuableToken = IERC20(tokenAddress);
}
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.add(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