ERC20FlashMint.sol 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../../../interfaces/IERC3156.sol";
  4. import "../ERC20.sol";
  5. /**
  6. * @dev Implementation of the ERC3156 Flash loans extension, as defined in
  7. * https://eips.ethereum.org/EIPS/eip-3156[ERC-3156].
  8. *
  9. * Adds the {flashLoan} method, which provides flash loan support at the token
  10. * level. By default there is no fee, but this can be changed by overriding {flashFee}.
  11. *
  12. * _Available since v4.1._
  13. */
  14. abstract contract ERC20FlashMint is ERC20, IERC3156FlashLender {
  15. bytes32 constant private RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  16. /**
  17. * @dev Returns the maximum amount of tokens available for loan.
  18. * @param token The address of the token that is requested.
  19. * @return The amont of token that can be loaned.
  20. */
  21. function maxFlashLoan(address token) public view override returns (uint256) {
  22. return token == address(this) ? type(uint256).max - ERC20.totalSupply() : 0;
  23. }
  24. /**
  25. * @dev Returns the fee applied when doing flash loans. By default this
  26. * implementation has 0 fees. This function can be overloaded to make
  27. * the flash loan mechanism deflationary.
  28. * @param token The token to be flash loaned.
  29. * @param amount The amount of tokens to be loaned.
  30. * @return The fees applied to the corresponding flash loan.
  31. */
  32. function flashFee(address token, uint256 amount) public view virtual override returns (uint256) {
  33. require(token == address(this), "ERC20FlashMint: wrong token");
  34. // silence warning about unused variable without the addition of bytecode.
  35. amount;
  36. return 0;
  37. }
  38. /**
  39. * @dev Performs a flash loan. New tokens are minted and sent to the
  40. * `receiver`, who is required to implement the {IERC3156FlashBorrower}
  41. * interface. By the end of the flash loan, the receiver is expected to own
  42. * amount + fee tokens and have them approved back to the token contract itself so
  43. * they can be burned.
  44. * @param receiver The receiver of the flash loan. Should implement the
  45. * {IERC3156FlashBorrower.onFlashLoan} interface.
  46. * @param token The token to be flash loaned. Only `address(this)` is
  47. * supported.
  48. * @param amount The amount of tokens to be loaned.
  49. * @param data An arbitrary datafield that is passed to the receiver.
  50. * @return `true` is the flash loan was successfull.
  51. */
  52. function flashLoan(
  53. IERC3156FlashBorrower receiver,
  54. address token,
  55. uint256 amount,
  56. bytes calldata data
  57. )
  58. public virtual override returns (bool)
  59. {
  60. uint256 fee = flashFee(token, amount);
  61. _mint(address(receiver), amount);
  62. require(receiver.onFlashLoan(msg.sender, token, amount, fee, data) == RETURN_VALUE, "ERC20FlashMint: invalid return value");
  63. uint256 currentAllowance = allowance(address(receiver), address(this));
  64. require(currentAllowance >= amount + fee, "ERC20FlashMint: allowance does not allow refund");
  65. _approve(address(receiver), address(this), currentAllowance - amount - fee);
  66. _burn(address(receiver), amount + fee);
  67. return true;
  68. }
  69. }