ERC-6682 NFT flash loans (NFT Flashloans)
Draft · Vaults & DeFi · created 2023-03-12
Extends ERC-3156, the flash loan standard for ERC-20, to define a minimal interface for borrowing an NFT (ERC-721) within a single transaction and returning it along with a fee.
Why it is needed
The existing flashloan standard ERC-3156 only supports ERC-20 tokens. NFTs (ERC-721) are each unique, so they cannot be split by quantity, and they differ enough from ERC-20 that the existing standard cannot be used as is. Being able to borrow an NFT for just the duration of one transaction would be useful for any action that checks NFT ownership, such as claiming an airdrop, claiming staking rewards, or receiving resources harvested in a game. But without a common specification, borrowers have no standard way to find out which NFTs are currently available for lending or which token the fee must be paid in. This standard leaves the widely used ERC-3156 as unchanged as possible and supports NFT flashloans with minimal changes by adding just two lookup functions.
What to implement
function flashFeeToken() external view returns (address);: Returns the address of the token used to pay the flashloan fee. If the fee is paid in ETH, returns address(0).function availableForFlashLoan(address token, uint256 tokenId) external view returns (bool);: Returns whether the tokenId NFT of the token contract is currently available for flashloan borrowing.
In depth
An extension standard of ERC-3156
Rather than creating a new standard from scratch, this standard is designed as an extension of the existing flashloan standard, ERC-3156. Implementers MUST implement IERC3156FlashLender alongside IERC6682. The source explains that this is because ERC-3156 is already widely adopted and the changes needed to support NFTs are small, making the additions in this proposal the simplest possible extension to the existing standard.
flashFeeToken: looking up the fee payment token
The flashFeeToken function MUST return the address of the token used to pay the flashloan fee. If the fee is paid in ETH, it MUST return address(0). Since NFTs themselves are not always divisible, fees are usually paid in a currency separate from the borrowed NFT. For example, if a lender charges a fee of 0.1 ETH per NFT, the interface must provide a way for the borrower to find out the fee rate and payment currency for each NFT.
availableForFlashLoan: looking up lending availability
The availableForFlashLoan function MUST return whether the tokenId of the token contract is currently available for flashloan borrowing. If it is not currently available, it MUST return false rather than reverting. The reference implementation checks ownership of the NFT via IERC721's ownerOf, catching even failed calls with try/catch to return false.
Backwards compatibility with maxFlashLoan
This standard is fully backwards compatible with ERC-3156 except for the maxFlashLoan method. maxFlashLoan represents the quantity available for lending, a concept that does not fit NFTs since they are not fungible. Still, since it is part of the existing standard, it cannot be removed without breaking compatibility. For this reason, implementations that do not intend to support ERC-20 flashloans are RECOMMENDED to always return 1 from maxFlashLoan. The value 1 reflects the fact that only one NFT can be borrowed per call to flashLoan.
Validation order of flashLoan in the reference implementation
The reference implementation's flashLoan works in the following order. First it uses require to check availability via availableForFlashLoan, then sends the NFT to the borrower via safeTransferFrom. It computes the fee with flashFee, calls the borrower's onFlashLoan, and determines success by checking whether the return value matches keccak256("ERC3156FlashBorrower.onFlashLoan"). It then checks ownerOf again to confirm the NFT has been returned to itself, and finally collects the fee from msg.sender via transferFrom on the flashFeeToken token.
A proposal still in Draft status
This proposal is in Draft status as of the source. The content may change going forward, and the source's Security Considerations also notes that further discussion is needed. When implementing or referencing it, it is advisable to check the latest source.
Things to watch out for
- flashFeeToken could point to a malicious contract: There is a possibility that the flashFeeToken method returns the address of a malicious contract. A borrower intending to call the returned address should carefully verify that the contract is not malicious. One approach is to verify that the address returned by flashFeeToken matches an address the user has input.