How do I borrow a flash loan?

Mainnet smart contract addresses are listed in the Smart Contracts guide

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. Do NOT send mainnet tokens to these smart contracts as they will be lost!

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.

Argument

Description

Example

receiver

Address of the smart contract that will borrow and return a flash loan.

0x5b4b021d2abcf6116d782884bc5cd414027aeff2 (pre-deployed FlashBorrower Example)

token

The token address we want to borrow. In the example, you can find the USDT test token address.

0xbE6C02195A28d163A1DF2e05B0596416d326b190

amount

Amount of tokens we want to borrow. Note that USDT has 6 decimal places.

100000

data

Input data for the Flash Borrower smart contract.

0x00

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