1

Building a test game with structs that represent items with the following code:

    struct GameItem {
        string name;
        uint maxSupply;
        bool saleOpen;
        uint mintPrice;
    }
    
    GameItem[] public gameItems;

The array gameItems is going to be filled with hundreds of items that will serve as a master list for all items in the game. Now, when my Web3 application connects, I want to return the full list to the client so they can list it or whatever, I'm using the following function for it:

    function getItems()public view returns( GameItem[] memory){
        return gameItems;
    }

When I test it on Remix with one or two items, it returns almost instantly. If I add something like 10 or 20, it takes a couple of seconds, which makes me wonder what would happen if this has hundreds, or perhaps thousands of items.

My questions are:

If I had 1000+ items, would my call to this function timeout or freeze? How can I know what is the limit?

Is there a better way to approach that does not have a big impact on performance? Would it make sense to add pagination to the function?

1 Answer 1

1

The blockchain and smart contracts are good to save verifiable data, ownership of some asset, the result of things like elections, etc. But maybe they are not good for any kind of data that does not require special logic, trust, and decentralization.

Maybe you have some data and it's too big to fit in the blockchain, but you could certainly save proof of that data in the blockchain. For example, maybe you have a book and you would like to save it in the blockchain as proof you are the owner or something like that. But it's better to save a hash of the book in the blockchain instead.

So, coming back to your scenario, maybe you could use a regular backend/database to save all the data you need, and save the results of the game in the blockchain. For example, user1 won the match against user2, then you can save that in the blockchain so others can see it and no one is able to tamper with it.

Yes, having a lot of items in an array in a smart contract may not be a good idea. If you don't have logic to clean the data that is no longer needed, then when running your smart contract you may start getting out-of-gas exceptions and the contract may become unresponsive and unusable.

If you manage to remove the elements that you no longer need from your contract, then it could be ok to have the data in it. Pagination could work as well.

You could use the delete keyword to delete/clear the whole array at once like delete gameItems. Or a specific element delete gameItems[i]. But maybe you want to resize the array each time you delete an item:

// 'Deleting' an element at index `i` by setting the last element to the position of the element at index `i`
gameItems[i] = gameItems[gameItems.length - 1];
// Removing the last element from the array since we already copied it.
gameItems.pop();

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.