0

So right now i have a function in my smart contract that is able to fetch all open listings(nft marketplace). It looks like this:

 function fetchListings() external view returns (Listing[] memory) {
    uint itemCount = _itemIds.current();
    uint unsoldItemCount = _itemIds.current() - _itemsSold.current() - _itemsCancelled.current();
    uint currentIndex = 0;
    Listing[] memory items = new Listing[](unsoldItemCount);
    for (uint i = 0; i < itemCount; i++) {
      if (_listings[i + 1].owner == address(0)) {
        items[currentIndex] = _listings[i + 1];
        currentIndex += 1;
      }
    }
    return items;
  }

But my guess is that this would not work if there will be a lot of items. The array will get to big right? Now im trying to understand to what extend i could use this function, that is, to what amount of listings is using an array here appropriate?

Secondly, what are the other ways of fetching the data about listings? Have a db and listen to events fired? Or..?

Appreciate your time.

1 Answer 1

0

You have two choices. Either do the for loop in client-side or in the contract code:

If you want to get the items on the client, you can get your data faster from the server, but if the data you get is much bigger than what the client needs then you need to do some additional work to filter them and use the client's resources.

If you want to filter data on the chain, then it may take longer data to get them, but the data you get is already filtered/smaller, and the client doesn't need to do any work.

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.