0

I am getting an unusual error. It would be great if someone can help me out with it.

This is the code of my user interface which sends a transaction to the smart contract.

const provider = new ethers.providers.JsonRpcProvider("https://matic-mumbai.chainstacklabs.com/")

const marketContract = new ethers.Contract(marketplace_mumbai, Marketplace.abi, provider);

const data = await marketContract.fetchMarketItems();

I have checked my arguments for setting up the contract instance are correct, but there is something wrong whenever I call the fetchMarketItems() of my smart contract:

 function fetchMarketItems() public view returns (MarketItem[] memory) {
    uint256 itemCount = itemIds.current();
    uint256 unsoldItemCount = itemIds.current() - itemsSold.current();
    uint256 currentIndex = 0;

    MarketItem[] memory items = new MarketItem[](unsoldItemCount);

    for (uint256 i = 0; i < itemCount; i++) {
        if (idToMarketItem[i + 1].owner == address(0)) {
            uint256 currentId = idToMarketItem[i + 1].itemId;
            MarketItem storage currentItem = idToMarketItem[currentId];
            items[currentIndex] = currentItem;
            currentIndex += 1;
        }
    }
    return items;
}

I get this error:

Uncaught (in promise) Error: call revert exception; VM Exception while processing transaction: reverted with panic code 50 [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="fetchMarketItems()", data="0x4e487b710000000000000000000000000000000000000000000000000000000000000032", errorArgs=[{"type":"BigNumber","hex":"0x32"}], errorName="Panic", errorSignature="Panic(uint256)", reason=null, code=CALL_EXCEPTION, version=abi/5.7.0)
    at Logger.makeError (index.js?dd68:224:1)
    at Logger.throwError (index.js?dd68:233:1)
    at Interface.decodeFunctionResult (interface.js?1df7:345:1)
    at Contract.eval (index.js?b70c:293:1)
    at Generator.next (<anonymous>)
    at fulfilled (index.js?b70c:5:43)
1
  • are you initializing the array? it can't parse Uint256 with a very big value so it panics
    – Nulik
    Commented Mar 7, 2023 at 21:08

1 Answer 1

0

idToMarketItem[i + 1].owner is out of bounds for the last item.

Solidity's array starts with index 0, you don't need to use idToMarketItem[i + 1]. Is your idToMarketItem array padded with some initial item?🤔

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.