Question:

How would you render this structure of data?

Isabella: 25 May 2022

I have coded a smart contract which allows one to store a certain type of structure of datas which can be described as something like that :

├── Client1(Contract)
│   ├── Year2018 (Struct containing string,uint and struct)
│   │   ├── Case 1 (Struct containing string)
│   │   └── Case 2 (Struct containing string)
│   └── Year2017
│       └── Case 1 (Struct containing string)
├── Client 2
│   └── Year2018
│          ├──Case1 (Struct containing string)
│          └──Case2 (You got it)

I managed to fill it correctly and parse it too ( But only one case INDIVIDUALLY). I would like to be able to have a function which returns all the datas in a organized way (such as a multi-dimensionnal array or a JSON for example). What should I use ? Is it possible to store all these value in a bytes array ?

Thanks for your ideas and have a good day !

EDIT:

Should i try something like this ?

function massImport(bytes[] _datas)  view external onlyOwner(){
        for(uint i = 0;i<_datas.length;i++){
            addYear(bytes(_datas[i]));
            for(uint j = 0; j<_datas[i].length;j++){
                addCase(bytes(_datas[i][j]));
            }
        }
    }

I typed the function bytes() because I assume that bytes are the only possible array in this case?

EDIT 2 : Here is the struct 'YEAR'

struct Year{ 
    string entryName; 
    string entryYear; 
    uint permissionType; 
    Elem elem; 
    uint[] elemList; 
    mapping(uint => Elem) allElem; 
    uint elemId; 
}

Do you think that it's too heavy to be stored fully on the blockchain ?

Answer:
James: 25 May 2022

You may run out of gas by having those nested for-loops. What about having a mapping from uint to string, and use it to store a hash of the information for each year? Then you could store your structs in a centralized database and use the blockchain for validation purposes.

The idea behind this is that in order to take advantage of the immutability of the blockchain, there's no need to store the whole content but only a hash of the content. You can then use that hash as a proof that the information you have off-chain didn't change (because if it did, it's hash would be different than the one on-chain). Many projects do this, for example, https://www.po.et: it is not feasible to store a whole book in the blockchain, but if you hash it and store the hash, then that is enough to prove that at that moment of time you had a book with that exact text.

If you do want to store everything in the contract, I would not try to return a complex structure like that directly from the contract. Instead, I would write that logic in Javascript and call the contract several times to return the information for each pair of year & case number (or maybe just year).