Lesson 15: Expected event but reverted with reason ERC721: caller is not token owner or approved
#5183
-
Corresponding test function: const { deployments, ethers, network } = require("hardhat")
const { assert, expect } = require("chai")
const { developmentChains } = require("../../helper-hardhat-config")
!developmentChains.includes(network.name)
? describe.skip
: describe("NFTMarketplace test", function () {
let nftMarketplaceContract,
basicNftContract,
nftMarketplace,
basicNft,
deployer,
player
const PRICE = ethers.utils.parseEther("1")
const TOKEN_ID = ethers.BigNumber.from("0")
beforeEach(async function () {
accounts = await ethers.getSigners()
deployer = accounts[0]
player = accounts[1]
await deployments.fixture(["all"])
nftMarketplaceContract = await ethers.getContract("NftMarketplace")
nftMarketplace = await nftMarketplaceContract.connect(deployer)
basicNftContract = await ethers.getContract("BasicNft")
basicNft = await basicNftContract.connect(deployer)
await basicNft.mintNft()
await basicNft.approve(basicNftContract.address, TOKEN_ID)
})
describe("BuyItem function", function () {
it("Emits an event if nft is bought", async function () {
await nftMarketplace.listItem(basicNft.address, TOKEN_ID, PRICE)
nftMarketplace = nftMarketplaceContract.connect(player)
expect(
await nftMarketplace.buyItem(basicNft.address, TOKEN_ID, {
value: PRICE,
})
).to.emit("ItemBought")
})
}) function buyItem(
address nftAddress,
uint256 tokenId
) external payable nonReentrant isListed(nftAddress, tokenId) {
Listing memory listedItem = s_listings[nftAddress][tokenId];
if (msg.value < listedItem.price) {
revert NftMarketplace__SentLessThanListed(nftAddress, tokenId);
}
s_proceeds[listedItem.seller] += msg.value;
delete s_listings[nftAddress][tokenId];
IERC721(nftAddress).safeTransferFrom(
listedItem.seller,
msg.sender,
tokenId
);
emit ItemBought(msg.sender, nftAddress, tokenId, listedItem.price);
} Error:
|
Beta Was this translation helpful? Give feedback.
Answered by
alymurtazamemon
Mar 29, 2023
Replies: 1 comment
-
@Yana-Gupta Here you need to give approval to NFT marketplace; -await basicNft.approve(basicNftContract.address, TOKEN_ID)
+await basicNft.approve(nftMarketplaceContract.address, TOKEN_ID) |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
Yana-Gupta
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Yana-Gupta Here you need to give approval to NFT marketplace;