Introduction

Hi, I'm cardene, a software engineer and technical writer focused on blockchain and AI.

I've also authored Japanese technical books on specification-driven development with LLMs and decentralized application (DApp) development.

I also build and publish a variety of tools, libraries, and open-source projects.

This article examines ERC6682, a proposal to extend the existing ERC3156 flash loan standard to support ERC721 NFTs.

This article translates, summarizes, and expands on the proposal below.

I have also covered a range of other EIPs.

Overview

ERC3156 is an Ethereum smart contract standard for flash loans, a mechanism for borrowing and repaying assets within a single transaction.

For more on ERC3156, see the following article.

[ERC3156] Implementing flash loans on Ethereum

The standard primarily supports flash loans of ERC20 tokens, the most common fungible token standard on Ethereum.

For more on ERC20, see the following article.

https://chaldene.net/erc20

As demand for flash loans of ERC721 tokens (unique, non-fungible tokens, or NFTs) grows, a new standard has been proposed to extend ERC3156 to support ERC721 NFT flash loans.

For more on ERC721, see the following article.

https://chaldene.net/erc721

Overview of ERC721 NFT flash loans

The ERC721 NFT flash loan standard proposes a way for flash lenders to lend NFTs to contracts. The key requirement is that the loan must be repaid within the same transaction. A fee may also be charged.

The standard allows users and smart contracts to borrow an NFT temporarily, use it for a particular operation (such as temporarily accessing an in-game ability, completing a collection, or interacting with a DeFi protocol), and return it and pay the fee before the transaction ends.

Basic process

  1. Loan request
    • A smart contract requests a flash loan of a particular NFT.
  2. NFT transfer
    • The flash lender temporarily transfers the requested NFT to the requesting contract.
  3. Executing the operation
    • The borrower uses the NFT to perform a particular operation, such as a trade or an in-game action.
  4. Loan repayment
    • After the operation, the borrower returns the NFT to the lender within the same transaction.
    • The agreed fee is also paid at this point.
  5. Transaction completion
    • If all conditions are met, the transaction succeeds.
    • If the NFT is not returned or the required fee is not paid, the transaction reverts and its state changes are rolled back through Ethereum's atomic execution.

Benefits and applications

The standard can improve NFT liquidity and accessibility. This is particularly useful for users and contracts that need temporary access to an expensive NFT without buying it outright. Possible applications include short-lived interactions with particular applications, complex financial operations, and testing specific in-game scenarios.

By enabling new uses of digital assets and improving interoperability between NFTs and DeFi, the extension could contribute to the development of the Ethereum ecosystem.

Motivation

The existing flash loan standard, ERC3156, is designed for ERC20 tokens. ERC20 tokens are fungible: units of a given token are interchangeable and share the same functionality. By contrast, ERC721 tokens are non-fungible, with individual token IDs that can have distinct values and attributes.

Why NFT flash loans are needed

This distinction calls for an extension to the existing flash loan standard to handle ERC721 NFTs. NFT flash loans can be useful for actions in which ownership of an NFT is checked.

  • Claiming airdrops
    • If an airdrop determines eligibility by current NFT ownership, an eligible NFT can be borrowed temporarily, used to claim the airdrop, and returned within the transaction, subject to the protocol's rules.
  • Claiming staking rewards
    • Where a reward mechanism permits claims based on temporary ownership of an NFT, the NFT can be borrowed to perform the claim and returned afterward. Staking requirements may make this unsuitable for some protocols.
  • In-game actions
    • An NFT can be borrowed temporarily for an in-game action that requires ownership of that NFT, such as harvesting a particular resource.

Functionality and applications

NFT flash loans can offer the following benefits.

  • Improved liquidity
    • Users can access expensive NFTs temporarily for a particular purpose without purchasing them outright.
  • New financial strategies
    • New types of NFT-based DeFi strategies become possible.
    • For example, a strategy may seek to realize value from an NFT during the brief period in which it is borrowed, provided the NFT can be returned within the same transaction.
  • Expanded in-game strategies
    • Temporarily borrowing NFTs needed for particular in-game actions may create new strategic and gameplay opportunities.

NFT flash loans create new possibilities for actions that depend on NFT ownership. This could improve NFT liquidity and expand the ways digital assets are used. Implementing and operating the new standard securely, however, requires addressing technical and security challenges.

Specification

ts
pragma solidity ^0.8.19;

interface IERC6682 {
    /// @dev The address of the token used to pay flash loan fees.
    function flashFeeToken() external view returns (address);

    /// @dev Whether or not the NFT is available for a flash loan.
    /// @param token The address of the NFT contract.
    /// @param tokenId The ID of the NFT.
    function availableForFlashLoan(address token, uint256 tokenId) external view returns (bool);
}

The contract interface above defines IERC6682, part of the proposed standard for ERC721 NFT flash loans. Through this interface, flash lenders can indicate NFT availability and specify which token is used to pay flash loan fees.

The flashFeeToken function

Returns the address of the token used to pay flash loan fees. If fees are payable in ETH, the function must return address(0). In this interface, address(0) is a special sentinel value representing native ETH, rather than an ERC20 token address.

The availableForFlashLoan function

Returns whether a particular NFT, identified by the token address and tokenId, is available for a flash loan. If the specified NFT is not currently available, the function must return false. The function is designed to return false rather than revert when the NFT is unavailable.

Requirement to implement IERC3156FlashLender

Contracts implementing IERC6682 must also implement IERC3156FlashLender. ERC3156 provides the existing standard for flash loans of ERC20 tokens, and the NFT flash loans defined by IERC6682 build on it. This provides a consistent interface for flash lending and promotes compatibility when supporting both ERC20 tokens and ERC721 NFTs.

The proposed interface is a step toward making NFTs more liquid and accessible. By temporarily borrowing particular NFTs through flash loans, users may enable new types of applications and financial strategies. Implementations must also follow the rules concerning fee payments, availability checks, and other requirements.

Additional considerations

An extension to the existing ERC3156 flash loan standard should introduce as few changes as possible to support NFTs. ERC3156 is already widely adopted, and only a small number of changes are needed to support NFT flash loans. This approach adds functionality while retaining compatibility with existing infrastructure where possible.

Paying fees

For NFT flash loans in particular, it is generally desirable to pay fees in a currency other than the NFT being lent. NFTs are non-fungible and generally cannot be divided into smaller units of the same token. For example, a lender might charge 0.1 ETH for each NFT flash loan. The interface therefore needs methods that allow borrowers to determine both the fee for a particular NFT and the currency in which the fee must be paid.

Determining fees and payment currencies

To meet this requirement, the interface needs the following functionality.

  • Fee queries
    • A method that allows borrowers to determine the flash loan fee for a particular NFT in advance.
  • Payment currency identification
    • A method identifying the currency used to pay the fee.
    • This accommodates fees denominated in ETH or different ERC20 tokens.

Benefits of extending the existing standard

The main benefit of extending ERC3156 is that developers and projects can reduce learning and integration costs by building on a widely adopted standard. Existing ERC3156-compatible tools and infrastructure can also help developers implement and deploy the new NFT flash loan functionality.

Extending the existing flash loan standard to support NFTs is a way to improve their accessibility and liquidity within the Ethereum ecosystem. Straightforward changes, including the fee payment interface, could support further development and experimentation in the NFT market.

Backwards compatibility

The proposal retains the ERC3156 interface, with a caveat concerning the semantics of maxFlashLoan. ERC3156 primarily targets flash loans of ERC20 fungible tokens, and maxFlashLoan indicates the maximum quantity of a token that can be lent. Because ERC721 tokens are non-fungible, that quantity does not convey the availability of individual NFT IDs.

The role of maxFlashLoan

The maxFlashLoan method defined by ERC3156 returns the maximum amount of a specified token available for lending. This is meaningful for ERC20 tokens but does not directly express NFT availability, since each NFT is identified individually.

Handling maxFlashLoan for NFT flash loans

For NFT flash loans, the proposal recommends returning 1 from maxFlashLoan. This reflects the fact that each flash loan call handles one NFT. If the contract also supports ERC20 flash loans, it can return an appropriate amount for those tokens instead. This allows a lender to support flash loans for both ERC20 and ERC721 assets through the same interface.

Preserving backwards compatibility

The proposal introduces minimal changes to the existing ERC3156 interface to enable NFT flash loans while preserving compatibility with its existing functionality. Allowing contracts that already support ERC20 flash loans to add NFT support without changing the signature of maxFlashLoan is important for maintaining compatibility with existing infrastructure.

Developers can extend existing flash loan implementations to support NFTs while retaining compatibility with existing contracts and tools where the semantics permit it. Preserving compatibility while addressing new technical requirements is an important aspect of evolving blockchain standards.

Implementation

js
// SPDX-License-Identifier: CC0-1.0
pragma solidity ^0.8.19;

import "../interfaces/IERC20.sol";
import "../interfaces/IERC721.sol";
import "../interfaces/IERC3156FlashBorrower.sol";
import "../interfaces/IERC3156FlashLender.sol";
import "../interfaces/IERC6682.sol";

contract ExampleFlashLender is IERC6682, IERC3156FlashLender {
    uint256 internal _feePerNFT;
    address internal _flashFeeToken;

    constructor(uint256 feePerNFT_, address flashFeeToken_) {
        _feePerNFT = feePerNFT_;
        _flashFeeToken = flashFeeToken_;
    }

    function flashFeeToken() public view returns (address) {
        return _flashFeeToken;
    }

    function availableForFlashLoan(address token, uint256 tokenId) public view returns (bool) {
        // return if the NFT is owned by this contract
        try IERC721(token).ownerOf(tokenId) returns (address result) {
            return result == address(this);
        } catch {
            return false;
        }
    }

    function flashFee(address token, uint256 tokenId) public view returns (uint256) {
        return _feePerNFT;
    }

    function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 tokenId, bytes calldata data)
        public
        returns (bool)
    {
        // check that the NFT is available for a flash loan
        require(availableForFlashLoan(token, tokenId), "IERC6682: NFT not available for flash loan");

        // transfer the NFT to the borrower
        IERC721(token).safeTransferFrom(address(this), address(receiver), tokenId);

        // calculate the fee
        uint256 fee = flashFee(token, tokenId);

        // call the borrower
        bool success =
            receiver.onFlashLoan(msg.sender, token, tokenId, fee, data) == keccak256("ERC3156FlashBorrower.onFlashLoan");

        // check that flashloan was successful
        require(success, "IERC6682: Flash loan failed");
        
        // check that the NFT was returned by the borrower
        require(IERC721(token).ownerOf(tokenId) == address(this), "IERC6682: NFT not returned by borrower");

        // transfer the fee from the borrower
        IERC20(flashFeeToken()).transferFrom(msg.sender, address(this), fee);

        return success;
    }

    function maxFlashLoan(address token) public pure override returns (uint256) {
        // if a contract also supports flash loans for ERC20 tokens then it can
        // return some value here instead of 1
        return 1;
    }

    function onERC721Received(address, address, uint256, bytes memory) public returns (bytes4) {
        return this.onERC721Received.selector;
    }
}

flashFeeToken

javascript
function flashFeeToken() public view returns (address) {
    return _flashFeeToken;
}

Overview

Returns the address of the token used to pay flash loan fees.

Details

This function returns the address of the ERC20 token used to pay the fee. When the fee is denominated in ETH, the interface uses address(0) as the return value. The example's actual fee collection code, however, only implements an ERC20 transfer.

Return value

  • address
    • The address of the ERC20 token used for payment, or the native ETH sentinel.

availableForFlashLoan

javascript
function availableForFlashLoan(address token, uint256 tokenId) public view returns (bool) {
    // return if the NFT is owned by this contract
    try IERC721(token).ownerOf(tokenId) returns (address result) {
        return result == address(this);
    } catch {
        return false;
    }
}

Overview

Checks whether the specified NFT is available for a flash loan.

Details

This function checks whether the NFT identified by the ERC721 contract address (token) and token ID (tokenId) is owned by this contract. It returns true if the lender owns the NFT, and false otherwise, including when the ownership query reverts. An NFT not owned by the lender is considered unavailable.

Parameters

  • token
    • The address of the ERC721 token contract.
  • tokenId
    • The ID of the NFT to check.

Return value

  • bool
    • Whether the specified NFT is available for a flash loan.

flashFee

javascript
function flashFee(address token, uint256 tokenId) public view returns (uint256) {
    return _feePerNFT;
}

Overview

Calculates the flash loan fee for the specified NFT.

Details

This function returns the fee charged for a flash loan. The fee is defined by the contract's internal variable _feePerNFT.

Parameters

  • token
    • The address of the ERC721 token contract (unused in this example).
  • tokenId
    • The NFT ID (unused in this example).

Return value

  • uint256
    • The flash loan fee.

flashLoan

javascript
function flashLoan(IERC3156FlashBorrower receiver, address token, uint256 tokenId, bytes calldata data)
    public
    returns (bool)
{
    // check that the NFT is available for a flash loan
    require(availableForFlashLoan(token, tokenId), "IERC6682: NFT not available for flash loan");

    // transfer the NFT to the borrower
    IERC721(token).safeTransferFrom(address(this), address(receiver), tokenId);

    // calculate the fee
    uint256 fee = flashFee(token, tokenId);

    // call the borrower
    bool success =
        receiver.onFlashLoan(msg.sender, token, tokenId, fee, data) == keccak256("ERC3156FlashBorrower.onFlashLoan");

    // check that flashloan was successful
    require(success, "IERC6682: Flash loan failed");
    
    // check that the NFT was returned by the borrower
    require(IERC721(token).ownerOf(tokenId) == address(this), "IERC6682: NFT not returned by borrower");

    // transfer the fee from the borrower
    IERC20(flashFeeToken()).transferFrom(msg.sender, address(this), fee);

    return success;
}

Overview

Executes a flash loan of the specified NFT.

Details

This function carries out the flash loan workflow. It first checks whether the specified NFT is available, then transfers it to the borrower. The borrower must return the NFT, and the required fee must be paid. The fee is calculated by flashFee and denominated in the token identified by flashFeeToken. In this example, the fee is collected from msg.sender. If all operations succeed, the function returns true.

Parameters

  • receiver
    • The borrower contract implementing the IERC3156FlashBorrower interface.
  • token
    • The address of the ERC721 token contract.
  • tokenId
    • The ID of the NFT being borrowed.
  • data
    • Arbitrary data supplied for the borrower's flash loan callback.

Return value

  • bool
    • Whether the flash loan operation succeeded.

maxFlashLoan

javascript
function maxFlashLoan(address token) public pure override returns (uint256) {
    return 1;
}

Overview

Returns the maximum number of NFTs that can be lent per flash loan.

Details

This function indicates the maximum number of NFTs that can be borrowed in one flash loan call. Because each call identifies one NFT, the example always returns 1.

Parameters

  • token
    • The address of the ERC721 token contract (unused in this example).

Return value

  • uint256
    • The maximum number of NFTs that can be borrowed per flash loan.

onERC721Received

javascript
function onERC721Received(address, address, uint256, bytes memory) public returns (bytes4) {
    return this.onERC721Received.selector;
}

Overview

A receiver hook invoked when an ERC721 token is safely transferred to this contract.

Details

The function confirms that the contract accepts the incoming ERC721 token. Under ERC721, a safe transfer to a contract invokes this hook, which must return the expected selector to confirm receipt.

Return value

  • bytes4
    • The selector indicating acceptance of the safe transfer.

Security considerations

Borrowers must exercise caution when making calls to the address returned by flashFeeToken, because it could identify a malicious contract. If the returned address belongs to a malicious contract, interacting with it may expose the borrower to unauthorized or unexpected operations. For example, a malicious fee token contract may behave unexpectedly when the borrower attempts to pay or authorize a fee.

One way to address this issue is to verify that the address returned by flashFeeToken matches the address supplied or expected by the user. This helps ensure that borrowers interact only with the intended fee token contract and reduces their exposure to malicious contracts.

Points for consideration

The following additional approaches merit consideration.

  • Using trusted lists
    • A flash lending platform could maintain a list of trusted fee tokens and check whether the address returned by flashFeeToken is included.
  • Verifying contracts
    • Review the published code of the contract returned by flashFeeToken to verify that its behavior is legitimate.
    • This may include examining its previous on-chain transactions and its reputation in the community.
  • User education
    • Help users evaluate risks themselves and exercise caution when encountering suspicious addresses.
    • For example, provide guidelines for using flash loans or a checklist for identifying malicious contracts.

Securing interactions with the address returned by flashFeeToken therefore calls for a combination of approaches. This requires effort not only from individual users and developers but also from the platform as a whole.

Citation

out.eth (@outdoteth), "ERC-6682: NFT Flashloans [DRAFT]," Ethereum Improvement Proposals, no. 6682, March 2023. [Online serial]. Available: https://eips.ethereum.org/EIPS/eip-6682.

Conclusion

This article covered ERC6682, a proposal to extend the existing ERC3156 flash loan standard to support ERC721 NFT flash loans. I hope you found it useful.

If you have any questions, feel free to contact me via Twitter DM.

Twitter @cardene777

I also publish on other platforms, which you can find below.

https://chaldene.net/