Playground
cliffvestingdemo.gno
// Package cliffvestingdemo is a small gnoweb demo of the vesting calculator
// provided by the [p/moul/x/daily/cliffvesting](/p/nym-moultest123/x/daily/cliffvesting/v1)
// library: the cliff step, the linear ramp, integer rounding, and the two
// arithmetic bugs the library exists to avoid.
//
// It contains no vesting logic of its own and holds no coins. Stateless, so
// Render is deterministic, which is precisely what the library is for.
package cliffvestingdemo
import (
"strconv"
"strings"
"gno.land/p/nym-moultest123/x/daily/cliffvesting/v1"
)
// Render renders the demo for gnoweb.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Cliff Vesting\n\n")
b.WriteString("A pure vesting calculator, demoing the ")
b.WriteString("[`p/moul/x/daily/cliffvesting`](/p/nym-moultest123/x/daily/cliffvesting/v1) library.\n\n")
// 1200 tokens over 12 months, 3-month cliff.
s, err := cliffvesting.New(1200, 0, 3, 12)
if err != nil {
return "error: " + err.Error()
}
b.WriteString("## A 12-month grant with a 3-month cliff\n\n")
b.WriteString("`1200` tokens, `Start = 0`, `Cliff = 3`, `End = 12`.\n\n")
b.WriteString("| month | vested | unvested | % |\n|---|---|---|---|\n")
for m := int64(0); m <= 12; m++ {
note := ""
switch m {
case 2:
note = " ← nothing yet"
case 3:
note = " ← **cliff**"
case 12:
note = " ← fully vested"
}
b.WriteString("| " + strconv.FormatInt(m, 10) + " | " +
strconv.FormatInt(s.Vested(m), 10) + " | " +
strconv.FormatInt(s.Unvested(m), 10) + " | " +
strconv.FormatInt(s.PercentVested(m), 10) + "%" + note + " |\n")
}
b.WriteString("\nThe cliff is a **step, not a ramp**: at month 3 a quarter of the term ")
b.WriteString("has elapsed, so `")
b.WriteString(strconv.FormatInt(s.CliffAmount(), 10))
b.WriteString("` tokens unlock at once. After that it accrues linearly.\n\n")
b.WriteString("## Claiming\n\n")
b.WriteString("The library tracks no balances, so the caller supplies what has already ")
b.WriteString("been claimed:\n\n")
b.WriteString("| at month | already claimed | claimable |\n|---|---|---|\n")
for _, c := range [][2]int64{{6, 0}, {6, 300}, {6, 600}, {12, 600}} {
b.WriteString("| " + strconv.FormatInt(c[0], 10) + " | " +
strconv.FormatInt(c[1], 10) + " | " +
strconv.FormatInt(s.Claimable(c[0], c[1]), 10) + " |\n")
}
b.WriteString("\n## Integer rounding\n\n")
b.WriteString("All arithmetic is integer, no float ever reaches consensus state. ")
b.WriteString("Rounding is **down**, so nobody is ever paid more than they earned, ")
b.WriteString("and the final instalment collects the remainder.\n\n")
odd, _ := cliffvesting.NewLinear(1000, 0, 3)
b.WriteString("`1000` over `3` periods:\n\n")
b.WriteString("| t | vested |\n|---|---|\n")
for m := int64(0); m <= 3; m++ {
b.WriteString("| " + strconv.FormatInt(m, 10) + " | " +
strconv.FormatInt(odd.Vested(m), 10) + " |\n")
}
b.WriteString("\n`333 + 333 + 334`, and the end is exactly `1000`, never `999`.\n\n")
b.WriteString("## Bug 1: a rate truncated to zero\n\n")
tiny, _ := cliffvesting.NewLinear(7, 0, 1000)
b.WriteString("When the total is smaller than the duration, computing a per-tick rate ")
b.WriteString("first truncates it to zero and **nothing ever vests**. Multiplying ")
b.WriteString("before dividing keeps it honest. `7` tokens over `1000` ticks:\n\n")
b.WriteString("| t | vested |\n|---|---|\n")
for _, m := range []int64{100, 150, 500, 1000} {
b.WriteString("| " + strconv.FormatInt(m, 10) + " | " +
strconv.FormatInt(tiny.Vested(m), 10) + " |\n")
}
b.WriteString("\n## Bug 2: a product that leaves int64\n\n")
b.WriteString("Multiplying first is only safe if the product fits. Over a term ")
b.WriteString("measured in **seconds**, `total * elapsed` passes 2^63 for any grant ")
b.WriteString("above about `146,036` whole coins, and a wrapped int64 is still a ")
b.WriteString("valid int64: `v0` of this library returned a **negative** vested ")
b.WriteString("amount and nothing signalled it. `v1` routes the product through a ")
b.WriteString("128-bit intermediate.\n\n")
b.WriteString("A real two-year schedule, `1789225200` to `1852383600`, at the ")
b.WriteString("halfway mark:\n\n")
b.WriteString("| grant | vested at halfway | expected |\n|---|---|---|\n")
const (
vStart int64 = 1789225200
vEnd int64 = 1852383600
)
for _, total := range []int64{100000000000, 146037000000, 318720000000000} {
big, err := cliffvesting.NewLinear(total, vStart, vEnd)
if err != nil {
continue
}
b.WriteString("| " + strconv.FormatInt(total, 10) + " | " +
strconv.FormatInt(big.Vested(vStart+(vEnd-vStart)/2), 10) + " | " +
strconv.FormatInt(total/2, 10) + " |\n")
}
return b.String()
}
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/cliffvestingdemo/v1" gno = "0.9" private = true [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186410 max_deposit = "5000000ugnot"
render_example_test.gno
package cliffvestingdemo
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Cliff Vesting
//
// A pure vesting calculator, demoing the [`p/moul/x/daily/cliffvesting`](/p/nym-moultest123/x/daily/cliffvesting/v1) library.
//
// ## A 12-month grant with a 3-month cliff
//
// `1200` tokens, `Start = 0`, `Cliff = 3`, `End = 12`.
//
// | month | vested | unvested | % |
// |---|---|---|---|
// | 0 | 0 | 1200 | 0% |
// | 1 | 0 | 1200 | 0% |
// | 2 | 0 | 1200 | 0% ← nothing yet |
// | 3 | 300 | 900 | 25% ← **cliff** |
// | 4 | 400 | 800 | 33% |
// | 5 | 500 | 700 | 41% |
// | 6 | 600 | 600 | 50% |
// | 7 | 700 | 500 | 58% |
// | 8 | 800 | 400 | 66% |
// | 9 | 900 | 300 | 75% |
// | 10 | 1000 | 200 | 83% |
// | 11 | 1100 | 100 | 91% |
// | 12 | 1200 | 0 | 100% ← fully vested |
//
// The cliff is a **step, not a ramp**: at month 3 a quarter of the term has elapsed, so `300` tokens unlock at once. After that it accrues linearly.
//
// ## Claiming
//
// The library tracks no balances, so the caller supplies what has already been claimed:
//
// | at month | already claimed | claimable |
// |---|---|---|
// | 6 | 0 | 600 |
// | 6 | 300 | 300 |
// | 6 | 600 | 0 |
// | 12 | 600 | 600 |
//
// ## Integer rounding
//
// All arithmetic is integer, no float ever reaches consensus state. Rounding is **down**, so nobody is ever paid more than they earned, and the final instalment collects the remainder.
//
// `1000` over `3` periods:
//
// | t | vested |
// |---|---|
// | 0 | 0 |
// | 1 | 333 |
// | 2 | 666 |
// | 3 | 1000 |
//
// `333 + 333 + 334`, and the end is exactly `1000`, never `999`.
//
// ## Bug 1: a rate truncated to zero
//
// When the total is smaller than the duration, computing a per-tick rate first truncates it to zero and **nothing ever vests**. Multiplying before dividing keeps it honest. `7` tokens over `1000` ticks:
//
// | t | vested |
// |---|---|
// | 100 | 0 |
// | 150 | 1 |
// | 500 | 3 |
// | 1000 | 7 |
//
// ## Bug 2: a product that leaves int64
//
// Multiplying first is only safe if the product fits. Over a term measured in **seconds**, `total * elapsed` passes 2^63 for any grant above about `146,036` whole coins, and a wrapped int64 is still a valid int64: `v0` of this library returned a **negative** vested amount and nothing signalled it. `v1` routes the product through a 128-bit intermediate.
//
// A real two-year schedule, `1789225200` to `1852383600`, at the halfway mark:
//
// | grant | vested at halfway | expected |
// |---|---|---|
// | 100000000000 | 50000000000 | 50000000000 |
// | 146037000000 | 73018500000 | 73018500000 |
// | 318720000000000 | 159360000000000 | 159360000000000 |
}