Playground
gnomod.toml
module = "gno.land/r/nym-moultest123/x/daily/kudos/v0" gno = "0.9" [addpkg] creator = "g1ml9grtwnpzqn3hre5ve5vjdlmv2srrmpmc0dsv" height = 186407 max_deposit = "5000000ugnot"
kudos.gno
package kudos
import (
"chain"
"chain/runtime/unsafe"
"sort"
"strconv"
"strings"
"gno.land/p/nt/avl/v0"
)
// counts maps an address (as string) -> number of kudos received.
var counts avl.Tree
// feed holds recent kudos, newest first, capped at feedCap entries.
var feed []kudo
const feedCap = 50
type kudo struct {
from address
to address
reason string
}
// Give records a kudos from the caller to `to` with a reason.
// A caller cannot give kudos to themselves.
func Give(cur realm, to address, reason string) {
from := unsafe.PreviousRealm().Address()
if !to.IsValid() {
panic("invalid recipient address")
}
if from == to {
panic("cannot give kudos to yourself")
}
reason = strings.TrimSpace(reason)
if reason == "" {
panic("reason must not be empty")
}
key := to.String()
n := int64(0)
if v := counts.Get(key); v != nil {
n = v.(int64)
}
counts.Set(key, n+1)
// prepend newest, cap the feed
feed = append([]kudo{{from: from, to: to, reason: reason}}, feed...)
if len(feed) > feedCap {
feed = feed[:feedCap]
}
chain.Emit("KudosGiven", "from", from.String(), "to", to.String(), "reason", reason)
}
// entry is a leaderboard row used for deterministic sorting.
type entry struct {
addr string
count int64
}
// byScore sorts by count desc, then address asc for stable ties.
type byScore []entry
func (s byScore) Len() int { return len(s) }
func (s byScore) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func (s byScore) Less(i, j int) bool {
if s[i].count != s[j].count {
return s[i].count > s[j].count
}
return s[i].addr < s[j].addr
}
func leaderboard() []entry {
out := make([]entry, 0, counts.Size())
counts.Iterate("", "", func(key string, value any) bool {
out = append(out, entry{addr: key, count: value.(int64)})
return false
})
sort.Sort(byScore(out))
return out
}
// Render shows a leaderboard of most-received addresses and a recent feed.
func Render(path string) string {
var b strings.Builder
b.WriteString("# Kudos\n\n")
b.WriteString("Give props to addresses. `Give(to, reason)` records a kudos.\n\n")
b.WriteString("## Leaderboard\n\n")
lb := leaderboard()
if len(lb) == 0 {
b.WriteString("_No kudos yet._\n\n")
} else {
b.WriteString("| # | Address | Kudos |\n")
b.WriteString("| --- | --- | --- |\n")
for i, e := range lb {
b.WriteString("| " + strconv.Itoa(i+1) + " | " + e.addr + " | " + strconv.FormatInt(e.count, 10) + " |\n")
}
b.WriteString("\n")
}
b.WriteString("## Recent kudos\n\n")
if len(feed) == 0 {
b.WriteString("_Nothing here yet._\n")
} else {
for _, k := range feed {
b.WriteString("- " + k.from.String() + " → " + k.to.String() + ": " + k.reason + "\n")
}
}
return b.String()
}
kudos_test.gno
package kudos
import (
"strings"
"testing"
"gno.land/p/nt/avl/v0"
"gno.land/p/nt/testutils/v0"
"gno.land/p/nt/uassert/v0"
)
func TestGiveAndLeaderboard(cur realm, t *testing.T) {
alice := testutils.TestAddress("alice")
bob := testutils.TestAddress("bob")
carol := testutils.TestAddress("carol")
// reset package state between test funcs
counts = avl.Tree{}
feed = nil
testing.SetRealm(testing.NewUserRealm(alice))
Give(cross(cur), bob, "great review")
testing.SetRealm(testing.NewUserRealm(carol))
Give(cross(cur), bob, "helpful pairing")
Give(cross(cur), alice, "nice docs")
lb := leaderboard()
uassert.Equal(t, 2, len(lb))
// bob has 2, alice has 1 -> bob first
uassert.Equal(t, bob.String(), lb[0].addr)
uassert.Equal(t, int64(2), lb[0].count)
uassert.Equal(t, alice.String(), lb[1].addr)
uassert.Equal(t, int64(1), lb[1].count)
out := Render("")
uassert.True(t, strings.Contains(out, "helpful pairing"))
uassert.True(t, strings.Contains(out, "Leaderboard"))
}
func TestCannotKudosSelf(cur realm, t *testing.T) {
dave := testutils.TestAddress("dave")
counts = avl.Tree{}
feed = nil
testing.SetRealm(testing.NewUserRealm(dave))
uassert.AbortsWithMessage(t, cur, "cannot give kudos to yourself", func() {
Give(cross(cur), dave, "self five")
})
}
func TestEmptyReasonAborts(cur realm, t *testing.T) {
eve := testutils.TestAddress("eve")
frank := testutils.TestAddress("frank")
counts = avl.Tree{}
feed = nil
testing.SetRealm(testing.NewUserRealm(eve))
uassert.AbortsWithMessage(t, cur, "reason must not be empty", func() {
Give(cross(cur), frank, " ")
})
}
render_example_test.gno
package kudos
// ExampleRender pins the realm's Render output as a testable example.
func ExampleRender() {
print(Render(""))
// Output:
// # Kudos
//
// Give props to addresses. `Give(to, reason)` records a kudos.
//
// ## Leaderboard
//
// _No kudos yet._
//
// ## Recent kudos
//
// _Nothing here yet._
}