Skip to content

Latest commit

 

History

History
804 lines (654 loc) · 25.7 KB

FourYearVestingLogic.md

File metadata and controls

804 lines (654 loc) · 25.7 KB

Four Year Vesting Logic contract. (FourYearVestingLogic.sol)

View Source: contracts/governance/Vesting/fouryear/FourYearVestingLogic.sol

↗ Extends: IFourYearVesting, FourYearVestingStorage, ApprovalReceiver

FourYearVestingLogic contract

Staking, delegating and withdrawal functionality.

Events

event TokensStaked(address indexed caller, uint256  amount);
event VotesDelegated(address indexed caller, address  delegatee);
event TokensWithdrawn(address indexed caller, address  receiver);
event DividendsCollected(address indexed caller, address  loanPoolToken, address  receiver, uint32  maxCheckpoints);
event MigratedToNewStakingContract(address indexed caller, address  newStakingContract);
event TokenOwnerChanged(address indexed newOwner, address indexed oldOwner);

Modifiers

onlyOwners

Throws if called by any account other than the token owner or the contract owner.

modifier onlyOwners() internal

onlyTokenOwner

Throws if called by any account other than the token owner.

modifier onlyTokenOwner() internal

Functions


setMaxInterval

Sets the max interval.

function setMaxInterval(uint256 _interval) external nonpayable onlyOwner 

Arguments

Name Type Description
_interval uint256 Max interval for which tokens scheduled shall be staked.
Source Code
function setMaxInterval(uint256 _interval) external onlyOwner {
        require(_interval.mod(FOUR_WEEKS) == 0, "invalid interval");
        maxInterval = _interval;
    }

stakeTokens

⤾ overrides IFourYearVesting.stakeTokens

Stakes tokens according to the vesting schedule.

function stakeTokens(uint256 _amount, uint256 _restartStakeSchedule) external nonpayable
returns(lastSchedule uint256, remainingAmount uint256)

Arguments

Name Type Description
_amount uint256 The amount of tokens to stake.
_restartStakeSchedule uint256 The time from which staking schedule restarts. The issue is that we can only stake tokens for a max duration. Thus, we need to restart from the lastSchedule.

Returns

lastSchedule The max duration for which tokens were staked.

Source Code
function stakeTokens(uint256 _amount, uint256 _restartStakeSchedule)
        external
        returns (uint256 lastSchedule, uint256 remainingAmount)
    {
        (lastSchedule, remainingAmount) = _stakeTokens(msg.sender, _amount, _restartStakeSchedule);
    }

stakeTokensWithApproval

Stakes tokens according to the vesting schedule.

function stakeTokensWithApproval(address _sender, uint256 _amount, uint256 _restartStakeSchedule) external nonpayable onlyThisContract 
returns(lastSchedule uint256, remainingAmount uint256)

Arguments

Name Type Description
_sender address The sender of SOV.approveAndCall
_amount uint256 The amount of tokens to stake.
_restartStakeSchedule uint256 The time from which staking schedule restarts. The issue is that we can only stake tokens for a max duration. Thus, we need to restart from the lastSchedule.

Returns

lastSchedule The max duration for which tokens were staked.

Source Code
function stakeTokensWithApproval(
        address _sender,
        uint256 _amount,
        uint256 _restartStakeSchedule
    ) external onlyThisContract returns (uint256 lastSchedule, uint256 remainingAmount) {
        (lastSchedule, remainingAmount) = _stakeTokens(_sender, _amount, _restartStakeSchedule);
    }

delegate

Delegate votes from msg.sender which are locked until lockDate to delegatee.

function delegate(address _delegatee) external nonpayable onlyTokenOwner 

Arguments

Name Type Description
_delegatee address The address to delegate votes to.
Source Code
function delegate(address _delegatee) external onlyTokenOwner {
        require(_delegatee != address(0), "delegatee address invalid");
        uint256 stakingEndDate = endDate;
        /// @dev Withdraw for each unlocked position.
        /// @dev Don't change FOUR_WEEKS to TWO_WEEKS, a lot of vestings already deployed with FOUR_WEEKS
        ///		workaround found, but it doesn't work with TWO_WEEKS
        for (uint256 i = startDate.add(cliff); i <= stakingEndDate; i += FOUR_WEEKS) {
            staking.delegate(_delegatee, i);
        }
        emit VotesDelegated(msg.sender, _delegatee);
    }

withdrawTokens

Withdraws unlocked tokens from the staking contract and forwards them to an address specified by the token owner.

function withdrawTokens(address receiver) external nonpayable onlyTokenOwner 

Arguments

Name Type Description
receiver address The receiving address.
Source Code
function withdrawTokens(address receiver) external onlyTokenOwner {
        _withdrawTokens(receiver, false);
    }

collectDividends

Collect dividends from fee sharing proxy.

function collectDividends(address _loanPoolToken, uint32 _maxCheckpoints, address _receiver) external nonpayable onlyTokenOwner 

Arguments

Name Type Description
_loanPoolToken address The loan pool token address.
_maxCheckpoints uint32 Maximum number of checkpoints to be processed.
_receiver address The receiver of tokens or msg.sender
Source Code
function collectDividends(
        address _loanPoolToken,
        uint32 _maxCheckpoints,
        address _receiver
    ) external onlyTokenOwner {
        require(_receiver != address(0), "receiver address invalid");

        /// @dev Invokes the fee sharing proxy.
        feeSharingCollector.withdraw(_loanPoolToken, _maxCheckpoints, _receiver);

        emit DividendsCollected(msg.sender, _loanPoolToken, _receiver, _maxCheckpoints);
    }

changeTokenOwner

Change token owner - only vesting owner is allowed to change.

function changeTokenOwner(address _newTokenOwner) public nonpayable onlyOwner 

Arguments

Name Type Description
_newTokenOwner address Address of new token owner.
Source Code
function changeTokenOwner(address _newTokenOwner) public onlyOwner {
        require(_newTokenOwner != address(0), "invalid new token owner address");
        require(_newTokenOwner != tokenOwner, "same owner not allowed");
        newTokenOwner = _newTokenOwner;
    }

approveOwnershipTransfer

Approve token owner change - only token Owner.

function approveOwnershipTransfer() public nonpayable onlyTokenOwner 
Source Code
function approveOwnershipTransfer() public onlyTokenOwner {
        require(newTokenOwner != address(0), "invalid address");
        tokenOwner = newTokenOwner;
        newTokenOwner = address(0);
        emit TokenOwnerChanged(tokenOwner, msg.sender);
    }

setImpl

Set address of the implementation - only Token Owner.

function setImpl(address _newImplementation) public nonpayable onlyTokenOwner 

Arguments

Name Type Description
_newImplementation address Address of the new implementation.
Source Code
function setImpl(address _newImplementation) public onlyTokenOwner {
        require(_newImplementation != address(0), "invalid new implementation address");
        newImplementation = _newImplementation;
    }

migrateToNewStakingContract

Allows the owners to migrate the positions to a new staking contract.

function migrateToNewStakingContract() external nonpayable onlyOwners 
Source Code
function migrateToNewStakingContract() external onlyOwners {
        staking.migrateToNewStakingContract();
        staking = IStaking(staking.newStakingContract());
        emit MigratedToNewStakingContract(msg.sender, address(staking));
    }

extendStaking

Extends stakes(unlocked till timeDuration) for four year vesting contracts.

function extendStaking() external nonpayable
Source Code
function extendStaking() external {
        uint256 timeDuration = startDate.add(extendDurationFor);
        uint256[] memory dates;
        uint96[] memory stakes;
        (dates, stakes) = staking.getStakes(address(this));

        for (uint256 i = 0; i < dates.length; i++) {
            if ((dates[i] < block.timestamp) && (dates[i] <= timeDuration) && (stakes[i] > 0)) {
                staking.extendStakingDuration(dates[i], dates[i].add(156 weeks));
                endDate = dates[i].add(156 weeks);
            } else {
                break;
            }
        }
    }

_stakeTokens

Stakes tokens according to the vesting schedule. Low level function.

function _stakeTokens(address _sender, uint256 _amount, uint256 _restartStakeSchedule) internal nonpayable
returns(lastSchedule uint256, remainingAmount uint256)

Arguments

Name Type Description
_sender address The sender of tokens to stake.
_amount uint256 The amount of tokens to stake.
_restartStakeSchedule uint256 The time from which staking schedule restarts. The issue is that we can only stake tokens for a max duration. Thus, we need to restart from the lastSchedule.

Returns

lastSchedule The max duration for which tokens were staked.

Source Code
function _stakeTokens(
        address _sender,
        uint256 _amount,
        uint256 _restartStakeSchedule
    ) internal returns (uint256 lastSchedule, uint256 remainingAmount) {
        // Creating a new staking schedule for the same vesting contract is disallowed unlike normal vesting
        require(
            (startDate == 0) ||
                (startDate > 0 && remainingStakeAmount > 0 && _restartStakeSchedule > 0),
            "create new vesting address"
        );
        uint256 restartDate;
        uint256 relativeAmount;
        // Calling the _stakeTokens function first time for the vesting contract
        // Runs for maxInterval only (consider maxInterval = 18 * 4 = 72 weeks)
        if (startDate == 0 && _restartStakeSchedule == 0) {
            startDate = staking.timestampToLockDate(block.timestamp); // Set only once
            durationLeft = duration; // We do not touch duration and cliff as they are used throughout
            cliffAdded = cliff; // Hence, durationLeft and cliffAdded is created
        }
        // Calling the _stakeTokens second/third time - we start from the end of previous interval
        // and the remaining amount(amount left after tokens are staked in the previous interval)
        if (_restartStakeSchedule > 0) {
            require(
                _restartStakeSchedule == lastStakingSchedule && _amount == remainingStakeAmount,
                "invalid params"
            );
            restartDate = _restartStakeSchedule;
        } else {
            restartDate = startDate;
        }
        // Runs only once when the _stakeTokens is called for the first time
        if (endDate == 0) {
            endDate = staking.timestampToLockDate(block.timestamp.add(duration));
        }
        uint256 addedMaxInterval = restartDate.add(maxInterval); // run for maxInterval
        if (addedMaxInterval < endDate) {
            // Runs for max interval
            lastStakingSchedule = addedMaxInterval;
            relativeAmount = (_amount.mul(maxInterval)).div(durationLeft); // (_amount * 18) / 39
            durationLeft = durationLeft.sub(maxInterval); // durationLeft - 18 periods(72 weeks)
            remainingStakeAmount = _amount.sub(relativeAmount); // Amount left to be staked in subsequent intervals
        } else {
            // Normal run
            lastStakingSchedule = endDate; // if staking intervals left < 18 periods(72 weeks)
            remainingStakeAmount = 0;
            durationLeft = 0;
            relativeAmount = _amount; // Stake all amount left
        }

        /// @dev Transfer the tokens to this contract.
        bool success = SOV.transferFrom(_sender, address(this), relativeAmount);
        require(success, "transfer failed");

        /// @dev Allow the staking contract to access them.
        SOV.approve(address(staking), relativeAmount);

        staking.stakesBySchedule(
            relativeAmount,
            cliffAdded,
            duration.sub(durationLeft),
            FOUR_WEEKS,
            address(this),
            tokenOwner
        );
        if (durationLeft == 0) {
            // All tokens staked
            cliffAdded = 0;
        } else {
            cliffAdded = cliffAdded.add(maxInterval); // Add cliff to the end of previous maxInterval
        }

        emit TokensStaked(_sender, relativeAmount);
        return (lastStakingSchedule, remainingStakeAmount);
    }

_withdrawTokens

Withdraws tokens from the staking contract and forwards them to an address specified by the token owner. Low level function.

function _withdrawTokens(address receiver, bool isGovernance) internal nonpayable

Arguments

Name Type Description
receiver address The receiving address.
isGovernance bool Whether all tokens (true) or just unlocked tokens (false).
Source Code
function _withdrawTokens(address receiver, bool isGovernance) internal {
        require(receiver != address(0), "receiver address invalid");

        uint96 stake;

        /// @dev Usually we just need to iterate over the possible dates until now.
        uint256 end;

        /// @dev In the unlikely case that all tokens have been unlocked early,
        ///   allow to withdraw all of them.
        if (staking.allUnlocked() || isGovernance) {
            end = endDate;
        } else {
            end = block.timestamp;
        }

        /// @dev Withdraw for each unlocked position.
        /// @dev Don't change FOUR_WEEKS to TWO_WEEKS, a lot of vestings already deployed with FOUR_WEEKS
        ///		workaround found, but it doesn't work with TWO_WEEKS
        /// @dev For four year vesting, withdrawal of stakes for the first year is not allowed. These
        /// stakes are extended for three years. In some cases the withdrawal may be allowed at a different
        /// time and hence we use extendDurationFor.
        for (uint256 i = startDate.add(extendDurationFor); i <= end; i += FOUR_WEEKS) {
            /// @dev Read amount to withdraw.
            stake = staking.getPriorUserStakeByDate(address(this), i, block.number.sub(1));

            /// @dev Withdraw if > 0
            if (stake > 0) {
                staking.withdraw(stake, i, receiver);
            }
        }

        emit TokensWithdrawn(msg.sender, receiver);
    }

_getToken

⤾ overrides ApprovalReceiver._getToken

Overrides default ApprovalReceiver._getToken function to register SOV token on this contract.

function _getToken() internal view
returns(address)
Source Code
function _getToken() internal view returns (address) {
        return address(SOV);
    }

_getSelectors

⤾ overrides ApprovalReceiver._getSelectors

Overrides default ApprovalReceiver._getSelectors function to register stakeTokensWithApproval selector on this contract.

function _getSelectors() internal pure
returns(bytes4[])
Source Code
function _getSelectors() internal pure returns (bytes4[] memory) {
        bytes4[] memory selectors = new bytes4[](1);
        selectors[0] = this.stakeTokensWithApproval.selector;
        return selectors;
    }

Contracts