Skip to main content

TokenVestingLib

Central library containing vesting calculations and data structures used across all vesting contracts in the TokenOps system.

Library Information

File Location: contracts/libraries/TokenVestingLib.sol

Usage Pattern:

using TokenVestingLib for TokenVestingLib.Vesting;

Overview

The TokenVestingLib library serves as the core computational engine for major vesting operations in the TokenOps ecosystem. It provides gas-optimized calculations, standardized data structures, and consistent vesting logic across all manager types.

Key Features

  • Gas-optimized calculations for vesting amounts with minimal computational overhead
  • Packed struct design for efficient storage and reduced gas costs
  • Consistent vesting logic across all manager types (standard, native, votes, milestone)
  • Flexible schedule support with cliff periods, linear vesting, and initial unlocks
  • Precision handling with truncated calculations for predictable release intervals

Data Structures

Vesting Struct

The core data structure representing a complete vesting schedule:

struct Vesting {
address recipient;
uint32 startTimestamp;
uint32 endTimestamp;
uint32 deactivationTimestamp;
uint32 timelock;
uint32 releaseIntervalSecs;
uint32 cliffReleaseTimestamp;
uint256 initialUnlock;
uint256 cliffAmount;
uint256 linearVestAmount;
uint256 claimedAmount;
bool isRevocable;
}

Field Descriptions:

FieldTypeDescriptionConstraints
recipientaddressBeneficiary who can claim vested tokensCannot be zero address
startTimestampuint32When vesting schedule beginsMust be > 0, ≤ endTimestamp
endTimestampuint32When vesting schedule completesMust be ≥ startTimestamp
deactivationTimestampuint32When vesting was revoked (0 = active)Set by revocation, freezes vesting
timelockuint32Earliest time tokens can be claimedOptional delay before first claim
releaseIntervalSecsuint32Frequency of linear releasesMust be > 0, divides vesting period
cliffReleaseTimestampuint32When cliff amount is releasedBetween start and end if used
initialUnlockuint256Tokens unlocked immediately at startAvailable regardless of time
cliffAmountuint256Tokens released at cliff timestampReleased all at once
linearVestAmountuint256Tokens vested linearly over timeReleased in intervals
claimedAmountuint256Tokens already withdrawnIncreases with claims
isRevocableboolWhether admin can revoke vestingDetermines revocation capability

VestingParams Struct

Parameter structure for vesting creation to avoid stack-too-deep errors:

struct VestingParams {
bytes32 _vestingId; // Unique vesting identifier
address _recipient; // Beneficiary address
uint32 _startTimestamp; // Vesting start time
uint32 _endTimestamp; // Vesting end time
uint32 _timelock; // Claim timelock
uint256 _initialUnlock; // Initial unlock amount
uint32 _cliffReleaseTimestamp; // Cliff release time
uint256 _cliffAmount; // Cliff amount
uint32 _releaseIntervalSecs; // Release interval
uint256 _linearVestAmount; // Linear vesting amount
bool _isRevocable; // Revocable flag
}

Purpose: This struct is used exclusively for passing parameters to vesting creation functions, avoiding Solidity's stack depth limitations while maintaining code readability.

Functions

calculateVestedAmount

Calculate the vested amount for a given Vesting, at a given timestamp.

function calculateVestedAmount(Vesting memory _vesting, uint32 _referenceTimestamp) internal pure returns (uint256);

Parameters

NameTypeDescription
_vestingVestingThe vesting in question
_referenceTimestampuint32Timestamp for which we're calculating

The TokenVestingLib library provides the mathematical foundation for vesting operations in the TokenOps ecosystem, ensuring consistent, efficient, and secure token distribution across all manager types.