I have a struct in a library:

// lib
struct Data {
    int256 count;
    mapping(address => int256) indices;
    mapping(int256 => address) items;

// Also append function in lib
function append(Data storage self, address addr)
returns (bool) {
    if (addr == ZERO_ADDRESS) {
        return false;

    int256 index = self.indices[addr] - 1;
    if (index >= 0 && index < self.count) {
        return false;

    self.indices[addr] = self.count;
    self.items[self.count] = addr;
    return true;

And then another contract that uses this library struct:

// Contract.sol
using LibMap for LibMap.Data;
mapping(address => LibMap.Data) public addresses;

And then in a function I push to it


From the front end with web3 I can do contract.addresses(msg.sender) where msg.sender is the address from earlier in the contract that was set and it will return a length/count of how many have been appended. But how would I rather read all the addresses that have been pushed instead of a length/count?

Would I need a function in the contract for this?

  • What specifically are you trying to achieve with addresses[msg.sender].append(addr); ? Feb 1 '19 at 1:13
  • Its a list of public contract addresses. And I need to access those contract address list for msg.sender from the front end
    – Chipe
    Feb 1 '19 at 1:20
  • Have you defined the append function somewhere? Feb 1 '19 at 1:38
  • append is in the lib. I updated question above. items holds the addresses
    – Chipe
    Feb 1 '19 at 2:09

If you want to read all the addresses appended to any given Data, you will have to do it iteratively.

You could do this on the frontend, by querying the contract for what the value of addresses[msg.sender].count, and then loop through and get the value of everything in addresses[msg.sender].items one at a time (from 1 to count).

The alternative would just be to do the same sort of thing with a function in your contract, loop through and return an array of them.

You can't just return a mapping though.

  • Thanks. I added a function in my contract that will pull at index, and on the front end I can iterate through the count.
    – Chipe
    Feb 1 '19 at 16:30

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.