This is the first dapp I'm going to build and I was thinking about not doing it 100% decentralized due to some limitations I've been encountering with Solidity (This may just be because of the way I'm used to do things), basically the dapp is like a marketplace where each user can buy/bid for a digital good. What I'm having trouble right now is thinking a way of tracking everything an user owns in said dapp. Initially I was thinking about using a mapping for each address, each address would have an array of structs for the item they bought or whatever to later retrieve that information by address so I don't have to loop through the mapping (since this is discouraged for what I've read) but my problem here is that I can't return the whole array of structs and I don't want to destructure it and retrieve each field of it one by one. I was thinking of processing the transaction (This way it is available in the Blockchain) and depending on the output of said transaction I can send a request to an API with the information sent in the previous transaction to save it in a friendlier way to be retrieved later, this way I can track user ownership and everything we'd need. Is this a "valid" way of doing things or I'm breaking some crucial concept by saving that kind of information in a database from an API? What can I do to track each item an user owns (Maybe this'll help sync the UI too), make this look simpler and probably not having to use an API to save this information (If not at all)?

1 Answer 1


You can use mappings for 1 OP lookups and insertions. You can arrange things so iterative activities are handled by software clients - either user interfaces or caching servers. You have (basically) two options.

  1. Emit events for each insert/update operation and rely on the clients to just know what's in the database.
  2. Keep indexes of the keys in the contract so the contents are completely discoverable.

Since contracts are a write-once, live with it forever proposition, I usually make contracts that enable both approaches.

Have a look at Mapped Structs with Indexes over here: Are there well-solved and simple storage patterns for Solidity? That's a sort of Swiss Army Knife for a single table, including deletes (you may not need). When you understand the pattern, have a look at a way to handle joins. https://medium.com/@robhitchens/enforcing-referential-integrity-in-ethereum-smart-contracts-a9ab1427ff42

I'm a big believer in the idea that contracts should ensure the integrity of the contract state at all times. I'm a big believer in externalizing concerns than can be solved without using a contract. A synthesis of those leanings is that your functions can be externalized to the extent that you don't need to solve for trust using blockchain instead (for that step), and no matter what the trusted client decides to do, the contract won't allow the contract state to stop making sense.

Hope it helps.

  • Thanks for the insight Rob, this was helpful and helped me decide what to do.
    – Aguxez
    Jun 11, 2018 at 16:29

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.