0

I'm building an NFT marketplace. I have a view function that returns the NFTs of a user. However, this function requires a transaction and costs gas that the user must pay.

Here is the related part of the MarketContract code:

contract Market is ReentrancyGuard {
    using Counters for Counters.Counter;
    Counters.Counter private _listingCount;

    mapping(uint256 => Listing) public marketListings;

    struct Listing {
        uint256 listingId;
        address nftContractAdd;
        uint256 tokenId;
        address ownerAdd;
        uint256 price;
    }

    function getItemsOf (address wallet) public view returns (Listing[] memory) {
        uint resultCount = 0;
        for (uint i = 0; i < _listingCount.current(); i++) {
            if (marketListings[i].ownerAdd == address(wallet)) {
                resultCount++; 
            }
        }

        uint itemCount = 0;
        Listing[] memory items = new Listing[](resultCount);
        for (uint256 i = 0; i <= _listingCount.current(); i++) { 
            if(marketListings[i].ownerAdd == address(wallet)) {
                items[itemCount] = marketListings[i];
                itemCount++;
            }
        }

        return items;
    }
}

And, here is my frontend code:

async getListedItemsOfUser({ }, payload) {
        const { walletAddress } = payload

        const web3Modal = new Web3Modal();
        const connection = await web3Modal.connect();
        const provider = new ethers.providers.Web3Provider(connection);
        const signer = provider.getSigner();

        const marketContract = new ethers.Contract(
            Secrets.MARKET_CONTRACT_ADDRESS,
            MarketContract.abi,
            signer
        );

        try {
            const res = await marketContract.getListedItemsOf(walletAddress);
            console.log("listed items", res)
        } catch (err) {
            console.log(err)
        }
    }

Why does this function cost gas?

Here the ABI of all the contract code: GIST

4
  • How are you measuring the gas cost? If called off-chain it shouldn't charge anything.
    – Ismael
    Apr 25, 2022 at 13:51
  • I couldn't get what you mean by 'measuring'? When the frontend method works, Metamask opens and the user must confirm a transaction and pay some gas. But this is only a view function? @Ismael
    – anileates
    Apr 25, 2022 at 13:57
  • It shouldn't ask for confirmation. What's the abi you are using? Can you attach it to the question? For some reason the frontend code creates a transaction, and it shouldn't do it for view functions.
    – Ismael
    Apr 25, 2022 at 17:39
  • Here is the abi: gist @Ismael
    – anileates
    Apr 25, 2022 at 18:31

1 Answer 1

2

You are calling getListedItemsOf method from your frontend. Shouldn't you be calling getItemsOf like you have posted in your question?

These two methdos have different stateMutability in the provided ABI.

1
  • Oh, you are right! didn't notice that. Thank you so much!
    – anileates
    Apr 26, 2022 at 9:27

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.