1

I have following Solidity code:

pragma solidity ^0.4.18;

contract ContractsRegistry {

  address owner;

  struct ContractInfo {
    bytes32 subject;
    bytes32 companyName;
    string contactName;
    string contactEmail;
    string contactPhone;
  }

  mapping(address => ContractInfo) contractsMap;
  address[]   addresses;

I want to display full content of the mapping contractsMap in the webapp. What would be the best approach to achieve this ? Here are some additional questions:

  • Is it possible to to convert it to JSON representation somehow ?

  • Should I use another data structure ?

  • Is it a regular way of using a smart contract or nobody does it like that ?

2

You cannot use mapping if you need to access “all” the data stored. Mapping aim is to find in a fast and quick way THE record you are looking for by means of a fast research in the hash table.

The only practical iterable item could be an array in which the single record is your struct previously stored, but you should push that record in the array when writing the single mapping item (this in the case that your mapping be needed truly for your work because its capability to both be fast and assuring gas saving in retrieving}.

You could, in that way, use the array to list only and the mapping to search only.

A second more quick possibility could be to have the mapping outputting the index in the array, which can the be used to retrieve the data without doubling the memory space used.

When a new record arrives, you should then write it in the array togheter the address or not, depending on your application, retrieve the index in the array and then write in the mapping {address, index}

0

when retrieving a struct through a call to a smart contract, you'll get a simple array that gives you the values without the keys.

You'll get something like this : ["mysubject","mycompanyname","mycontactname","mycontactemail","mycontactphone"]

you wont have a mapped object. You'll have to know exactly then what is the structure you have retrieved and the order of the defined keys, so this way you can rebuild the mapping of the object inside your application/website/middleware/whatevermadethecall

  • I want to retrieve all the mappings, not just struct, so it should be multidimensional array – RB_ Dec 20 '17 at 21:58
  • 2
    Unfortunately, that's not possible. You will only be able to retrieve one item at a time through the getter automatically created by making the mapping public. – pabloruiz55 Dec 20 '17 at 23:24
  • @pabloruiz55 What about using another data structure ? Or can it be a good idea to iterate the map, and assemble a JSON array of objects, and then return it as a string ? – RB_ Dec 21 '17 at 11:24
  • You can't iterate mappings. You could create an array that stores the keys of each struct in the mapping and then, in you Dapp, for each element, get a struct, recreate the json object and add it to an array to then display it as you wish. – pabloruiz55 Dec 21 '17 at 12:35
  • @pabloruiz55, "and then, in you Dapp, for each element, get a struct," you mean doing contract method call() to retrieve each struct ? – RB_ Dec 21 '17 at 13:03

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.