ERC20FlashMint.sol 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0) (token/ERC20/extensions/ERC20FlashMint.sol)
  3. pragma solidity ^0.8.20;
  4. import {IERC3156FlashBorrower} from "../../../interfaces/IERC3156FlashBorrower.sol";
  5. import {IERC3156FlashLender} from "../../../interfaces/IERC3156FlashLender.sol";
  6. import {ERC20} from "../ERC20.sol";
  7. /**
  8. * @dev Implementation of the ERC-3156 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. * NOTE: When this extension is used along with the {ERC20Capped} or {ERC20Votes} extensions,
  15. * {maxFlashLoan} will not correctly reflect the maximum that can be flash minted. We recommend
  16. * overriding {maxFlashLoan} so that it correctly reflects the supply cap.
  17. */
  18. abstract contract ERC20FlashMint is ERC20, IERC3156FlashLender {
  19. bytes32 private constant RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  20. /**
  21. * @dev The loan token is not valid.
  22. */
  23. error ERC3156UnsupportedToken(address token);
  24. /**
  25. * @dev The requested loan exceeds the max loan value for `token`.
  26. */
  27. error ERC3156ExceededMaxLoan(uint256 maxLoan);
  28. /**
  29. * @dev The receiver of a flashloan is not a valid {onFlashLoan} implementer.
  30. */
  31. error ERC3156InvalidReceiver(address receiver);
  32. /**
  33. * @dev Returns the maximum amount of tokens available for loan.
  34. * @param token The address of the token that is requested.
  35. * @return The amount of token that can be loaned.
  36. *
  37. * NOTE: This function does not consider any form of supply cap, so in case
  38. * it's used in a token with a cap like {ERC20Capped}, make sure to override this
  39. * function to integrate the cap instead of `type(uint256).max`.
  40. */
  41. function maxFlashLoan(address token) public view virtual returns (uint256) {
  42. return token == address(this) ? type(uint256).max - totalSupply() : 0;
  43. }
  44. /**
  45. * @dev Returns the fee applied when doing flash loans. This function calls
  46. * the {_flashFee} function which returns the fee applied when doing flash
  47. * loans.
  48. * @param token The token to be flash loaned.
  49. * @param value The amount of tokens to be loaned.
  50. * @return The fees applied to the corresponding flash loan.
  51. */
  52. function flashFee(address token, uint256 value) public view virtual returns (uint256) {
  53. if (token != address(this)) {
  54. revert ERC3156UnsupportedToken(token);
  55. }
  56. return _flashFee(token, value);
  57. }
  58. /**
  59. * @dev Returns the fee applied when doing flash loans. By default this
  60. * implementation has 0 fees. This function can be overloaded to make
  61. * the flash loan mechanism deflationary.
  62. * @param token The token to be flash loaned.
  63. * @param value The amount of tokens to be loaned.
  64. * @return The fees applied to the corresponding flash loan.
  65. */
  66. function _flashFee(address token, uint256 value) internal view virtual returns (uint256) {
  67. // silence warning about unused variable without the addition of bytecode.
  68. token;
  69. value;
  70. return 0;
  71. }
  72. /**
  73. * @dev Returns the receiver address of the flash fee. By default this
  74. * implementation returns the address(0) which means the fee amount will be burnt.
  75. * This function can be overloaded to change the fee receiver.
  76. * @return The address for which the flash fee will be sent to.
  77. */
  78. function _flashFeeReceiver() internal view virtual returns (address) {
  79. return address(0);
  80. }
  81. /**
  82. * @dev Performs a flash loan. New tokens are minted and sent to the
  83. * `receiver`, who is required to implement the {IERC3156FlashBorrower}
  84. * interface. By the end of the flash loan, the receiver is expected to own
  85. * value + fee tokens and have them approved back to the token contract itself so
  86. * they can be burned.
  87. * @param receiver The receiver of the flash loan. Should implement the
  88. * {IERC3156FlashBorrower-onFlashLoan} interface.
  89. * @param token The token to be flash loaned. Only `address(this)` is
  90. * supported.
  91. * @param value The amount of tokens to be loaned.
  92. * @param data An arbitrary datafield that is passed to the receiver.
  93. * @return `true` if the flash loan was successful.
  94. */
  95. // This function can reenter, but it doesn't pose a risk because it always preserves the property that the amount
  96. // minted at the beginning is always recovered and burned at the end, or else the entire function will revert.
  97. // slither-disable-next-line reentrancy-no-eth
  98. function flashLoan(
  99. IERC3156FlashBorrower receiver,
  100. address token,
  101. uint256 value,
  102. bytes calldata data
  103. ) public virtual returns (bool) {
  104. uint256 maxLoan = maxFlashLoan(token);
  105. if (value > maxLoan) {
  106. revert ERC3156ExceededMaxLoan(maxLoan);
  107. }
  108. uint256 fee = flashFee(token, value);
  109. _mint(address(receiver), value);
  110. if (receiver.onFlashLoan(_msgSender(), token, value, fee, data) != RETURN_VALUE) {
  111. revert ERC3156InvalidReceiver(address(receiver));
  112. }
  113. address flashFeeReceiver = _flashFeeReceiver();
  114. _spendAllowance(address(receiver), address(this), value + fee);
  115. if (fee == 0 || flashFeeReceiver == address(0)) {
  116. _burn(address(receiver), value + fee);
  117. } else {
  118. _burn(address(receiver), value);
  119. _transfer(address(receiver), flashFeeReceiver, fee);
  120. }
  121. return true;
  122. }
  123. }