ERC-3156: Flash Loans (FlashLender) Smart Contract Module in Bunzz

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

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/40yvFvu

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],
"FlashLender: Unsupported currency"
);
uint256 fee = _flashFee(token, amount);
require(
IERC20(token).transfer(address(receiver), amount),
"FlashLender: Transfer failed"
);
require(
receiver.onFlashLoan(msg.sender, token, amount, fee, data) == CALLBACK_SUCCESS,
"FlashLender: Callback failed"
);
require(
IERC20(token).transferFrom(address(receiver), address(this), amount + fee),
"FlashLender: Repay failed"
);
return true;
}

Functions

#WRITE

  • transferOwnership
  • flashLoan
  • renounceOwnership

#READ

  • supportedTokens
  • CALLBACK_SUCCESS
  • flashFee
  • maxFlashLoan
  • owner
  • fee

You can access this Module and the code here: https://bit.ly/40yvFvu

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:

Leave a Comment

Your email address will not be published. Required fields are marked *