0

Please consider this contract:

pragma solidity 0.8.7;

contract Test {

    struct UserStruct {
        uint256 betAmount;
        uint256[] potentialWinnings;
        string teamChosen;}

    mapping(address => UserStruct[]) public userStructs;

    function appendUserBet(string memory userTeamChosen,uint256 eventNumber) public payable {
        UserStruct memory userStruct = UserStruct(msg.value,new uint256[](0),userTeamChosen);
        userStructs[msg.sender].push(userStruct);}

    function updatePotentialWinnings(uint256 eventNumber,address userId,uint256[] memory potentialWinnings) public {
        userStructs[userId][eventNumber].potentialWinnings = potentialWinnings;}}

It works fine, however when I'm including a function below to the contract, it doesn't compile. It errors out with TypeError: Invalid type for argument in function call. Invalid implicit conversion from uint256 to uint256[] memory requested.

    function addAllPotentialWinnings(uint256 _eventNumber, uint256[] memory _potentialWinnings, address[] memory _usersList) public {
        for (uint i =0; i<_usersList.length;i++){
            updatePotentialWinnings(_eventNumber,_usersList[i],_potentialWinnings[i]);}}

I want to provide an array of arrays (2D-array of dynamic size) as an argument to the function, but I seem to be doing it incorrectly. From what I was able to understand is that UserStruct expects potentialWinnings to be in uint256[] format, but it recieves them as uint256. By design I want to provide the function with an array that looks like this: [[0,1,2],[2,5,6],[x,y,z],...]

How do I change my contract such that it "knows" that the array i'm passing as an argument, is going to have the required arrays inside?

Thank you in advance!

1 Answer 1

1

Just use 2d array

function addAllPotentialWinnings(uint256 _eventNumber, uint256[][] memory _potentialWinnings, address[] memory _usersList) public {
        for (uint i =0; i<_usersList.length;i++){
            updatePotentialWinnings(_eventNumber,_usersList[i],_potentialWinnings[i]);
        }
    }

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.