ERC721NonexistentToken(1)错误求助:NFT托管合约测试问题
NFT开发测试报错排查:ERC721NonexistentToken(1)
部署RealEstate(ERC721合约)与Escrow托管合约后,执行Hardhat测试时报错:Error: VM Exception while processing transaction: reverted with custom error 'ERC721NonexistentToken(1)'。调用totalSupply返回1,说明awardItem成功,但调用approve(tokenId=1)时触发该错误。
相关合约与测试代码
Escrow.sol
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; interface IERC721 { function transferFrom( address _from, address _to, uint256 _tokenId ) external; } contract Escrow { address public nftAddress; address payable public seller; address public inspector; //检察员 address public lender; //贷方 constructor( address _nftAddress, address payable _seller, address _inspector, address _lender ) { nftAddress = _nftAddress; seller = _seller; inspector = _inspector; lender = _lender; } function list(uint256 _nftID) public { IERC721(nftAddress).transferFrom(msg.sender, address(this), _nftID); } }
RealEstate.sol
// SPDX-License-Identifier: MIT pragma solidity ^0.8.20; import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; contract RealEstate is ERC721URIStorage { uint256 private _tokenIds; constructor() ERC721("Real Estate", "Real") {} function awardItem(string memory tokenURI) public returns (uint256) { uint256 tokenId = _tokenIds++; _mint(msg.sender, tokenId); _setTokenURI(tokenId, tokenURI); return tokenId; } function totalSupply() public view returns (uint256) { return _tokenIds; } }
test.js
const { expect, assert } = require("chai"); const { ethers } = require("hardhat"); const tokens = (n) => { return ethers.utils.parseUnits(n.toString(), "ether"); }; describe("Escrow", function () { let buyer, seller, lender, inspector; let realEstate, escrow; beforeEach(async function () { [buyer, seller, inspector, lender] = await ethers.getSigners(); const RealEstate = await ethers.getContractFactory("RealEstate"); realEstate = await RealEstate.deploy(); let transaction = await realEstate .connect(seller) .awardItem( "https://ipfs.io/ipfs/QmUsuRJyRUmeHzZxes5FRMkc4mjx35HbaTzHzzWoiRdT5G" ); const receipt = await transaction.wait(); if (receipt.status === 1) { console.log("AwardItem transaction successful"); } else { console.error("AwardItem transaction failed"); } const Escrow = await ethers.getContractFactory("Escrow"); escrow = await Escrow.deploy( realEstate.target, seller.address, inspector.address, lender.address ); transaction = await realEstate.connect(seller).approve(escrow.target, 1); await transaction.wait(); }); describe("Deploy!!", function () { it("return nftAddress", async function () { const result = await escrow.nftAddress(); assert.equal(result, realEstate.target); }); it("return sellerAddress", async function () { assert.equal(await escrow.seller(), seller.address); }); it("return inspectorAddress", async function () { assert(await escrow.inspector(), inspector.address); }); it("return lenderAddress", async function () { assert(await escrow.lender(), lender.address); }); it("check tokenId", async function () { const id = await realEstate.totalSupply(); console.log(id); }); }); });
测试错误输出
yarn hardhat test yarn run v1.22.21 warning package.json: No license field $ D:\blockchain\hh-fcc\hotelRoom\node_modules\.bin\hardhat test Escrow Deploy!! AwardItem transaction successful Total Supply After AwardItem: 1 1) "before each" hook for "return nftAddress" 0 passing (3s) 1 failing 1) Escrow "before each" hook for "return nftAddress": Error: VM Exception while processing transaction: reverted with custom error 'ERC721NonexistentToken(1)' at RealEstate._requireOwned (@openzeppelin/contracts/token/ERC721/ERC721.sol:451)
明明totalSupply返回1,证明已成功铸造NFT,但调用approve(tokenId=1)时触发ERC721NonexistentToken(1)错误,恳请帮忙排查原因。
内容的提问来源于stack exchange,提问作者tan qinrun
相关产品推荐
相关产品推荐

