1

Need this architecture

    {
      address1 => [struct1, struct2],
      address2 => [struct1, struct2, struct3,],
      address3 => [struct1, struct2, struct3, struct4, struct5],
      ...
    }

My Attempt

contract Gallery{
uint public emojiCount;
struct Token {
    uint token;
    string ipfsHash;
}
mapping (address => Token[]) public emoji;

function addEmoji (uint _token, string _ipfsHash) public {
   emojiCount ++;
   emoji[msg.sender].push(Token(_token, _ipfsHash));
 }
}

How do i return Struct array so that i can save Images IPFS String according to user account address [msg.sender] .

How is this possible in solidity
Thanks in advance

Syed Zain Ali
  • 115
  • 1
  • 7

1 Answers1

3

your code should work as intended, and give you a total count of 'emojis', but you may want that information on a per address basis, to do that you could add a second mapping, to always hold the last position for the inserted address:

mapping (address => uint256) addressToLastUsedPosition;

then when you do a push on the array, and get the position back, and set the mapping.

uint256 newMaxPositionForAddress = emoji[msg.sender].push(Token(_token, _ipfsHash));
addressToLastUsedPostion[msg.sender] = newMaxPositionForAddress;

doing so will always allow you to know how many 'emoji' are per address.

shaddow
  • 308
  • 1
  • 11
  • Thank you it's work for me . I think solution you explain with solidity code is much easier to understand , where similar question are as mention above are not self explanatory @shaddow – Syed Zain Ali Sep 19 '18 at 05:49
  • @shaddow instead of adding a new mapping can we just use

    retrun emoji[msg.sender].length

    – CaptPython Aug 25 '22 at 05:14