Maintaining state - how


I've built a couple of dapps. The usual stuff. Say I have a BankContract with accounts and balances.

My recurring question is: the contract maintains state, but querying that state to get a list of accounts and their balances is hard (e.g. the contract cannot return an array of data items). So is it reasonable to expect that we have an external database (or cluster, to remove SPOF) that listens to filter events emitted by the contract, and we query against that instead?

How are other dapps handling this? E.g, Cicero's Weifund - If a user is using a UI, and wants to see all the available crowd-sourcing opportunities, how can they do that? Any listings created through the UI will be known by the UI (in a single page application SPA model), but when the user launches the SPA UI, how does it catch up on the status of other listings?

Hope this question makes sense,



  • FabianVogelstellerFabianVogelsteller Berlin, GermanyMember Posts: 13
    Solidity will introduce arrays, which then can get returned.

    Otherwise, you could iterate through the storage, using web3.eth.getStorageAt(, )

    I will aks christian to give you an estimate on arrays.
  • chrisethchriseth Member Posts: 170 ✭✭✭
    Solidity can return arrays since - erm - yesterday.
    Automatic accessors will not return arrays, but provide index access.
    So as an example:
    contract c { uint[] data; function getData() returns (uint[]) { return data; } }
    If you have an array of structs, you have to either manually convert them to fixed-size arrays in the getData function, or you just make them public (which provides you an index-based accessor) and only implement a function to return the length of the array.
  • jwgcarlylejwgcarlyle Member Posts: 29
    Thanks, @chriseth, your answer makes perfect sense
  • SilentCiceroSilentCicero Toronto, CAMember Posts: 159 ✭✭✭
    @jwcarlyle so the WeiFund contract keeps track of the total number of campaigns. Each campaign is given an id, the higher the id value (a uint) the more recent the campaign is. WeiFund will load the campaigns from top to bottom, so the most recent to the earliest. WeiFund uses the browsers localStore to keep the campaigns so it doesn't have to load them twice. When campaign data is changed, WeiFund tries to keep track of the changes and reloads the campaign that has those changes. WeiFund does not load all campaigns at once. It only loads them in chunks, and generally the most recent first. If your loading in a specific category, it batch loads until the necessary chunk size is fulfilled by the load.

    However this is only one approach. A centralized approach would be to cache the campaign data in a central server storage and load the campaign data quickly when necessary. Another approach would be to use the getStorageAt method. Our approach loads from a public contract getter 'campaigns'.

    I'm sure there are others. But the way I approach it seems to suite the current application presently. Also, the current WeiFund code has just been re-factored, so the 'load more' feature I'm talking about is still under development. I broke it to make it better :)
  • chrisethchriseth Member Posts: 170 ✭✭✭
    I also just made an example contract that shows how to iterate over mapping keys - if that is of any help.
  • jwgcarlylejwgcarlyle Member Posts: 29
    @chriseth, many thanks. I took a quick look at your example contract, and my cursory understanding is that it is maintaining a shadow record (as an array) of item IDs and whether they are logically deleted, along with a mapping of keys/values where the pair gets physically deleted, along with iterator and add/remove methods. Looks cool.

    But I'm thinking that if a Single Page App needs to query the contract to understand what items are available, and the contract can't provide an array of structs, then the SPA will need to query for item details one-by-one, and so it is equally valid for the iterator function to be provided through a linked list. This is what I am experimenting with for now.
Sign In or Register to comment.