ERC-6682 · Draft · ERC · 2023-03-12
ERC-6682: NFT Flashloans
A draft extension of the ERC-3156 flash-loan standard for NFTs: borrow a specific ERC-721 token, use it inside one transaction, return it and pay a fee in a separate token or ETH.
What problem does it solve?
A flash loan lends an asset with no collateral, on the condition that it comes back before the transaction ends; if not, everything reverts. ERC-3156 standardized this for fungible ERC-20 tokens only.
Many actions only check that you own an NFT at the moment of the call: claiming an airdrop, collecting staking rewards, or an in-game action such as harvesting resources. ERC-6682 defines how a lender can flash-lend a particular ERC-721 token so a contract can do such an action and give the NFT back in the same transaction.
How it works
Lenders implement the normal IERC3156FlashLender functions plus two new ones:
interface IERC6682 {
function flashFeeToken() external view returns (address); // address(0) = ETH
function availableForFlashLoan(address token, uint256 tokenId) external view returns (bool);
}
flashFeeToken()says which token the fee is paid in. NFTs are not divisible, so the fee is charged in another currency.availableForFlashLoan(token, tokenId)must returnfalse, not revert, when that NFT cannot be borrowed.- The ERC-3156
amountarguments carry the token id instead:flashFee(token, tokenId)andflashLoan(receiver, token, tokenId, data). maxFlashLoan(token)makes little sense for NFTs; the spec recommends returning1, meaning one NFT per call.
Flow: the lender transfers the NFT to the borrower contract, calls its onFlashLoan(initiator, token, tokenId, fee, data) (which must return keccak256("ERC3156FlashBorrower.onFlashLoan")), checks that it owns the NFT again, then collects the fee.
Sample code
A borrower that claims something with the NFT and returns it. It assumes the fee is paid in an ERC-20 that the lender pulls from the initiator, as in the spec's reference lender:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
interface IERC721 { function safeTransferFrom(address from, address to, uint256 id) external; }
interface IClaim { function claim(uint256 tokenId) external; }
contract NftBorrower {
address public immutable lender;
IClaim public immutable target;
constructor(address lender_, IClaim target_) { lender = lender_; target = target_; }
function onFlashLoan(address, address token, uint256 tokenId, uint256, bytes calldata)
external returns (bytes32)
{
require(msg.sender == lender, "untrusted lender");
target.claim(tokenId); // action that checks ownership
IERC721(token).safeTransferFrom(address(this), lender, tokenId); // give it back
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}
function onERC721Received(address, address, uint256, bytes calldata) external pure returns (bytes4) {
return this.onERC721Received.selector;
}
}
Things to know
- Status: Draft. The interface may still change; Security Considerations is marked "Needs discussion".
flashFeeToken()could point to a malicious contract. Borrowers should check it against an expected address before interacting with it.- Projects that hand out rewards based on a single ownership check are exposed to NFT flash loans. If that matters, use snapshots or minimum holding periods instead of
ownerOfat call time. - The borrower must be able to receive ERC-721 via
safeTransferFrom(implementonERC721Received).