0
votes

Let's say I have a struct & a mapping like this:

    struct myStruct {
    address addr;
    uint256 price;
    bool approved;
    }

mapping(string => myStruct) mappy;

How can I retrieve all the keys? I know solidity will generate getters so if I have the key I can retrieve the info from inside the struct. But the keys are unknown to me and I need to retrieve the complete struct.

Maybe a better solution would be to have a public variable which would be the size of the struct and an index has key and store the key in the struct? That way I will know the size and I suppose I can iterate it

1
Try looking through ethereum.stackexchange.com/questions/13167/… for suggestions on common contract design patterns. You should find a useful solution there. - Adam Kipnis

1 Answers

0
votes

First thing is that you shouldn't use string as key, better declare bytes32 as string is just an alias for it.

you can make a call to the mapping giving the string key as reference.

However it will retrieve only an iterative array without the internal keynames.

let's say for example you have : mappy['a'] that contains the following information

{ '0x000...000', 1, true }

the call will retrieve an simple array like this : ['0x000...000',1,true]

If you want to make it an object you'll have to rebuild it on your own.