0

I'm new to Solidity and coding in general and this code may be a disaster, but I'm learning.

Basically, I'd like to get members of a struct with a function. I can return the entire struct in the form of a tuple in another function (getPairData()), but not when trying to return individual members. When I try accessing the members (getPairMembers()) - I get an error: struct member not found or not visible after argument dependent lookup in struct MapTest.Pair storage... (see photo below)

I think this code may be a little hacky - I wasn't sure how to keep track of multiple datasets from the same user. I tried making a mapping to an array of the structs, but couldn't figure out how to access each one individually so I mapped every address to an integer mapped to the struct array to act as a sort of index(?)

Here's my code:

pragma solidity ^0.8.0;

contract MapTest {

struct Pair {
    uint256 token1;
    uint256 token2;
    uint256 breedStart;
    uint256 breedEnd;
}

mapping(address => mapping(uint256 => Pair[])) userPairs; 
mapping(address => uint256) pairCount;
mapping(uint256 => bool) isBreeding;

function getRandom() public view returns(uint256){
    uint256 time = block.timestamp;
    uint256 rand = uint256(keccak256(abi.encodePacked(time)));
    
    return (rand % 360);
}

function breed(uint256 _token1, uint256 _token2) public {
    require (isBreeding[_token1] == false && isBreeding[_token2] == false);

    uint256 breedTime = getRandom();
    uint256 time = block.timestamp;
    
    Pair memory pairTest;
    
    pairTest.breedStart = time;
    pairTest.breedEnd = time + breedTime;
    pairTest.token1 = _token1;
    pairTest.token2 = _token2;
    
    userPairs[msg.sender][pairCount[msg.sender]].push(pairTest);
    isBreeding[_token1] = true;
    isBreeding[_token2] = true;
    
    pairCount[msg.sender]++;
    //function for burning incubator here
}


function getPairData(address _wallet, uint256 _index) public view returns(Pair[] memory){
    return userPairs[_wallet][_index];
}

function getPairMembers(address _wallet, uint _index) public view returns(uint256, uint256, uint256, uint256){
    return (userPairs[_wallet][_index].token1, 
            userPairs[_wallet][_index].token2, 
            userPairs[_wallet][_index].breedStart, 
            userPairs[_wallet][_index].breedEnd);
}

}

The error code I receive is this:

enter image description here

Any help would be greatly appreciated.

0

The logic you used is actually for: mapping(address=>mapping(uint=>Pair)) which is the structure itself instead of the struct array you're defining inside the mapping. To access token1 value, you'll first need to push the items into the Pair[] array like you did here: userPairs[msg.sender][pairCount[msg.sender]].push(pairTest) and then iterate through the mapping array with a for loop.

for(uint i=0;i<userPairs[msg.sender][pairCount[msg.sender]].length;i++)
{
 return userPairs[msg.sender][pairCount[msg.sender]][i].token1;
}
2
  • Thank you so much! That's incredible. So it seems like I might not have even needed to use an array for Pair in the first place when mapping(address=>mapping(uint=>Pair)) and accessing by index seems to work fine. Would you recommend against this, or would that implementation be fine as well? Again, thanks so much for your help. You're a lifesaver.
    – Ethan Z.
    Oct 5 at 20:35
  • No worries, glad I could help! I'm still learning to work with mappings and arrays atm, but the way I would do it would be to declare 2 more values in the Pair struct: an uint id to keep count of the pair and an address breeder to keep track of the user that is initiating the breed process. After that I would change the mapping mapping(address=>mapping(uint=>Pair)) into this: mapping(address => Pair[]) because I suppose you want to keep track of the number of pairs each address generates.
    – Icarus23
    Oct 5 at 21:48

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.