ERC20FlashMint.sol 5.1 KB

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