Playground
cliffvesting.gno
// Package cliffvesting computes cliff-then-linear vesting schedules as a PURE
// calculator — no state, no balances, no transfers.
//
// The shape is the standard employee/token grant: nothing vests until the
// cliff, the cliff releases the whole elapsed portion at once, and the rest
// accrues linearly until the end of the term. Keeping it pure is deliberate:
// the arithmetic is the part that is easy to get subtly wrong and easy to test,
// while custody belongs to the realm holding the coins.
//
// Everything is integer arithmetic. Vested is computed as
// total*elapsed/duration with the multiplication FIRST, so the usual rounding
// bug — dividing before multiplying and truncating the rate to zero — cannot
// happen. Rounding is always DOWN, which means the beneficiary never receives
// more than they have earned and the final instalment collects the remainder;
// at t >= end the result is exactly total, never total-1.
//
// Times are int64 so the caller can use block heights or unix seconds. The unit
// only has to be consistent.
//
// A live demo of this package is at
// [r/moul/x/daily/cliffvestingdemo](/r/nym-moultest123/x/daily/cliffvestingdemo/v0).
package cliffvesting
import "errors"
var (
ErrBadTotal = errors.New("cliffvesting: total must be positive")
ErrBadDuration = errors.New("cliffvesting: duration must be positive")
ErrCliffAfter = errors.New("cliffvesting: cliff must not fall after the end")
ErrCliffBefore = errors.New("cliffvesting: cliff must not fall before the start")
)
// Schedule is a cliff-then-linear vesting plan. Construct with New so the
// invariants are checked once.
type Schedule struct {
Total int64 // total amount to vest
Start int64 // vesting begins
Cliff int64 // nothing is claimable before this
End int64 // fully vested at or after this
}
// New validates and returns a Schedule. cliff must lie within [start, end].
// Passing cliff == start means "no cliff".
func New(total, start, cliff, end int64) (Schedule, error) {
s := Schedule{Total: total, Start: start, Cliff: cliff, End: end}
if total <= 0 {
return Schedule{}, ErrBadTotal
}
if end <= start {
return Schedule{}, ErrBadDuration
}
if cliff > end {
return Schedule{}, ErrCliffAfter
}
if cliff < start {
return Schedule{}, ErrCliffBefore
}
return s, nil
}
// NewLinear is New with no cliff.
func NewLinear(total, start, end int64) (Schedule, error) {
return New(total, start, start, end)
}
// Duration returns the length of the vesting term.
func (s Schedule) Duration() int64 { return s.End - s.Start }
// HasCliff reports whether the schedule has a non-trivial cliff.
func (s Schedule) HasCliff() bool { return s.Cliff > s.Start }
// Vested returns how much has vested at time t. Zero before the cliff, exactly
// Total at or after End, and floor(total*elapsed/duration) in between.
func (s Schedule) Vested(t int64) int64 {
if t < s.Cliff || t < s.Start {
return 0
}
if t >= s.End {
return s.Total
}
elapsed := t - s.Start
// Multiply BEFORE dividing: the reverse truncates the per-tick rate to
// zero whenever total < duration, which is the classic vesting bug.
return s.Total * elapsed / s.Duration()
}
// Unvested returns the remainder still locked at time t.
func (s Schedule) Unvested(t int64) int64 { return s.Total - s.Vested(t) }
// Claimable returns what can be withdrawn at time t given how much has already
// been claimed. Never negative, even if claimed somehow exceeds vested.
func (s Schedule) Claimable(t, claimed int64) int64 {
v := s.Vested(t) - claimed
if v < 0 {
return 0
}
return v
}
// IsFullyVested reports whether the term has completed at time t.
func (s Schedule) IsFullyVested(t int64) bool { return t >= s.End }
// PercentVested returns the vested share at t as an integer percentage,
// rounded down. Integer-only: no floats reach consensus state.
func (s Schedule) PercentVested(t int64) int64 {
return s.Vested(t) * 100 / s.Total
}
// CliffAmount returns the lump sum released the instant the cliff is reached.
func (s Schedule) CliffAmount() int64 { return s.Vested(s.Cliff) }
cliffvesting_test.gno
package cliffvesting
import (
"testing"
"gno.land/p/nt/uassert/v0"
)
func mustNew(t *testing.T, total, start, cliff, end int64) Schedule {
s, err := New(total, start, cliff, end)
uassert.NoError(t, err)
return s
}
func TestNewValidation(t *testing.T) {
_, err := New(0, 0, 0, 100)
uassert.ErrorIs(t, err, ErrBadTotal)
_, err = New(-5, 0, 0, 100)
uassert.ErrorIs(t, err, ErrBadTotal)
_, err = New(100, 50, 50, 50)
uassert.ErrorIs(t, err, ErrBadDuration, "end must be after start")
_, err = New(100, 50, 50, 10)
uassert.ErrorIs(t, err, ErrBadDuration)
_, err = New(100, 0, 200, 100)
uassert.ErrorIs(t, err, ErrCliffAfter)
_, err = New(100, 50, 10, 100)
uassert.ErrorIs(t, err, ErrCliffBefore)
_, err = New(100, 0, 100, 100)
uassert.NoError(t, err, "a cliff exactly at the end is allowed")
}
func TestNewLinearHasNoCliff(t *testing.T) {
s, err := NewLinear(1000, 0, 100)
uassert.NoError(t, err)
uassert.False(t, s.HasCliff())
uassert.Equal(t, int64(0), s.CliffAmount())
uassert.Equal(t, int64(500), s.Vested(50), "linear from the start")
}
// TestNothingBeforeCliff is the defining behaviour.
func TestNothingBeforeCliff(t *testing.T) {
s := mustNew(t, 1200, 0, 300, 1200)
uassert.Equal(t, int64(0), s.Vested(0))
uassert.Equal(t, int64(0), s.Vested(299), "one tick before the cliff, still nothing")
uassert.Equal(t, int64(1200), s.Unvested(299))
}
// TestCliffReleasesElapsedPortionAtOnce pins the step, not a ramp.
func TestCliffReleasesElapsedPortionAtOnce(t *testing.T) {
s := mustNew(t, 1200, 0, 300, 1200)
uassert.Equal(t, int64(300), s.Vested(300), "25% of the term elapsed, released in one step")
uassert.Equal(t, int64(300), s.CliffAmount())
uassert.Equal(t, int64(25), s.PercentVested(300))
}
func TestLinearAfterCliff(t *testing.T) {
s := mustNew(t, 1200, 0, 300, 1200)
uassert.Equal(t, int64(600), s.Vested(600))
uassert.Equal(t, int64(900), s.Vested(900))
uassert.Equal(t, int64(1199), s.Vested(1199))
}
// TestExactlyTotalAtEnd is what rounding down must never break.
func TestExactlyTotalAtEnd(t *testing.T) {
s := mustNew(t, 1000, 0, 0, 3) // 1000/3 does not divide evenly
uassert.Equal(t, int64(333), s.Vested(1))
uassert.Equal(t, int64(666), s.Vested(2))
uassert.Equal(t, int64(1000), s.Vested(3), "the last instalment collects the remainder")
uassert.Equal(t, int64(0), s.Unvested(3))
uassert.Equal(t, int64(1000), s.Vested(99999), "still exactly total long after the end")
}
// TestSmallTotalLongDuration is the rounding bug this package exists to avoid:
// with total < duration, dividing before multiplying truncates the rate to 0
// and nothing ever vests.
func TestSmallTotalLongDuration(t *testing.T) {
s := mustNew(t, 7, 0, 0, 1000)
uassert.Equal(t, int64(0), s.Vested(100), "7*100/1000 = 0.7, floors to 0")
uassert.Equal(t, int64(1), s.Vested(150), "7*150/1000 = 1.05, floors to 1")
uassert.Equal(t, int64(3), s.Vested(500), "half the term, half of 7, floored")
uassert.Equal(t, int64(7), s.Vested(1000), "and still exactly total at the end")
}
func TestVestedIsMonotonic(t *testing.T) {
s := mustNew(t, 997, 10, 40, 310)
prev := int64(-1)
for tick := int64(0); tick <= 320; tick++ {
v := s.Vested(tick)
uassert.True(t, v >= prev, "vested must never decrease")
uassert.True(t, v <= s.Total, "vested must never exceed total")
prev = v
}
}
func TestUnvestedComplementsVested(t *testing.T) {
s := mustNew(t, 500, 0, 100, 400)
for _, tick := range []int64{0, 99, 100, 250, 399, 400, 500} {
uassert.Equal(t, s.Total, s.Vested(tick)+s.Unvested(tick),
"vested + unvested must always equal total")
}
}
func TestClaimable(t *testing.T) {
s := mustNew(t, 1000, 0, 0, 100)
uassert.Equal(t, int64(500), s.Claimable(50, 0))
uassert.Equal(t, int64(300), s.Claimable(50, 200), "already-claimed is deducted")
uassert.Equal(t, int64(0), s.Claimable(50, 500), "nothing left right now")
uassert.Equal(t, int64(500), s.Claimable(100, 500), "the rest at the end")
}
// TestClaimableNeverNegative guards the case where bookkeeping says more was
// claimed than has vested — the caller gets 0, not a negative payout.
func TestClaimableNeverNegative(t *testing.T) {
s := mustNew(t, 1000, 0, 0, 100)
uassert.Equal(t, int64(0), s.Claimable(10, 999))
}
func TestPercentVested(t *testing.T) {
s := mustNew(t, 1000, 0, 0, 100)
uassert.Equal(t, int64(0), s.PercentVested(0))
uassert.Equal(t, int64(50), s.PercentVested(50))
uassert.Equal(t, int64(99), s.PercentVested(99))
uassert.Equal(t, int64(100), s.PercentVested(100))
uassert.Equal(t, int64(100), s.PercentVested(1000))
}
// TestNonZeroStart checks the schedule is relative to Start, not to zero.
func TestNonZeroStart(t *testing.T) {
s := mustNew(t, 400, 1000, 1100, 1400)
uassert.Equal(t, int64(0), s.Vested(999), "before the start")
uassert.Equal(t, int64(0), s.Vested(1099), "before the cliff")
uassert.Equal(t, int64(100), s.Vested(1100), "cliff: a quarter elapsed")
uassert.Equal(t, int64(200), s.Vested(1200))
uassert.Equal(t, int64(400), s.Vested(1400))
}
func TestDurationAndFlags(t *testing.T) {
s := mustNew(t, 100, 10, 20, 110)
uassert.Equal(t, int64(100), s.Duration())
uassert.True(t, s.HasCliff())
uassert.False(t, s.IsFullyVested(109))
uassert.True(t, s.IsFullyVested(110))
uassert.True(t, s.IsFullyVested(999))
}
// TestCliffAtEndIsAllOrNothing covers the degenerate schedule.
func TestCliffAtEndIsAllOrNothing(t *testing.T) {
s := mustNew(t, 100, 0, 100, 100)
uassert.Equal(t, int64(0), s.Vested(99))
uassert.Equal(t, int64(100), s.Vested(100))
uassert.Equal(t, int64(100), s.CliffAmount())
}
gnomod.toml
module = "gno.land/p/nym-moultest123/x/daily/cliffvesting/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186405 max_deposit = "5000000ugnot"