I'm developing an app that allows user to make bets against each other. In order to get some useful web-UI info, i need to write a getter function for an array of structs.
Simplified version of my code to get the point across:
struct UserStruct {
uint256 betAmount;
uint256 matchId;}
mapping(address => UserStruct[]) public userStructs;
function appendUserBet( uint256 eventNumber) public payable{
UserStruct memory userStruct = UserStruct(msg.value, eventNumber);
userStructs[msg.sender].push(userStruct);}
function userStructLookUp(uint eventNumber, address userId) public view returns (UserStruct memory){
uint index = userStructs[userId].length;
for (uint i = 0; i < index; i++) {
if (userStructs[userId][i].matchId == eventNumber) {
return userStructs[userId][i];}}}
My userStructs
has two keys - an address
and an event id
, which my current version of a getter function can work with, given I provide it with required args.
What I need though is a function that would provide me with an array of structs for a particular user when i provide said function with a user address. So i supposed it would be like this:
function userMatchesLookUp(address userId ) public view returns(uint256[] memory){
return userStructs[userId];}
But it doesn't compile because i try to assign the return to the wrong type storage ref[] storage ref is not implicitly convertible to expected type uint256[]
, but which is the correct?. A similar question was asked here [Similar question] but there was no solution to the problem. Can you please tell me where i went wrong?
Thank you in advance!