Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

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.
//
// The multiplication goes through a 128-bit intermediate. v0 did it in plain
// int64, which silently WRAPPED once total*elapsed passed 2^63: over a
// two-year term in seconds that is any grant above ~146,036 whole coins, and
// a large one reported a NEGATIVE vested amount rather than failing. See
// [Schedule.Vested].
//
// 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"
	"math/bits"
)

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.
//
// The product goes through [mulDiv] rather than int64. v0 computed
// `s.Total * elapsed / s.Duration()` directly, which is correct only while the
// product fits: with the term in seconds, a 318,720,000-coin grant over two
// years returned -6,264,395,224. Nothing signalled it, because a wrapped
// int64 is still a valid int64.
func (s Schedule) Vested(t int64) int64 {
	if t < s.Cliff || t < s.Start {
		return 0
	}
	if t >= s.End {
		return s.Total
	}
	// Multiply BEFORE dividing: the reverse truncates the per-tick rate to
	// zero whenever total < duration, which is the classic vesting bug.
	return mulDiv(s.Total, t-s.Start, 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 mulDiv(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) }

// mulDiv computes floor(a*b/den) through a 128-bit intermediate, so a product
// that leaves int64 does not wrap.
//
// Every caller here passes 0 <= b <= den with den > 0 and a >= 0, which is
// what makes the bits.Div64 precondition hold: the quotient is then at most a,
// so it fits in 64 bits, which is exactly the hi < den that Div64 requires and
// panics without. New enforces Total > 0 and End > Start; Vested only reaches
// this line with Start < t < End.
func mulDiv(a, b, den int64) int64 {
	if a == 0 || b == 0 {
		return 0
	}
	hi, lo := bits.Mul64(uint64(a), uint64(b))
	q, _ := bits.Div64(hi, lo, uint64(den))
	return int64(q)
}

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())
}

// TestVestedDoesNotOverflow pins the defect that forced v1.
//
// v0 computed total*elapsed in plain int64. Over a term measured in seconds,
// the product passes 2^63 for any grant above roughly 146,036 whole coins, and
// the wrap is silent: the figures below came back NEGATIVE, which every caller
// then treated as a real vested amount.
func TestVestedDoesNotOverflow(t *testing.T) {
	// The real gno.land mainnet vesting term, 2026-09-12 to 2028-09-12.
	const (
		start int64 = 1789225200
		end   int64 = 1852383600
		term        = end - start // 63,158,400 seconds
	)

	for _, tc := range []struct {
		name  string
		total int64
	}{
		{"just under the v0 ceiling", 146035000000},
		{"just over the v0 ceiling", 146037000000},
		{"the largest genesis grant", 318720000000000},
		{"the whole genesis allocation", 1332999998328067},
	} {
		s, err := NewLinear(tc.total, start, end)
		uassert.NoError(t, err, tc.name)

		// Halfway through the term is exactly half the grant, and the naive
		// int64 product has long since wrapped by this size.
		uassert.Equal(t, tc.total/2, s.Vested(start+term/2), tc.name)

		// The curve stays inside its bounds and never goes backwards.
		prev := int64(0)
		for i := int64(0); i <= 8; i++ {
			got := s.Vested(start + term*i/8)
			uassert.True(t, got >= prev, tc.name+": vested went backwards")
			uassert.True(t, got >= 0, tc.name+": vested went negative")
			uassert.True(t, got <= tc.total, tc.name+": vested exceeded the total")
			prev = got
		}
		uassert.Equal(t, tc.total, prev, tc.name)

		// PercentVested multiplies by 100 on top, so it has its own ceiling.
		// It is 49 and not 50 at halfway for an ODD total: Vested floors to
		// (total-1)/2 and the percentage floors again, so two roundings in the
		// same direction land just under. That is the documented behaviour,
		// not the overflow, so the assertion allows both.
		half := s.PercentVested(start + term/2)
		uassert.True(t, half == 49 || half == 50, tc.name+": halfway percent left 49..50")
		uassert.Equal(t, int64(100), s.PercentVested(end), tc.name)
	}
}

gnomod.toml

module = "gno.land/p/nym-moultest123/x/daily/cliffvesting/v1"
gno = "0.9"

[addpkg]
  creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv"
  height = 186405
  max_deposit = "7000000ugnot"