Solidity – Handling Structs with Mappings in Foundry Tests

foundrysolidity

This is the mapping containing the struct.

    mapping(address => UserFessInfo) public userFeesInfo;

This is the struct UserFeeInfo

    struct UserFessInfo {
    uint256 stakedAmount;
    uint256 stakedWeight;
    uint256 lastStakedBlock;
    uint256 lastClaimedBlock;
    mapping(uint256 => uint256) epochToUserStakedWeight;
}

Now, to retrieve these values for testing I tried to use this syntax in foundry test file.

       (
        uint256 stakedAmount,
        uint256 stakedWeight,
        uint256 lastStakedBlock,
        uint256 lastClaimedBlock,
        mapping(uint256 => uint256) epochToUserStakedWeight
    ) = core.userFeesInfo(bob);

But later I realized that it contains mapping and needed the data location to be set as storage. And I was right as I got this error.

error[6651]: test/StakeAndClaim.t.sol:85:13: TypeError: Data location must be "storage" for variable, but none was given.
        mapping(uint256 => uint256) epochToUserStakedWeight
        ^-------------------------------------------------^

I am confused about how to do this. How to store the struct that is returned from the mapping?

Best Answer

I believe you need to add 'storage' (or 'memory') in the following line:

mapping(uint256 => uint256) storage epochToUserStakedWeight