You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
110 lines
4.7 KiB
Solidity
110 lines
4.7 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
// OpenZeppelin Contracts (last updated v4.8.0) (token/ERC20/extensions/ERC20FlashMint.sol)
|
|
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "../../../interfaces/IERC3156FlashBorrower.sol";
|
|
import "../../../interfaces/IERC3156FlashLender.sol";
|
|
import "../ERC20.sol";
|
|
|
|
/**
|
|
* @dev Implementation of the ERC3156 Flash loans extension, as defined in
|
|
* https://eips.ethereum.org/EIPS/eip-3156[ERC-3156].
|
|
*
|
|
* Adds the {flashLoan} method, which provides flash loan support at the token
|
|
* level. By default there is no fee, but this can be changed by overriding {flashFee}.
|
|
*
|
|
* _Available since v4.1._
|
|
*/
|
|
abstract contract ERC20FlashMint is ERC20, IERC3156FlashLender {
|
|
bytes32 private constant _RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");
|
|
|
|
/**
|
|
* @dev Returns the maximum amount of tokens available for loan.
|
|
* @param token The address of the token that is requested.
|
|
* @return The amount of token that can be loaned.
|
|
*/
|
|
function maxFlashLoan(address token) public view virtual override returns (uint256) {
|
|
return token == address(this) ? type(uint256).max - ERC20.totalSupply() : 0;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the fee applied when doing flash loans. This function calls
|
|
* the {_flashFee} function which returns the fee applied when doing flash
|
|
* loans.
|
|
* @param token The token to be flash loaned.
|
|
* @param amount The amount of tokens to be loaned.
|
|
* @return The fees applied to the corresponding flash loan.
|
|
*/
|
|
function flashFee(address token, uint256 amount) public view virtual override returns (uint256) {
|
|
require(token == address(this), "ERC20FlashMint: wrong token");
|
|
return _flashFee(token, amount);
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the fee applied when doing flash loans. By default this
|
|
* implementation has 0 fees. This function can be overloaded to make
|
|
* the flash loan mechanism deflationary.
|
|
* @param token The token to be flash loaned.
|
|
* @param amount The amount of tokens to be loaned.
|
|
* @return The fees applied to the corresponding flash loan.
|
|
*/
|
|
function _flashFee(address token, uint256 amount) internal view virtual returns (uint256) {
|
|
// silence warning about unused variable without the addition of bytecode.
|
|
token;
|
|
amount;
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the receiver address of the flash fee. By default this
|
|
* implementation returns the address(0) which means the fee amount will be burnt.
|
|
* This function can be overloaded to change the fee receiver.
|
|
* @return The address for which the flash fee will be sent to.
|
|
*/
|
|
function _flashFeeReceiver() internal view virtual returns (address) {
|
|
return address(0);
|
|
}
|
|
|
|
/**
|
|
* @dev Performs a flash loan. New tokens are minted and sent to the
|
|
* `receiver`, who is required to implement the {IERC3156FlashBorrower}
|
|
* interface. By the end of the flash loan, the receiver is expected to own
|
|
* amount + fee tokens and have them approved back to the token contract itself so
|
|
* they can be burned.
|
|
* @param receiver The receiver of the flash loan. Should implement the
|
|
* {IERC3156FlashBorrower-onFlashLoan} interface.
|
|
* @param token The token to be flash loaned. Only `address(this)` is
|
|
* supported.
|
|
* @param amount The amount of tokens to be loaned.
|
|
* @param data An arbitrary datafield that is passed to the receiver.
|
|
* @return `true` if the flash loan was successful.
|
|
*/
|
|
// This function can reenter, but it doesn't pose a risk because it always preserves the property that the amount
|
|
// minted at the beginning is always recovered and burned at the end, or else the entire function will revert.
|
|
// slither-disable-next-line reentrancy-no-eth
|
|
function flashLoan(
|
|
IERC3156FlashBorrower receiver,
|
|
address token,
|
|
uint256 amount,
|
|
bytes calldata data
|
|
) public virtual override returns (bool) {
|
|
require(amount <= maxFlashLoan(token), "ERC20FlashMint: amount exceeds maxFlashLoan");
|
|
uint256 fee = flashFee(token, amount);
|
|
_mint(address(receiver), amount);
|
|
require(
|
|
receiver.onFlashLoan(msg.sender, token, amount, fee, data) == _RETURN_VALUE,
|
|
"ERC20FlashMint: invalid return value"
|
|
);
|
|
address flashFeeReceiver = _flashFeeReceiver();
|
|
_spendAllowance(address(receiver), address(this), amount + fee);
|
|
if (fee == 0 || flashFeeReceiver == address(0)) {
|
|
_burn(address(receiver), amount + fee);
|
|
} else {
|
|
_burn(address(receiver), amount);
|
|
_transfer(address(receiver), flashFeeReceiver, fee);
|
|
}
|
|
return true;
|
|
}
|
|
}
|