Skip to content

Latest commit

 

History

History
749 lines (624 loc) · 25 KB

CheckpointsShared.md

File metadata and controls

749 lines (624 loc) · 25 KB

Checkpoints contract. (CheckpointsShared.sol)

View Source: contracts/governance/Staking/modules/shared/CheckpointsShared.sol

↗ Extends: StakingStorageShared, SafeMath96 ↘ Derived Contracts: StakingGovernanceModule, StakingStakeModule, StakingWithdrawModule, WeightedStakingModule

CheckpointsShared contract

Increases and decreases storage values for users, delegatees and total daily stake.

Events

event DelegateChanged(address indexed delegator, uint256  lockedUntil, address indexed fromDelegate, address indexed toDelegate);
event DelegateStakeChanged(address indexed delegate, uint256  lockedUntil, uint256  previousBalance, uint256  newBalance);
event TokensStaked(address indexed staker, uint256  amount, uint256  lockedUntil, uint256  totalStaked);
event StakingWithdrawn(address indexed staker, uint256  amount, uint256  until, address indexed receiver, bool  isGovernance);
event VestingTokensWithdrawn(address  vesting, address  receiver);
event TokensUnlocked(uint256  amount);
event ExtendedStakingDuration(address indexed staker, uint256  previousDate, uint256  newDate, uint256  amountStaked);
event AdminAdded(address  admin);
event AdminRemoved(address  admin);
event PauserAddedOrRemoved(address indexed pauser, bool indexed added);
event StakingPaused(bool indexed setPaused);
event StakingFrozen(bool indexed setFrozen);
event ContractCodeHashAdded(bytes32  hash);
event ContractCodeHashRemoved(bytes32  hash);
event VestingStakeSet(uint256  lockedTS, uint96  value);
event TeamVestingCancelled(address indexed caller, address  receiver);
event TeamVestingPartiallyCancelled(address indexed caller, address  receiver, uint256  lastProcessedDate);

Functions


constructor

function () internal nonpayable
Source Code
constructor() internal {
        // abstract
    }

_increaseVestingStake

Increases the user's vesting stake for a giving lock date and writes a checkpoint.

function _increaseVestingStake(uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
value uint96 The value to add to the staked balance.
Source Code
function _increaseVestingStake(uint256 lockedTS, uint96 value) internal {
        uint32 nCheckpoints = numVestingCheckpoints[lockedTS];
        uint96 vested = vestingCheckpoints[lockedTS][nCheckpoints - 1].stake;
        uint96 newVest = add96(vested, value, "CP01"); // vested overflow
        _writeVestingCheckpoint(lockedTS, nCheckpoints, newVest);
    }

_decreaseVestingStake

Decreases the user's vesting stake for a giving lock date and writes a checkpoint.

function _decreaseVestingStake(uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
value uint96 The value to substract to the staked balance.
Source Code
function _decreaseVestingStake(uint256 lockedTS, uint96 value) internal {
        uint32 nCheckpoints = numVestingCheckpoints[lockedTS];
        uint96 vested = vestingCheckpoints[lockedTS][nCheckpoints - 1].stake;
        uint96 newVest = sub96(vested, value, "CP02"); // vested underflow
        _writeVestingCheckpoint(lockedTS, nCheckpoints, newVest);
    }

_writeVestingCheckpoint

Writes on storage the user vested amount.

function _writeVestingCheckpoint(uint256 lockedTS, uint32 nCheckpoints, uint96 newVest) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
nCheckpoints uint32 The number of checkpoints, to find out the last one index.
newVest uint96 The new vest balance.
Source Code
function _writeVestingCheckpoint(
        uint256 lockedTS,
        uint32 nCheckpoints,
        uint96 newVest
    ) internal {
        uint32 blockNumber = safe32(block.number, "CP03"); // block num > 32 bits

        if (
            nCheckpoints > 0 &&
            vestingCheckpoints[lockedTS][nCheckpoints - 1].fromBlock == blockNumber
        ) {
            vestingCheckpoints[lockedTS][nCheckpoints - 1].stake = newVest;
        } else {
            vestingCheckpoints[lockedTS][nCheckpoints] = Checkpoint(blockNumber, newVest);
            numVestingCheckpoints[lockedTS] = nCheckpoints + 1;
        }
    }

_increaseUserStake

Increases the user's stake for a giving lock date and writes a checkpoint.

function _increaseUserStake(address account, uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
account address The user address.
lockedTS uint256 The lock date.
value uint96 The value to add to the staked balance.
Source Code
function _increaseUserStake(
        address account,
        uint256 lockedTS,
        uint96 value
    ) internal {
        uint32 nCheckpoints = numUserStakingCheckpoints[account][lockedTS];
        uint96 staked = userStakingCheckpoints[account][lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = add96(staked, value, "CP04"); // staked overflow
        _writeUserCheckpoint(account, lockedTS, nCheckpoints, newStake);
    }

_decreaseUserStake

Decreases the user's stake for a giving lock date and writes a checkpoint.

function _decreaseUserStake(address account, uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
account address The user address.
lockedTS uint256 The lock date.
value uint96 The value to substract to the staked balance.
Source Code
function _decreaseUserStake(
        address account,
        uint256 lockedTS,
        uint96 value
    ) internal {
        uint32 nCheckpoints = numUserStakingCheckpoints[account][lockedTS];
        uint96 staked = userStakingCheckpoints[account][lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = sub96(staked, value, "CP05"); // staked underflow
        _writeUserCheckpoint(account, lockedTS, nCheckpoints, newStake);
    }

_writeUserCheckpoint

Writes on storage the user stake.

function _writeUserCheckpoint(address account, uint256 lockedTS, uint32 nCheckpoints, uint96 newStake) internal nonpayable

Arguments

Name Type Description
account address The user address.
lockedTS uint256 The lock date.
nCheckpoints uint32 The number of checkpoints, to find out the last one index.
newStake uint96 The new staked balance.
Source Code
function _writeUserCheckpoint(
        address account,
        uint256 lockedTS,
        uint32 nCheckpoints,
        uint96 newStake
    ) internal {
        uint32 blockNumber = safe32(block.number, "CP06"); // block number > 32 bits

        if (
            nCheckpoints > 0 &&
            userStakingCheckpoints[account][lockedTS][nCheckpoints - 1].fromBlock == blockNumber
        ) {
            userStakingCheckpoints[account][lockedTS][nCheckpoints - 1].stake = newStake;
        } else {
            userStakingCheckpoints[account][lockedTS][nCheckpoints] = Checkpoint(
                blockNumber,
                newStake
            );
            numUserStakingCheckpoints[account][lockedTS] = nCheckpoints + 1;
        }
    }

_increaseDelegateStake

Increases the delegatee's stake for a giving lock date and writes a checkpoint.

function _increaseDelegateStake(address delegatee, uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
delegatee address The delegatee address.
lockedTS uint256 The lock date.
value uint96 The value to add to the staked balance.
Source Code
function _increaseDelegateStake(
        address delegatee,
        uint256 lockedTS,
        uint96 value
    ) internal {
        uint32 nCheckpoints = numDelegateStakingCheckpoints[delegatee][lockedTS];
        uint96 staked = delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = add96(staked, value, "CP07"); // block number > 32 bits
        _writeDelegateCheckpoint(delegatee, lockedTS, nCheckpoints, newStake);
    }

_decreaseDelegateStake

Decreases the delegatee's stake for a giving lock date and writes a checkpoint.

function _decreaseDelegateStake(address delegatee, uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
delegatee address The delegatee address.
lockedTS uint256 The lock date.
value uint96 The value to substract to the staked balance.
Source Code
function _decreaseDelegateStake(
        address delegatee,
        uint256 lockedTS,
        uint96 value
    ) internal {
        uint32 nCheckpoints = numDelegateStakingCheckpoints[delegatee][lockedTS];
        uint96 staked = delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = 0;
        // @dev We need to check delegate checkpoint value here,
        //		because we had an issue in `stake` function:
        //		delegate checkpoint wasn't updating for the second and next stakes for the same date
        //		if first stake was withdrawn completely and stake was delegated to the staker
        //		(no delegation to another address).
        // @dev It can be greater than 0, but inconsistent after 3 transactions
        if (staked > value) {
            newStake = sub96(staked, value, "CP08"); // staked underflow
        }
        _writeDelegateCheckpoint(delegatee, lockedTS, nCheckpoints, newStake);
    }

_writeDelegateCheckpoint

Writes on storage the delegate stake.

function _writeDelegateCheckpoint(address delegatee, uint256 lockedTS, uint32 nCheckpoints, uint96 newStake) internal nonpayable

Arguments

Name Type Description
delegatee address The delegate address.
lockedTS uint256 The lock date.
nCheckpoints uint32 The number of checkpoints, to find out the last one index.
newStake uint96 The new staked balance.
Source Code
function _writeDelegateCheckpoint(
        address delegatee,
        uint256 lockedTS,
        uint32 nCheckpoints,
        uint96 newStake
    ) internal {
        uint32 blockNumber = safe32(block.number, "CP09"); // block numb > 32 bits
        uint96 oldStake = delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints - 1].stake;

        if (
            nCheckpoints > 0 &&
            delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints - 1].fromBlock ==
            blockNumber
        ) {
            delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints - 1].stake = newStake;
        } else {
            delegateStakingCheckpoints[delegatee][lockedTS][nCheckpoints] = Checkpoint(
                blockNumber,
                newStake
            );
            numDelegateStakingCheckpoints[delegatee][lockedTS] = nCheckpoints + 1;
        }
        emit DelegateStakeChanged(delegatee, lockedTS, oldStake, newStake);
    }

_increaseDailyStake

Increases the total stake for a giving lock date and writes a checkpoint.

function _increaseDailyStake(uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
value uint96 The value to add to the staked balance.
Source Code
function _increaseDailyStake(uint256 lockedTS, uint96 value) internal {
        uint32 nCheckpoints = numTotalStakingCheckpoints[lockedTS];
        uint96 staked = totalStakingCheckpoints[lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = add96(staked, value, "CP10"); // staked overflow
        _writeStakingCheckpoint(lockedTS, nCheckpoints, newStake);
    }

_decreaseDailyStake

Decreases the total stake for a giving lock date and writes a checkpoint.

function _decreaseDailyStake(uint256 lockedTS, uint96 value) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
value uint96 The value to substract to the staked balance.
Source Code
function _decreaseDailyStake(uint256 lockedTS, uint96 value) internal {
        uint32 nCheckpoints = numTotalStakingCheckpoints[lockedTS];
        uint96 staked = totalStakingCheckpoints[lockedTS][nCheckpoints - 1].stake;
        uint96 newStake = sub96(staked, value, "CP11"); // staked underflow
        _writeStakingCheckpoint(lockedTS, nCheckpoints, newStake);
    }

_writeStakingCheckpoint

Writes on storage the total stake.

function _writeStakingCheckpoint(uint256 lockedTS, uint32 nCheckpoints, uint96 newStake) internal nonpayable

Arguments

Name Type Description
lockedTS uint256 The lock date.
nCheckpoints uint32 The number of checkpoints, to find out the last one index.
newStake uint96 The new staked balance.
Source Code
function _writeStakingCheckpoint(
        uint256 lockedTS,
        uint32 nCheckpoints,
        uint96 newStake
    ) internal {
        uint32 blockNumber = safe32(block.number, "CP12"); // block num > 32 bits

        if (
            nCheckpoints > 0 &&
            totalStakingCheckpoints[lockedTS][nCheckpoints - 1].fromBlock == blockNumber
        ) {
            totalStakingCheckpoints[lockedTS][nCheckpoints - 1].stake = newStake;
        } else {
            totalStakingCheckpoints[lockedTS][nCheckpoints] = Checkpoint(blockNumber, newStake);
            numTotalStakingCheckpoints[lockedTS] = nCheckpoints + 1;
        }
    }

_currentBalance

Get the current balance of an account locked until a certain date.

function _currentBalance(address account, uint256 lockDate) internal view
returns(uint96)

Arguments

Name Type Description
account address The user address.
lockDate uint256 The lock date.

Returns

The stake amount.

Source Code
function _currentBalance(address account, uint256 lockDate) internal view returns (uint96) {
        uint32 _numUnserStakingCheckpoints = numUserStakingCheckpoints[account][lockDate] - 1;
        return userStakingCheckpoints[account][lockDate][_numUnserStakingCheckpoints].stake;
    }

Contracts