How do I borrow a flash loan?

This guide demonstrates how to borrow a flash loan from Equalizer Vaults in a few simple steps. For the demo purpose, we deployed a simple Flash Borrower Example smart contract on the Rinkeby Testnet.

Flash Borrower Example doesn't perform any business logic. We invite you to build your own trading logic using Equalizer Flash loans.

For this example you need

Open the Flash Loan Provider Flash loan provider is a smart contract developed by Equalizer that enables you to borrow flash loans. You can inspect on Etherscan the Flash Loan Provider:

Connect your wallet Connect your wallet by clicking on Connect to Web3 and select one of the supported wallets (in our example, Metamask)

A wallet window will open and prompt you to connect with your wallet.

Borrow a flash loan Now you can borrow a flash loan. Click on the flashLoan function so you will be able to enter the function parameters.

In the table below, we explain the arguments and provide some example inputs you can test. All fields are mandatory.

Click on Write that will open your wallet and prompt you to sign the transaction. After the transaction is successfully processed, you can inspect the transaction. Example: https://rinkeby.etherscan.io/tx/0xc05e3a4dd0c59aa4de055e8f8f727eaa1f6da1a0c21f2b42fd427a479553b561

Congratulations, you just

  • borrowed 0.1 USDT from the Equalizer Vaults

  • Returned 0.1 USDT with 0.05% fee

Appendix - Flash Borrower Example smart contract

Below we present the Flash Borrower Example smart contract used in the example. The smart contract only borrows and returns funds. Business logic must be provided by the user.

// SPDX-License-Identifier: MIT
pragma solidity 0.8.4;

interface IERC20 {
    function approve(address spender, uint256 amount) external returns (bool);
}

interface IERC3156FlashBorrower {
    /**
     * @dev Receive a flash loan.
     * @param initiator The initiator of the loan.
     * @param token The loan currency.
     * @param amount The amount of tokens lent.
     * @param fee The additional amount of tokens to repay.
     * @param data Arbitrary data structure, intended to contain user-defined parameters.
     * @return The keccak256 hash of "ERC3156FlashBorrower.onFlashLoan"
     */
    function onFlashLoan(
        address initiator,
        address token,
        uint256 amount,
        uint256 fee,
        bytes calldata data
    ) external returns (bytes32);
}

/*
*  FlashBorrowerExample is a simple smart contract that enables
*  to borrow and returns a flash loan.
*/
contract FlashBorrowerExample is IERC3156FlashBorrower {
    uint256 MAX_INT = 0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff;

    // @dev ERC-3156 Flash loan callback
    function onFlashLoan(
        address initiator,
        address token,
        uint256 amount,
        uint256 fee,
        bytes calldata data
    ) external override returns (bytes32) {
        
        // Set the allowance to payback the flash loan
        IERC20(token).approve(msg.sender, MAX_INT);
        
        // Build your trading business logic here
        // e.g., sell on uniswapv2
        // e.g., buy on uniswapv3

        // Return success to the lender, he will transfer get the funds back if allowance is set accordingly
        return keccak256('ERC3156FlashBorrower.onFlashLoan');
    }
}

Last updated