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

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

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/3GfPhMN

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 ERC-3156 Flash loan callback
function onFlashLoan(
address initiator,
address token,
uint256 amount,
uint256 fee,
bytes calldata data
) external override returns(bytes32) {
require(
msg.sender == address(lender),
"FlashBorrower: Untrusted lender"
);
require(
initiator == address(this),
"FlashBorrower: Untrusted loan initiator"
);
(Action action) = abi.decode(data, (Action));
if (action == Action.NORMAL) {
// do one thing
} else if (action == Action.OTHER) {
// do another
}
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}

/// @dev Initiate a flash loan
function flashBorrow(
address token,
uint256 amount
) public {
bytes memory data = abi.encode(Action.NORMAL);
uint256 _allowance = IERC20(token).allowance(address(this), address(lender));
uint256 _fee = lender.flashFee(token, amount);
uint256 _repayment = amount + _fee;
IERC20(token).approve(address(lender), _allowance + _repayment);
lender.flashLoan(this, token, amount, data);
}

Functions

#WRITE

  • flashBorrow
  • flashBorrowAndReenter
  • onFlashLoan
  • transferOwnership
  • approveRepayment
  • flashBorrowAndSteal
  • renounceOwnership

#READ

  • flashAmount
  • flashBalance
  • flashFee
  • flashInitiator
  • flashToken
  • owner

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

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: