0

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!

2 Answers 2

0

Your issue is that you're retrieving inside userMatchesLookUp() function an uint256 array and you're returning entire array of struct. I suggest you to return entire array of struct because in this way you can retrieve all values inside it, like: betAmount and matchId for each match.

I adjusted your smart contract in this way:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Test {
    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];
            }
        }
    }

    // NOTE: I changed your return object with struct array in signature function
    function userMatchesLookUp(address userId) public view returns(UserStruct[] memory){
        return userStructs[userId];
    }

}
0

you must add age.rhref before adding the compile.json or else function will override uint256 eventNumber always make sure to reference ethereum.api.null in docs. or else this image will show what i meant ethereum ui

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.