1

I come from a HTML/PHP/MYSQL background so bear with me on this. I am in the process of developing a contract and it is going pretty well. The main issue I have at the moment is how to filter an Array created with solidity.

At the minute the Array contains a list of Structs (these are items that can be purchased). I want to be able to filter them so that i have a list of unsold items.

Now in MYSQL you can obviously query but not here. At the minute I am doing the following:

  1. Frontend - Call function that returns the length of the array.
  2. Frontend - Taking the length loop through and fetch all the items.
  3. Frontend - Check to see if the item is bought, if so separate into a "unsold" frontend array.
  4. Display items in the unsold array.

The obvious issue here is that lets say we have 1000 items, the frontend has to loop through each one and then call the function to get the items details so we end up with 2000 calls plus the filtering on top into a separate array. This obviously takes time.

I could go down the route of taking all the items from the contract and then storing them in a MYSQL db for queries but I was looking to avoid the use of DBs if possible.

Im wondering if there is a better approach to this that I don't know about? For example should I be worrying about network performance like you would do when you are fetching from lets say a single server?

Edit One way it could be made slightly easier is if I could return the Array containing the Structs in one go instead of having to do separate calls for each one based on the length.

1

One approach which comes to my mind is using events.

Create event with two parameters, one of which will say Sold/Available and the other one will store the name of the struct in your mapping(array). When you will need to check your unsold items you just get historic events of particular contract, filter which items are unsold and then retrieve the data from smart contract if needed.

You can use only events and store more infromation in them than just name and you won't even have to execute a transaction to retrieve data. This can be useful if this list will be loading on page load and unmutable (in case you don't need to change any data in your structs in your array).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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