Sub topic 11
How Vesting Contracts Work On-Chain
Assume you understand wallets, token transfers, timestamps, and basic Python. This chapter focuses on how a vesting schedule becomes contract state and how to verify that the contract follows its published terms.
A vesting contract holds tokens and releases them according to rules. The beneficiary usually calls a release function. The contract calculates how many tokens have vested, subtracts the amount already released, and transfers only the difference.
The working model
A schedule normally stores:
- The beneficiary address.
- The total amount assigned.
- A start time.
- A cliff duration.
- A total vesting duration.
- The amount already released.
- Sometimes a revocable flag or a separate owner address.
The contract does not need to transfer tokens at every moment. Instead, it calculates the amount owed when someone calls release.
For a simple cliff-plus-linear schedule:
- Before
start + cliff, the vested amount is zero. - After the cliff, the vested amount increases over time.
- At
start + duration, the entire allocation is vested. - The releasable amount is vested amount minus previously released amount.
def vested(total, start, cliff, duration, now):
"""Return the amount vested at `now`, using integer token units."""
if now < start + cliff:
return 0
if now >= start + duration:
return total
return total * (now - start) // duration
total = 1_200
released = 0
for month in (6, 12, 24, 48):
available = vested(total, 0, 12, 48, month) - released
print(f"month {month}: vested={vested(total, 0, 12, 48, month)}, "
f"claimable={available}")The on-chain version uses the token's smallest unit, such as wei-like integer units. It should not use floating-point numbers. Integer division rounds down, so a small remainder can remain until the schedule completes.
What happens during a release
A typical transaction follows this sequence:
The contract should update its accounting before making the external token transfer. This follows the checks-effects-interactions pattern and reduces reentrancy risk. A production contract may also use a reentrancy guard and check that the token transfer succeeded.
The contract normally uses block.timestamp as its clock. Block producers can influence this value within limited bounds, so timestamps are suitable for month-scale vesting but not for precise games or auctions. A schedule based on block numbers behaves differently across networks because block times vary.
Common contract designs and errors
| Design choice | What to check |
|---|---|
| One schedule per beneficiary | Confirm whether multiple grants are combined or tracked separately |
| Contract custody | Verify the contract actually owns enough tokens |
| Revocable schedule | Read who can revoke it and where unreleased tokens go |
| Transferable beneficiary | Check whether the beneficiary can be changed |
| Token compatibility | Confirm the token follows expected transfer behavior |
| Timestamp arithmetic | Check cliff and duration units and boundary comparisons |
A contract can be mathematically correct but still fail operationally. For example, a project may publish a 12-month cliff while the deployed contract uses a timestamp that is already several months old. A contract may also have the right schedule but hold too few tokens to pay every beneficiary.
Inspect the deployed bytecode or verified source, not only a dashboard. Compare constructor arguments or stored variables with the published allocation table. Then inspect token balances, emitted release events, and previous transfers.
Common errors include treating a cliff as an extra period after the total duration, confusing vested tokens with claimable tokens, and ignoring tokens released at the token generation event. Also check whether “fully vested” means released to the wallet or merely available for claiming.
Exercise
A beneficiary has a 1,000-token allocation, a 10-month cliff, and a 40-month total duration. At month 20, 100 tokens have already been released. How many additional tokens are claimable under linear vesting from the start?
Show answer
The vested amount is 1,000 × 20 ÷ 40 = 500 tokens. The claimable amount is 500 − 100 = 400 tokens.