ALERT: Transaction Error. Exception thrown in contract code

contract

pragma solidity ^0.8.0;

import "../../node_modules/@openzeppelin/contracts/token/ERC721/IERC721.sol";
import "../../node_modules/@openzeppelin/contracts/utils/Counters.sol/";

contract MarketContract {
    struct AuctionItem {
        uint256 id;
        address tokenAddress;
        uint256 tokenId;
        address payable seller;
        uint256 askingPrice;
        bool isSold;
    }

    AuctionItem[] public itemsForSale;
    mapping(address => mapping(uint256 => bool)) activeItems;

    event itemAdded(
        uint256 id,
        uint256 tokenId,
        address tokenAddress,
        uint256 askingPrice
    );
    event itemSold(uint256 id, address buyer, uint256 askingPrice);

    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 < itemsForSale.length && itemsForSale[id].id == id,"Item not found");
        _;
    }

    modifier IsForSale(uint256 id) {
        require(itemsForSale[id].isSold == false, "Item already sold");
        _;
    }

    function addItemToMarket(uint256 tokenId,address tokenAddress,uint256 askingPrice) external OnlyItemOwner(tokenAddress, tokenId) HasTransferApproval(tokenAddress, tokenId) returns (uint256) {
        require(activeItems[tokenAddress][tokenId] == false,"Item already up for sale");
        uint256 newItemId = itemsForSale.length;
        itemsForSale.push(AuctionItem(newItemId,tokenAddress,tokenId,payable(msg.sender),askingPrice, false));
        activeItems[tokenAddress][tokenId] = true;

        assert(itemsForSale[newItemId].id == newItemId);
        emit itemAdded(newItemId, tokenId, tokenAddress, askingPrice);
        return newItemId;
    }

    function buyItem(uint256 id) external payable ItemExists(id) IsForSale(id) HasTransferApproval(itemsForSale[id].tokenAddress,itemsForSale[id].tokenId){
        require(msg.value >= itemsForSale[id].askingPrice,"Not enough funds sent");
        require(msg.sender != itemsForSale[id].seller);

        itemsForSale[id].isSold = true;
        activeItems[itemsForSale[id].tokenAddress][itemsForSale[id].tokenId] = false;
        IERC721(itemsForSale[id].tokenAddress).safeTransferFrom(itemsForSale[id].seller, msg.sender, itemsForSale[id].tokenId);
        itemsForSale[id].seller.transfer(msg.value);

        emit itemSold(id, msg.sender, itemsForSale[id].askingPrice);
    }
}

I have buy; add; and sell working but then with 1 address I accidently tried to add an item with undefined price value now i can’t add any items to the contract with that address metamask shows the Alert txn Error msg in the signing dialog. I can still buy items from the market with that address and doesn’t seem like other address’s are effected since i can add buy and sell with them

Hey @PeacanDuck

Try the solution from there:

already have it here pretty sure my contract is the same as the one on github

modifier OnlyItemOwner(address tokenAddress, uint256 tokenId) {
        IERC721 tokenContract = IERC721(tokenAddress);
        require(tokenContract.ownerOf(tokenId) == msg.sender);
        _;
    }

Hey @PeacanDuck
Are you trying to buy an item from another address?(not owner of the token)

I’ve checked your smartcontract on remix.ethereum.org
It works correctly

I’m trying to add an item to the market it was working fine until i tried to add an item with no price data after that i now can’t add any items with that address;

as far as i can tell it should work but as you can see no event goes off and nothing gets added checked in storage no token id 20 exists

ahh found the problem i forgot they where getting auto added to market when created XD

1 Like