draft-ERC20FlashMint.sol 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. abstract contract ERC20FlashMint is ERC20, IERC3156FlashLender {
  13. bytes32 constant private RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
  14. /**
  15. * @dev Returns the maximum amount of tokens available for loan.
  16. * @param token The address of the token that is requested.
  17. * @return The amont of token that can be loaned.
  18. */
  19. function maxFlashLoan(address token) public view override returns (uint256) {
  20. return token == address(this) ? type(uint256).max - ERC20.totalSupply() : 0;
  21. }
  22. /**
  23. * @dev Returns the fee applied when doing flash loans. By default this
  24. * implementation has 0 fees. This function can be overloaded to make
  25. * the flash loan mechanism deflationary.
  26. * @param token The token to be flash loaned.
  27. * @param amount The amount of tokens to be loaned.
  28. * @return The fees applied to the corresponding flash loan.
  29. */
  30. function flashFee(address token, uint256 amount) public view virtual override returns (uint256) {
  31. require(token == address(this), "ERC20FlashMint: wrong token");
  32. // silence warning about unused variable without the addition of bytecode.
  33. amount;
  34. return 0;
  35. }
  36. /**
  37. * @dev Performs a flash loan. New tokens are minted and sent to the
  38. * `receiver`, who is required to implement the {IERC3156FlashBorrower}
  39. * interface. By the end of the flash loan, the receiver is expected to own
  40. * amount + fee tokens and have them approved back to the token contract itself so
  41. * they can be burned.
  42. * @param receiver The receiver of the flash loan. Should implement the
  43. * {IERC3156FlashBorrower.onFlashLoan} interface.
  44. * @param token The token to be flash loaned. Only `address(this)` is
  45. * supported.
  46. * @param amount The amount of tokens to be loaned.
  47. * @param data An arbitrary datafield that is passed to the receiver.
  48. * @return `true` is the flash loan was successfull.
  49. */
  50. function flashLoan(
  51. IERC3156FlashBorrower receiver,
  52. address token,
  53. uint256 amount,
  54. bytes calldata data
  55. )
  56. public virtual override returns (bool)
  57. {
  58. uint256 fee = flashFee(token, amount);
  59. _mint(address(receiver), amount);
  60. require(receiver.onFlashLoan(msg.sender, token, amount, fee, data) == RETURN_VALUE, "ERC20FlashMint: invalid return value");
  61. uint256 currentAllowance = allowance(address(receiver), address(this));
  62. require(currentAllowance >= amount + fee, "ERC20FlashMint: allowance does not allow refund");
  63. _approve(address(receiver), address(this), currentAllowance - amount - fee);
  64. _burn(address(receiver), amount + fee);
  65. return true;
  66. }
  67. }