Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ERC20FlashMintUpgradeable

Inherits: Initializable, ERC20Upgradeable, IERC3156FlashLender

Implementation of the ERC-3156 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}. NOTE: When this extension is used along with the {ERC20Capped} or {ERC20Votes} extensions, {maxFlashLoan} will not correctly reflect the maximum that can be flash minted. We recommend overriding {maxFlashLoan} so that it correctly reflects the supply cap.

State Variables

RETURN_VALUE

bytes32 private constant RETURN_VALUE = keccak256("ERC3156FlashBorrower.onFlashLoan");

Functions

__ERC20FlashMint_init

function __ERC20FlashMint_init() internal onlyInitializing;

__ERC20FlashMint_init_unchained

function __ERC20FlashMint_init_unchained() internal onlyInitializing;

maxFlashLoan

Returns the maximum amount of tokens available for loan.

function maxFlashLoan(address token) public view virtual returns (uint256);

Parameters

NameTypeDescription
tokenaddressThe address of the token that is requested.

Returns

NameTypeDescription
<none>uint256The amount of token that can be loaned. NOTE: This function does not consider any form of supply cap, so in case it's used in a token with a cap like {ERC20Capped}, make sure to override this function to integrate the cap instead of type(uint256).max.

flashFee

Returns the fee applied when doing flash loans. This function calls the _flashFee function which returns the fee applied when doing flash loans.

function flashFee(address token, uint256 value) public view virtual returns (uint256);

Parameters

NameTypeDescription
tokenaddressThe token to be flash loaned.
valueuint256The amount of tokens to be loaned.

Returns

NameTypeDescription
<none>uint256The fees applied to the corresponding flash loan.

_flashFee

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.

function _flashFee(address token, uint256 value) internal view virtual returns (uint256);

Parameters

NameTypeDescription
tokenaddressThe token to be flash loaned.
valueuint256The amount of tokens to be loaned.

Returns

NameTypeDescription
<none>uint256The fees applied to the corresponding flash loan.

_flashFeeReceiver

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.

function _flashFeeReceiver() internal view virtual returns (address);

Returns

NameTypeDescription
<none>addressThe address for which the flash fee will be sent to.

flashLoan

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 value + fee tokens and have them approved back to the token contract itself so they can be burned.

function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 value, bytes calldata data)
    public
    virtual
    returns (bool);

Parameters

NameTypeDescription
receiverIERC3156FlashBorrowerThe receiver of the flash loan. Should implement the IERC3156FlashBorrower-onFlashLoan interface.
tokenaddressThe token to be flash loaned. Only address(this) is supported.
valueuint256The amount of tokens to be loaned.
databytesAn arbitrary datafield that is passed to the receiver.

Returns

NameTypeDescription
<none>booltrue if the flash loan was successful.

Errors

ERC3156UnsupportedToken

The loan token is not valid.

error ERC3156UnsupportedToken(address token);

ERC3156ExceededMaxLoan

The requested loan exceeds the max loan value for token.

error ERC3156ExceededMaxLoan(uint256 maxLoan);

ERC3156InvalidReceiver

The receiver of a flashloan is not a valid IERC3156FlashBorrower-onFlashLoan implementer.

error ERC3156InvalidReceiver(address receiver);