functionmaxFlashLoan(addresstoken)publicpureoverridereturns(uint256){// if a contract also supports flash loans for ERC20 tokens then it can
// 如果合约还支持 ERC20 代币的闪电贷,则它可以
// return some value here instead of 1
// 在这里返回一些值而不是 1
return1;}
参考实现
// SPDX-License-Identifier: CC0-1.0
pragmasolidity^0.8.19;import"../interfaces/IERC20.sol";import"../interfaces/IERC721.sol";import"../interfaces/IERC3156FlashBorrower.sol";import"../interfaces/IERC3156FlashLender.sol";import"../interfaces/IERC6682.sol";contractExampleFlashLenderisIERC6682,IERC3156FlashLender{uint256internal_feePerNFT;addressinternal_flashFeeToken;constructor(uint256feePerNFT_,addressflashFeeToken_){_feePerNFT=feePerNFT_;_flashFeeToken=flashFeeToken_;}functionflashFeeToken()publicviewreturns(address){return_flashFeeToken;}functionavailableForFlashLoan(addresstoken,uint256tokenId)publicviewreturns(bool){// return if the NFT is owned by this contract
// 如果 NFT 由此合约拥有,则返回
tryIERC721(token).ownerOf(tokenId)returns(addressresult){returnresult==address(this);}catch{returnfalse;}}functionflashFee(addresstoken,uint256tokenId)publicviewreturns(uint256){return_feePerNFT;}functionflashLoan(IERC3156FlashBorrowerreceiver,addresstoken,uint256tokenId,bytescalldatadata)publicreturns(bool){// check that the NFT is available for a flash loan
// 检查 NFT 是否可用于闪电贷
require(availableForFlashLoan(token,tokenId),"IERC6682: NFT not available for flash loan");// transfer the NFT to the borrower
// 将 NFT 转移给借款人
IERC721(token).safeTransferFrom(address(this),address(receiver),tokenId);// calculate the fee
// 计算费用
uint256fee=flashFee(token,tokenId);// call the borrower
// 调用借款人
boolsuccess=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
// 检查 NFT 是否已由借款人退回
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);returnsuccess;}functionmaxFlashLoan(addresstoken)publicpureoverridereturns(uint256){// if a contract also supports flash loans for ERC20 tokens then it can
// 如果合约还支持 ERC20 代币的闪电贷,则它可以
// return some value here instead of 1
// 在这里返回一些值而不是 1
return1;}functiononERC721Received(address,address,uint256,bytesmemory)publicreturns(bytes4){returnthis.onERC721Received.selector;}}