SolomonDefi / solomon-monorepo

Monorepo containing core Solomon apps, services, libraries, and deploy config.
6 stars 4 forks source link

Managing gaps in the staker pool after user unstakes #189

Closed jtse0 closed 2 years ago

jtse0 commented 2 years ago

This issue pertains to the gaps created when users call the unstake() function - as shown in excerpt below:

        for (uint256 i = 0; i < stakerPool.length; i += 1) {
            uint256 beneficiary = stakerStorage.getUserId(backer);
            if (stakerPool[i] == beneficiary) {
                delete stakerPool[i];
            }
        }

Since we don't particularly care about the order, and to introduce an aspect of randomness in the staker pool, when a user unstakes, we can take the last staker in the staker pool and move that staker's index to the index of the unstaked user.

        for (uint256 i = 0; i < stakerPool.length; i += 1) {
            uint256 beneficiary = stakerStorage.getUserId(backer);
            if (stakerPool[i] == beneficiary) {
                delete stakerPool[i];

                if (stakerPool.length > 1 && i != (stakerPool.length - 1)) {
                    stakerPool[i] = stakerPool[stakerPool.length - 1];
                    stakerPool.pop();
                }   
            }
        }