ERC20FlashMint.sol 3.2 KB

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