ERC20FlashMint.sol 3.5 KB

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