Skip to content

Latest commit

 

History

History
448 lines (386 loc) · 14.7 KB

WeightedStakingModule.md

File metadata and controls

448 lines (386 loc) · 14.7 KB

Weighted Staking module contract. (WeightedStakingModule.sol)

View Source: contracts/governance/Staking/modules/WeightedStakingModule.sol

↗ Extends: IFunctionsList, StakingShared, CheckpointsShared

WeightedStakingModule contract

Implements getters for weighted staking functionality

Functions


getPriorWeightedStake

Determine the prior weighted stake for an account as of a block number. Iterate through checkpoints adding up voting power.

function getPriorWeightedStake(address account, uint256 blockNumber, uint256 date) external view
returns(priorWeightedStake uint96)

Arguments

Name Type Description
account address The address of the account to check.
blockNumber uint256 The block number to get the vote balance at.
date uint256 The date/timestamp of the unstaking time.

Returns

The weighted stake the account had as of the given block.

Source Code
function getPriorWeightedStake(
        address account,
        uint256 blockNumber,
        uint256 date
    ) external view returns (uint96 priorWeightedStake) {
        return _getPriorWeightedStake(account, blockNumber, date);
    }

_getPriorWeightedStake

function _getPriorWeightedStake(address account, uint256 blockNumber, uint256 date) internal view
returns(priorWeightedStake uint96)

Arguments

Name Type Description
account address
blockNumber uint256
date uint256
Source Code
function _getPriorWeightedStake(
        address account,
        uint256 blockNumber,
        uint256 date
    ) internal view returns (uint96 priorWeightedStake) {
        /// @dev If date is not an exact break point, start weight computation from the previous break point (alternative would be the next).
        uint256 start = _timestampToLockDate(date);
        uint256 end = start + MAX_DURATION;

        /// @dev Max 78 iterations.
        for (uint256 i = start; i <= end; i += TWO_WEEKS) {
            uint96 weightedStake = _weightedStakeByDate(account, i, start, blockNumber);
            if (weightedStake > 0) {
                priorWeightedStake = add96(
                    priorWeightedStake,
                    weightedStake,
                    "overflow on total weight calc"
                ); // WS12
            }
        }
    }

weightedStakeByDate

Compute the voting power for a specific date. Power = stake * weight

function weightedStakeByDate(address account, uint256 date, uint256 startDate, uint256 blockNumber) external view
returns(power uint96)

Arguments

Name Type Description
account address The user address.
date uint256 The staking date to compute the power for. Adjusted to the previous valid lock date, if necessary.
startDate uint256 The date for which we need to know the power of the stake. Adjusted to the previous valid lock date, if necessary.
blockNumber uint256 The block number, needed for checkpointing.

Returns

The staking power.

Source Code
function weightedStakeByDate(
        address account,
        uint256 date,
        uint256 startDate,
        uint256 blockNumber
    ) external view returns (uint96 power) {
        date = _timestampToLockDate(date);
        startDate = _timestampToLockDate(startDate);
        return _weightedStakeByDate(account, date, startDate, blockNumber);
    }

_weightedStakeByDate

Compute the voting power for a specific date. Power = stake * weight

function _weightedStakeByDate(address account, uint256 date, uint256 startDate, uint256 blockNumber) internal view
returns(power uint96)

Arguments

Name Type Description
account address The user address.
date uint256 The staking date to compute the power for.
startDate uint256 The date for which we need to know the power of the stake.
blockNumber uint256 The block number, needed for checkpointing.

Returns

The staking power.

Source Code
function _weightedStakeByDate(
        address account,
        uint256 date,
        uint256 startDate,
        uint256 blockNumber
    ) internal view returns (uint96 power) {
        uint96 staked = _getPriorUserStakeByDate(account, date, blockNumber);
        if (staked > 0) {
            uint96 weight = _computeWeightByDate(date, startDate);
            power = mul96(staked, weight, "mul overflow") / WEIGHT_FACTOR; // WS13
        } else {
            power = 0;
        }
    }

computeWeightByDate

Compute the weight for a specific date.

function computeWeightByDate(uint256 date, uint256 startDate) external pure
returns(weight uint96)

Arguments

Name Type Description
date uint256 The unlocking date.
startDate uint256 We compute the weight for the tokens staked until 'date' on 'startDate'.

Returns

The weighted stake the account had as of the given block.

Source Code
function computeWeightByDate(uint256 date, uint256 startDate)
        external
        pure
        returns (uint96 weight)
    {
        return _computeWeightByDate(date, startDate);
    }

getFunctionsList

⤾ overrides IFunctionsList.getFunctionsList

function getFunctionsList() external pure
returns(bytes4[])
Source Code
function getFunctionsList() external pure returns (bytes4[] memory) {
        bytes4[] memory functionsList = new bytes4[](3);
        functionsList[0] = this.getPriorWeightedStake.selector;
        functionsList[1] = this.weightedStakeByDate.selector;
        functionsList[2] = this.computeWeightByDate.selector;
        return functionsList;
    }

Contracts