Solidity – How to Solve ‘TypeError: Internal or Recursive Type is Not Allowed for Public State Variables’

mappingsolidity

I have the following mapping for the users stakes in solidity.

mapping(address => AllUserStakedTimestamp) internal allUserStakes;

struct AllUserStakedTimestamp {
    bool[] _wasUnstaked;
    bool[] _autoRenewal;
    uint[] _amountStaked;
    uint[] _timeOfStake;
    uint[] _timesOfRelease;
    uint[] _optionReleaseSelected; // 0-1-2
    uint[] _epochDuration;
    uint[] _rewardPerCycle;
    uint[] _finalStakeReward;
}

I have now finished to write the contract and need to retrieve some of this data from the front end / web3.js, but just now realized that I will encounter this error:

TypeError: Internal or recursive type is not allowed for public state variables.
--> 11 - NewPublic.sol:72:5:72 | mapping(address => AllUserStakedTimestamp) public allUserStakes;

How can I make the data available "public" without getting this error, as this struct is used by many other solidity functions and modifying it or splitting it into smaller parts will require pretty much a full rewrite of the functions and bugs search from the beginning.

Any suggesiton?

Best Answer

mapping(address => AllUserStakedTimestamp) public allUserStakes;

struct AllUserStakedTimestamp {
    bool[] _wasUnstaked;
    bool[] _autoRenewal;
    uint[] _amountStaked;
    uint[] _timeOfStake;
    uint[] _timesOfRelease;
    uint[] _optionReleaseSelected; // 0-1-2
    uint[] _epochDuration;
    uint[] _rewardPerCycle;
    uint[] _finalStakeReward;
}

That will do. There you have to give public instead of internal.

Just know there are four types of function visibility in the solidity:

  • Public
  • Private
  • Internal
  • External.

Public: The function can be called by anyone both from inside the contract and outside the contract.

Private: The function is only available inside the contract. When you assign this visbility, the function isn't even available to inherit.

Internal: Just like the private but it supports inheritance. You can inherit the function.

External: Function can only be accessed by outside the contract. If the function visibility is external, that means it is not accessable internally.

You can read more about the function visibilities here.

Tell me if it helps!

Related Topic