Hello Guys,
I am following the tutorial but using Reactjs… and much completed just getting the error in buying nfts… when meta mask popup is opening it is showing transaction error : exception thrown in contract code
after confirming the transaction , item is removed from EthNFTOwners but not from the EthNFTTransfers… and also i am not getting what is EthNFTTransfers if somebody clear my concept…will be grateful for that as I didn’t know anything about blockchain before landing on this project…
console logging this error after completing the transaction
this is contract code…
//Contract based on https://docs.openzeppelin.com/contracts/3.x/erc721
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol";
contract Marketplace {
struct ProductItem{
uint256 id;
address tokenAddress;
uint256 tokenId;
address payable seller;
uint256 price;
bool purchased;
}
ProductItem[] public products;
mapping(address => mapping(uint => bool)) activeItems;
event productCreated(uint256 id, uint256 tokenId, address tokenAddress, uint256 price);
event productSold(uint256 id, address buyer, uint256 price);
modifier OnlyItemOwner(address tokenAddress, uint256 tokenId){
IERC721 tokenContract = IERC721(tokenAddress);
require(tokenContract.ownerOf(tokenId) == msg.sender);
_;
}
modifier HasTransferApproval(address tokenAddress, uint256 tokenId){
IERC721 tokenContract = IERC721(tokenAddress);
require(tokenContract.getApproved(tokenId) == address(this));
_;
}
modifier ItemExists(uint256 id){
require(id < products.length && products[id].id == id, "Could not find Item");
_;
}
modifier IsForSale(uint256 id){
require(products[id].purchased == false, "Item is already sold");
_;
}
function itemAdded(uint256 tokenId, address tokenAddress, uint256 price) OnlyItemOwner(tokenAddress, tokenId) HasTransferApproval(tokenAddress, tokenId) external returns (uint256){
require(activeItems[tokenAddress][tokenId] == false, "Item is already up for sale");
uint256 newItemId = products.length;
products.push(ProductItem(newItemId, tokenAddress, tokenId, payable (msg.sender), price, false));
activeItems[tokenAddress][tokenId] = true;
assert(products[newItemId].id == newItemId);
emit productCreated(newItemId, tokenId, tokenAddress, price);
return newItemId;
}
function buyItem(uint256 id) payable external ItemExists(id) IsForSale(id) HasTransferApproval(products[id].tokenAddress, products[id].tokenId){
require(msg.value >= products[id].price, "Not enough Funds sent");
require(msg.sender != products[id].seller);
products[id].purchased = true;
activeItems[products[id].tokenAddress][products[id].tokenId] = false;
IERC721(products[id].tokenAddress).safeTransferFrom(products[id].seller, msg.sender, products[id].tokenId);
products[id].seller.transfer(msg.value);
emit productSold(id, msg.sender , products[id].price);
}
}
and this is buy function
export const buyItems = async (tokenId,price) => {
try {
const web3 = await Moralis.Web3.enable();
const currentUser = await Moralis.User.current();
const userAddress = currentUser.get('ethAddress')
const priceInWei = web3.utils.toWei(price,'ether')
window.marketplaceContract = new web3.eth.Contract(marketplaceABI, marketplaceRemixAddress);
const transfer = await window.marketplaceContract.methods.buyItem(tokenId).send({from:userAddress, value:priceInWei})
console.log('trf',transfer)
} catch (error) {
console.log(error)
}
}