ERC20FlashMintUpgradeable.sol 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.5.0-rc.0) (token/ERC20/extensions/ERC20FlashMint.sol)
  3. pragma solidity ^0.8.0;
  4. import "../../../interfaces/IERC3156Upgradeable.sol";
  5. import "../ERC20Upgradeable.sol";
  6. import "../../../proxy/utils/Initializable.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 ERC20FlashMintUpgradeable is Initializable, ERC20Upgradeable, IERC3156FlashLenderUpgradeable {
  17. function __ERC20FlashMint_init() internal onlyInitializing {
  18. }
  19. function __ERC20FlashMint_init_unchained() internal onlyInitializing {
  20. }
  21. bytes32 private constant _RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  22. /**
  23. * @dev Returns the maximum amount of tokens available for loan.
  24. * @param token The address of the token that is requested.
  25. * @return The amont of token that can be loaned.
  26. */
  27. function maxFlashLoan(address token) public view virtual override returns (uint256) {
  28. return token == address(this) ? type(uint256).max - ERC20Upgradeable.totalSupply() : 0;
  29. }
  30. /**
  31. * @dev Returns the fee applied when doing flash loans. By default this
  32. * implementation has 0 fees. This function can be overloaded to make
  33. * the flash loan mechanism deflationary.
  34. * @param token The token to be flash loaned.
  35. * @param amount The amount of tokens to be loaned.
  36. * @return The fees applied to the corresponding flash loan.
  37. */
  38. function flashFee(address token, uint256 amount) public view virtual override returns (uint256) {
  39. require(token == address(this), "ERC20FlashMint: wrong token");
  40. // silence warning about unused variable without the addition of bytecode.
  41. amount;
  42. return 0;
  43. }
  44. /**
  45. * @dev Performs a flash loan. New tokens are minted and sent to the
  46. * `receiver`, who is required to implement the {IERC3156FlashBorrower}
  47. * interface. By the end of the flash loan, the receiver is expected to own
  48. * amount + fee tokens and have them approved back to the token contract itself so
  49. * they can be burned.
  50. * @param receiver The receiver of the flash loan. Should implement the
  51. * {IERC3156FlashBorrower.onFlashLoan} interface.
  52. * @param token The token to be flash loaned. Only `address(this)` is
  53. * supported.
  54. * @param amount The amount of tokens to be loaned.
  55. * @param data An arbitrary datafield that is passed to the receiver.
  56. * @return `true` is the flash loan was successful.
  57. */
  58. function flashLoan(
  59. IERC3156FlashBorrowerUpgradeable receiver,
  60. address token,
  61. uint256 amount,
  62. bytes calldata data
  63. ) public virtual override returns (bool) {
  64. require(amount <= maxFlashLoan(token), "ERC20FlashMint: amount exceeds maxFlashLoan");
  65. uint256 fee = flashFee(token, amount);
  66. _mint(address(receiver), amount);
  67. require(
  68. receiver.onFlashLoan(msg.sender, token, amount, fee, data) == _RETURN_VALUE,
  69. "ERC20FlashMint: invalid return value"
  70. );
  71. uint256 currentAllowance = allowance(address(receiver), address(this));
  72. require(currentAllowance >= amount + fee, "ERC20FlashMint: allowance does not allow refund");
  73. _approve(address(receiver), address(this), currentAllowance - amount - fee);
  74. _burn(address(receiver), amount + fee);
  75. return true;
  76. }
  77. /**
  78. * This empty reserved space is put in place to allow future versions to add new
  79. * variables without shifting down storage in the inheritance chain.
  80. * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
  81. */
  82. uint256[50] private __gap;
  83. }