ERC20FlashMint.sol 3.2 KB

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