Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assignment to struct in array #15164

Open
SvenMeyer opened this issue May 30, 2024 · 1 comment
Open

assignment to struct in array #15164

SvenMeyer opened this issue May 30, 2024 · 1 comment
Labels

Comments

@SvenMeyer
Copy link

SvenMeyer commented May 30, 2024

I'll start with as little code as possible as likely "It's me not solc"

    struct Allocation {
        uint48 stakeTime;
        uint48 unlockTime;
        uint32 spare;
        uint128 stakeAmount;
    }

    mapping(address => Allocation[]) public userMap;

    Allocation[] storage allocations = userMap[_account];
    Allocation storage a;

    while (aid > i) {
        a = allocations[aid];
        if (a.unlockTime <= block.timestamp) {
            ++foundUnlocked;
            totalAmount += a.stakeAmount;
            // a = allocations[allocations.length - 1]; // Why does this does not work ????
            allocations[aid] = allocations[allocations.length - 1]; // this works
            allocations.pop();
        }
        --aid;
    }

I understand that array of struct if a reference type, but why can't I assign to a that it shows up in allocations[aid] (as it should point to the same storage slot) ?

If I assign individual variables within a struct it works, but that is of course not an efficient alternative.

    a = allocations[i];
    a.stakeAmount = totalAmount.toUint128();
    a.stakeTime = uint48(block.timestamp);
    a.unlockTime = uint48(block.timestamp);

What am I missing ?

@EduardoMelo00
Copy link

hey @SvenMeyer

How can we reproduce the code ?

Maybe we can help you with the entire code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants