ERC3156 Flash Loans (Flash Minter) Smart Contract Module in Bunzz

The FlashMinter is a submodule of ERC-3156: Flash Loans module and an extension of {ERC20} that allows flash minting.

A flash loan is a smart contract transaction in which a lender smart contract lends assets to a borrower smart contract with the condition that the assets are returned, plus an optional fee, before the end of the transaction.

The ERC-3156: Flash Loans specifies interfaces for lenders to accept flash loan requests, and for borrowers to take temporary control of the transaction within the lender execution.

The process for the safe execution of flash loans is also specified.

You can access this Module and the code here: https://bit.ly/3UcRDC4

How to Use

A lender MUST implement the {IERC3156FlashLender} interface.

A receiver of flash loans MUST implement the {IERC3156FlashBorrower} interface:

  • Flash Borrower Reference Implementation => FlashBorrower.sol (ERC-3156: Flash Loans Submodule)
  • Flash Mint Reference Implementation => FlashMinter.sol (ERC-3156: Flash Loans Submodule)
  • Flash Loan Reference Implementation => FlashRender.sol (ERC-3156: Flash Loans Submodule)

You can implement the {flashLoan} function as follows:

/**
* @dev Loan `amount` tokens to `receiver`, and takes it back plus a `flashFee` after the callback.
* @param receiver The contract receiving the tokens, needs to implement the `onFlashLoan(address user, uint256 amount, uint256 fee, bytes calldata)` interface.
* @param token The loan currency.
* @param amount The amount of tokens lent.
* @param data A data parameter to be passed on to the `receiver` for any custom use.
*/
function flashLoan(
IERC3156FlashBorrower receiver,
address token,
uint256 amount,
bytes calldata data
) external override returns(bool) {
require(
supportedTokens[token],
"FlashMinter: Unsupported currency"
);
uint256 fee = _flashFee(token, amount);
_mint(address(receiver), amount);
require(
receiver.onFlashLoan(msg.sender, token, amount, fee, data) == CALLBACK_SUCCESS,
"FlashMinter: Callback failed"
);
uint256 _allowance = allowance(address(receiver), address(this));
require(
_allowance >= (amount + fee),
"FlashMinter: Repay not approved"
);
_approve(address(receiver), address(this), _allowance - (amount + fee));
_burn(address(receiver), amount + fee);
return true;
}

Functions

#WRITE

  • approve
  • flashLoan
  • transferOwnership
  • renounceOwnership
  • transferFrom
  • increaseAllowance
  • transfer
  • decreaseAllowance

#READ

  • balanceOf
  • fee
  • CALLBACK_SUCCESS
  • totalSupply
  • maxFlashLoan
  • name
  • owner
  • allowance
  • decimals
  • flashFee
  • symbol

You can access this Module and the code here: https://bit.ly/3UcRDC4

If you still haven’t signed up to Bunzz, what are you waiting for?

Sign up here https://app.bunzz.dev/signup and get your smart contracts deployed in 5 minutes through our great GUI.

Share this article: