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:
Any help would be greatly appreciated.