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

builders.gno

// Package builders is a public directory of people with proven, paid work.
//
// v2 reads the work record from gild/v1, which counts paid work on both Gild
// boards, so people who only worked on the newer board can join too.
//
// v1 adds full profiles: a display name, a headline, a short bio, skills,
// links and an "open to work" switch. Each builder edits their own profile
// and nobody else can. Work stats are never stored here: they are read from
// the Gild realm every time, so they cannot be edited or faked.
//
// Anyone can list themselves, but only after finishing at least one paid
// task on Gild. No oracle, no admin, no off-chain list.
package builders

import (
	"chain"
	"strconv"
	"strings"
	"time"

	"gno.land/p/nt/avl/v0"
	"gno.land/r/g1jw2yrfqnd4fp90qadw88pnjgpden4vgagjvpe6/gild/v1"
)

// AppURL is the Gild web app, linked from every page.
const AppURL = "https://gild.sithunyein.com/app"

const (
	// MinTasks is how many paid Gild tasks you need before you can join.
	MinTasks    = 1
	MaxName     = 40
	MaxHeadline = 160
	MaxBio      = 600
	MaxSkills   = 6
	MaxSkill    = 24
	MaxLink     = 200
	MaxPage     = 50
)

const (
	ErrNoProof  = "builders: finish a paid task on Gild first"
	ErrName     = "builders: name must be 1 to 40 characters"
	ErrHeadline = "builders: headline must be 1 to 160 characters"
	ErrBio      = "builders: bio must be at most 600 characters"
	ErrSkills   = "builders: use up to 6 skills of letters, numbers, spaces, + # . or -"
	ErrWebsite  = "builders: website must start with https:// and have no spaces"
	ErrGitHub   = "builders: GitHub must be a username, like octocat"
	ErrX        = "builders: X must be a handle, like gnoland"
	ErrNotIn    = "builders: you are not in the directory"
)

// Entry is one person in the directory. It holds only what the builder
// wrote about themselves; their Gild stats are read live.
type Entry struct {
	Addr      address
	Name      string
	Headline  string
	Bio       string
	Skills    []string
	Website   string
	GitHub    string
	X         string
	Open      bool
	JoinedAt  time.Time
	UpdatedAt time.Time
}

var (
	entries avl.Tree // address string -> *Entry
	webPath string
)

func init(cur realm) {
	webPath = strings.TrimPrefix(cur.PkgPath(), "gno.land")
}

// SetProfile creates or updates the caller's profile. Skills is a
// comma-separated list. Empty links are allowed. It fails unless the caller
// has finished at least MinTasks paid Gild tasks.
func SetProfile(cur realm, name, headline, bio, skills, website, github, x string, open bool) {
	caller := cur.Previous().Address()
	if !gild.HasCompleted(caller, MinTasks) {
		panic(ErrNoProof)
	}
	name = strings.TrimSpace(name)
	headline = strings.TrimSpace(headline)
	bio = strings.TrimSpace(strings.ReplaceAll(bio, "\r\n", "\n"))
	website = strings.TrimSpace(website)
	github = strings.TrimPrefix(strings.TrimSpace(github), "@")
	x = strings.TrimPrefix(strings.TrimSpace(x), "@")

	if len(name) == 0 || len(name) > MaxName || hasControl(name) {
		panic(ErrName)
	}
	if len(headline) == 0 || len(headline) > MaxHeadline || hasControl(headline) {
		panic(ErrHeadline)
	}
	if len(bio) > MaxBio || hasControl(strings.ReplaceAll(bio, "\n", " ")) {
		panic(ErrBio)
	}
	tags, ok := parseSkills(skills)
	if !ok {
		panic(ErrSkills)
	}
	if website != "" && !isLink(website) {
		panic(ErrWebsite)
	}
	if github != "" && !isGitHub(github) {
		panic(ErrGitHub)
	}
	if x != "" && !isX(x) {
		panic(ErrX)
	}

	now := time.Now()
	key := caller.String()
	e := &Entry{Addr: caller, JoinedAt: now}
	if v := entries.Get(key); v != nil {
		e = v.(*Entry)
	}
	e.Name, e.Headline, e.Bio, e.Skills = name, headline, bio, tags
	e.Website, e.GitHub, e.X, e.Open = website, github, x, open
	e.UpdatedAt = now
	entries.Set(key, e)
	chain.Emit("ProfileSet", "addr", key)
}

// SetOpen flips only the "open to work" switch.
func SetOpen(cur realm, open bool) {
	key := cur.Previous().Address().String()
	v := entries.Get(key)
	if v == nil {
		panic(ErrNotIn)
	}
	e := v.(*Entry)
	e.Open = open
	e.UpdatedAt = time.Now()
	chain.Emit("ProfileSet", "addr", key)
}

// Leave removes the caller from the directory. Their Gild record stays.
func Leave(cur realm) {
	key := cur.Previous().Address().String()
	if _, removed := entries.Remove(key); !removed {
		panic(ErrNotIn)
	}
	chain.Emit("ProfileRemoved", "addr", key)
}

// IsListed reports whether addr is in the directory.
func IsListed(addr address) bool {
	return entries.Has(addr.String())
}

// Size returns how many people are listed.
func Size() int { return entries.Size() }

// ProfileJSON returns one builder with live Gild stats, or null.
func ProfileJSON(addr address) string {
	v := entries.Get(addr.String())
	if v == nil {
		return "null"
	}
	var b strings.Builder
	writeEntry(&b, v.(*Entry))
	return b.String()
}

// BuildersJSON lists builders with their live Gild stats.
func BuildersJSON(offset, limit int) string {
	if offset < 0 {
		offset = 0
	}
	if limit <= 0 || limit > MaxPage {
		limit = MaxPage
	}
	var b strings.Builder
	b.WriteString(`{"total":`)
	b.WriteString(strconv.Itoa(entries.Size()))
	b.WriteString(`,"items":[`)
	first := true
	entries.IterateByOffset(offset, limit, func(_ string, v any) bool {
		if !first {
			b.WriteByte(',')
		}
		first = false
		writeEntry(&b, v.(*Entry))
		return false
	})
	b.WriteString("]}")
	return b.String()
}

func writeEntry(b *strings.Builder, e *Entry) {
	b.WriteString(`{"address":`)
	b.WriteString(jsonStr(e.Addr.String()))
	b.WriteString(`,"name":`)
	b.WriteString(jsonStr(e.Name))
	b.WriteString(`,"headline":`)
	b.WriteString(jsonStr(e.Headline))
	b.WriteString(`,"bio":`)
	b.WriteString(jsonStr(e.Bio))
	b.WriteString(`,"skills":[`)
	for i, s := range e.Skills {
		if i > 0 {
			b.WriteByte(',')
		}
		b.WriteString(jsonStr(s))
	}
	b.WriteString(`],"website":`)
	b.WriteString(jsonStr(e.Website))
	b.WriteString(`,"github":`)
	b.WriteString(jsonStr(e.GitHub))
	b.WriteString(`,"x":`)
	b.WriteString(jsonStr(e.X))
	b.WriteString(`,"open":`)
	b.WriteString(strconv.FormatBool(e.Open))
	b.WriteString(`,"completed":`)
	b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
	b.WriteString(`,"earned":`)
	b.WriteString(strconv.FormatInt(gild.Earned(e.Addr), 10))
	b.WriteString(`,"joinedAt":`)
	b.WriteString(strconv.FormatInt(e.JoinedAt.Unix(), 10))
	b.WriteString(`,"updatedAt":`)
	b.WriteString(strconv.FormatInt(e.UpdatedAt.Unix(), 10))
	b.WriteByte('}')
}

// Render shows the directory, or one profile at u/<address>.
func Render(path string) string {
	if strings.HasPrefix(path, "u/") {
		return renderProfile(strings.TrimPrefix(path, "u/"))
	}
	var b strings.Builder
	b.WriteString("# Gild Builders\n\n")
	b.WriteString("[Directory in the app](" + AppURL + "#/builders) · [Gild board](/r/" + gildPath() + ") · [Source](" + webPath + "$source)\n\n")
	b.WriteString("People with proven, paid work on [Gild](/r/" + gildPath() + "). ")
	b.WriteString("To join, finish at least ")
	b.WriteString(strconv.Itoa(MinTasks))
	b.WriteString(" paid task, then call `SetProfile`. Stats below are read live from Gild.\n\n")
	if entries.Size() == 0 {
		b.WriteString("_Nobody has joined yet._\n")
		return b.String()
	}
	b.WriteString("| Builder | Open to work | Tasks done | Earned | Skills |\n|---|---|---|---|---|\n")
	entries.Iterate("", "", func(_ string, v any) bool {
		e := v.(*Entry)
		b.WriteString("| [" + mdText(e.Name) + "](" + webPath + ":u/" + e.Addr.String() + ") | ")
		if e.Open {
			b.WriteString("Yes")
		} else {
			b.WriteString("No")
		}
		b.WriteString(" | ")
		b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
		b.WriteString(" | ")
		b.WriteString(gnot(gild.Earned(e.Addr)))
		b.WriteString(" | ")
		b.WriteString(mdText(strings.Join(e.Skills, ", ")))
		b.WriteString(" |\n")
		return false
	})
	return b.String()
}

func renderProfile(addr string) string {
	v := entries.Get(addr)
	if v == nil {
		return "# Not listed\n\nThis address is not in the directory. [Back to Builders](" + webPath + ")\n"
	}
	e := v.(*Entry)
	var b strings.Builder
	b.WriteString("# " + mdText(e.Name) + "\n\n")
	b.WriteString("**" + mdText(e.Headline) + "**\n\n")
	if e.Open {
		b.WriteString("Open to work.\n\n")
	}
	b.WriteString("| Tasks done | Earned |\n|---|---|\n| ")
	b.WriteString(strconv.Itoa(gild.Completed(e.Addr)))
	b.WriteString(" | ")
	b.WriteString(gnot(gild.Earned(e.Addr)))
	b.WriteString(" |\n\n")
	if e.Bio != "" {
		for _, line := range strings.Split(e.Bio, "\n") {
			b.WriteString(mdText(line) + "  \n")
		}
		b.WriteString("\n")
	}
	if len(e.Skills) > 0 {
		b.WriteString("Skills: " + mdText(strings.Join(e.Skills, ", ")) + "\n\n")
	}
	if e.Website != "" {
		b.WriteString("- Website: [" + mdText(strings.TrimPrefix(e.Website, "https://")) + "](" + e.Website + ")\n")
	}
	if e.GitHub != "" {
		b.WriteString("- GitHub: [" + e.GitHub + "](https://github.com/" + e.GitHub + ")\n")
	}
	if e.X != "" {
		b.WriteString("- X: [@" + mdText(e.X) + "](https://x.com/" + e.X + ")\n")
	}
	b.WriteString("\nAddress: `" + addr + "` · [Gild record](/r/" + gildPath() + ":u/" + addr + ")\n\n")
	b.WriteString("[Hire " + mdText(e.Name) + " in the app](" + AppURL + "#/post?to=" + addr + ") · [Profile in the app](" + AppURL + "#/u/" + addr + ")\n")
	return b.String()
}

// gildPath is the Gild realm path without the gno.land/r/ prefix, derived from
// this realm's own namespace so it works wherever both are deployed.
func gildPath() string {
	p := strings.TrimPrefix(webPath, "/r/")
	if i := strings.Index(p, "/"); i >= 0 {
		return p[:i] + "/gild/v1"
	}
	return "gild/v1"
}

// parseSkills splits, trims, lowercases and de-duplicates a comma list.
func parseSkills(s string) ([]string, bool) {
	out := []string{}
	seen := map[string]bool{}
	for _, part := range strings.Split(s, ",") {
		t := strings.ToLower(strings.TrimSpace(part))
		if t == "" {
			continue
		}
		if len(t) > MaxSkill {
			return nil, false
		}
		for _, r := range t {
			ok := (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') || strings.ContainsRune(" +#.-", r)
			if !ok {
				return nil, false
			}
		}
		if seen[t] {
			continue
		}
		seen[t] = true
		out = append(out, t)
	}
	if len(out) > MaxSkills {
		return nil, false
	}
	return out, true
}

func isGitHub(s string) bool {
	if len(s) == 0 || len(s) > 39 || s[0] == '-' {
		return false
	}
	for _, r := range s {
		if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '-') {
			return false
		}
	}
	return true
}

func isX(s string) bool {
	if len(s) == 0 || len(s) > 15 {
		return false
	}
	for _, r := range s {
		if !((r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z') || (r >= '0' && r <= '9') || r == '_') {
			return false
		}
	}
	return true
}

func hasControl(s string) bool {
	for _, r := range s {
		if r < 0x20 || r == 0x7f {
			return true
		}
	}
	return false
}

func isLink(s string) bool {
	if len(s) > MaxLink || !strings.HasPrefix(s, "https://") || len(s) <= len("https://") {
		return false
	}
	for _, r := range s {
		if r <= ' ' || r == 0x7f || strings.ContainsRune("\"<>`()[]|", r) {
			return false
		}
	}
	return true
}

func gnot(u int64) string {
	whole := u / 1_000_000
	frac := u % 1_000_000
	s := strconv.FormatInt(whole, 10)
	if frac != 0 {
		f := strconv.FormatInt(frac+1_000_000, 10)[1:]
		s += "." + strings.TrimRight(f, "0")
	}
	return s + " GNOT"
}

// mdText escapes characters that would change markdown or table layout.
func mdText(s string) string {
	var b strings.Builder
	for _, r := range s {
		if strings.ContainsRune("\\`*_[]()<>#|!~", r) {
			b.WriteByte('\\')
		}
		b.WriteRune(r)
	}
	return b.String()
}

func jsonStr(s string) string {
	var b strings.Builder
	b.WriteByte('"')
	for _, r := range s {
		switch {
		case r == '"':
			b.WriteString(`\"`)
		case r == '\\':
			b.WriteString(`\\`)
		case r < 0x20 || r == '<' || r == '>' || r == '&' || r == 0x2028 || r == 0x2029:
			h := strconv.FormatInt(int64(r), 16)
			b.WriteString(`\u` + strings.Repeat("0", 4-len(h)) + h)
		default:
			b.WriteRune(r)
		}
	}
	b.WriteByte('"')
	return b.String()
}

gnomod.toml

module = "gno.land/r/g1jw2yrfqnd4fp90qadw88pnjgpden4vgagjvpe6/builders/v2"
gno = "0.9"

[addpkg]
  creator = "g1jw2yrfqnd4fp90qadw88pnjgpden4vgagjvpe6"
  height = 1504739
  max_deposit = "100000000ugnot"