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.

hearthguild.gno

9.12 Kb · 262 lines
  1// Package hearthguild is a companion consumer realm that imports both
  2// hearthcore/v0 (pure formatting, tier math, SVG badges, RFC-8259 JSON) and
  3// hearthbounty/v1 (live escrow stats, builder profiles, and Unicode-safe
  4// tag neutralization) to provide composable on-chain builder passports and
  5// peer skill endorsements for DAOs and grant programs.
  6package hearthguild
  7
  8import (
  9	"chain"
 10	"chain/runtime"
 11	"strconv"
 12	"strings"
 13
 14	"gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
 15	"gno.land/p/nt/avl/v0"
 16	"gno.land/p/nt/markdown/sanitize/v0"
 17	"gno.land/p/nt/mux/v0"
 18	"gno.land/p/nt/seqid/v0"
 19	"gno.land/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v1"
 20)
 21
 22type endorsementRecord struct {
 23	ID       uint64
 24	Builder  address
 25	Endorser address
 26	SkillTag string
 27	Note     string
 28	Height   int64
 29}
 30
 31// EndorsementView is an immutable value copy of a guild endorsement.
 32type EndorsementView struct {
 33	ID       uint64
 34	Builder  address
 35	Endorser address
 36	SkillTag string
 37	Note     string
 38	Height   int64
 39}
 40
 41var (
 42	guildPkgPath  string
 43	guildWebBase  string
 44	endorseSeq    seqid.ID
 45	endorsements  *avl.Tree // seqid -> *endorsementRecord
 46	endorsedByKey *avl.Tree // "endorser/builder" -> true
 47	builderCounts *avl.Tree // builder address string -> uint64
 48	router        *mux.Router
 49)
 50
 51func init(cur realm) {
 52	guildPkgPath = cur.PkgPath()
 53	idx := strings.Index(guildPkgPath, "/")
 54	if idx == -1 {
 55		guildWebBase = "/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthguild/v0"
 56	} else {
 57		guildWebBase = guildPkgPath[idx:]
 58	}
 59	endorsements = avl.NewTree()
 60	endorsedByKey = avl.NewTree()
 61	builderCounts = avl.NewTree()
 62	initRouter()
 63}
 64
 65func initRouter() {
 66	router = mux.NewRouter()
 67	router.HandleFunc("", renderHome)
 68	router.HandleFunc("passport/{addr}", renderPassport)
 69	router.NotFoundHandler = renderNotFound
 70}
 71
 72// EndorseBuilder records a peer skill endorsement backed by the builder's live
 73// hearthbounty/v1 profile. Self-endorsement and duplicate endorser->builder
 74// attestations are rejected.
 75func EndorseBuilder(cur realm, builder address, skillTag, note string) uint64 {
 76	caller := cur.Previous().Address()
 77	if !builder.IsValid() {
 78		panic("hearthguild: invalid builder address")
 79	}
 80	if caller == builder {
 81		panic("hearthguild: cannot endorse yourself")
 82	}
 83	if _, ok := hearthbounty.GetProfile(builder); !ok {
 84		panic("hearthguild: builder has no hearthbounty/v1 profile")
 85	}
 86
 87	pairKey := string(caller) + "/" + string(builder)
 88	if endorsedByKey.Has(pairKey) {
 89		panic("hearthguild: caller already endorsed this builder")
 90	}
 91
 92	cleanTag := hearthcore.ValidateText("skillTag", skillTag, 2, 32)
 93	cleanNote := hearthcore.ValidateText("note", note, 6, 240)
 94	nextID := uint64(endorseSeq.Next())
 95	height := runtime.ChainHeight()
 96
 97	rec := &endorsementRecord{
 98		ID:       nextID,
 99		Builder:  builder,
100		Endorser: caller,
101		SkillTag: cleanTag,
102		Note:     cleanNote,
103		Height:   height,
104	}
105	endorsements.Set(seqid.ID(nextID).String(), rec)
106	endorsedByKey.Set(pairKey, true)
107
108	bKey := string(builder)
109	prev := uint64(0)
110	if raw := builderCounts.Get(bKey); raw != nil {
111		prev = raw.(uint64)
112	}
113	builderCounts.Set(bKey, prev+1)
114
115	chain.Emit(
116		"BuilderEndorsed",
117		"id", strconv.FormatUint(nextID, 10),
118		"builder", string(builder),
119		"endorser", string(caller),
120		"skill_tag", cleanTag,
121	)
122
123	return nextID
124}
125
126// CheckEligibility evaluates whether a builder or AI agent meets DAO/grant
127// thresholds directly from live hearthbounty/v1 on-chain track record.
128func CheckEligibility(builder address, minSettled uint64, minEarnedUgnot int64) bool {
129	p, ok := hearthbounty.GetProfile(builder)
130	if !ok {
131		return false
132	}
133	return p.SettledCount >= minSettled && p.EarnedUgnot >= minEarnedUgnot
134}
135
136// EndorsementCount returns how many unique peer endorsements builder has received.
137func EndorsementCount(builder address) uint64 {
138	if raw := builderCounts.Get(string(builder)); raw != nil {
139		return raw.(uint64)
140	}
141	return 0
142}
143
144// ExportPassportJSON emits a deterministic JSON credential combining live
145// hearthbounty/v1 reputation data with hearthguild/v0 peer endorsements.
146func ExportPassportJSON(builderStr string) string {
147	addr := address(strings.TrimSpace(builderStr))
148	p, ok := hearthbounty.GetProfile(addr)
149	if !ok {
150		return `{"found":false}`
151	}
152	stats := hearthbounty.GetStats()
153	eCount := EndorsementCount(addr)
154
155	return hearthcore.NewJSONObject().
156		Bool("found", true).
157		Str("consumer_realm", guildPkgPath).
158		Str("upstream_realm", hearthbounty.PkgPath()).
159		Str("address", string(p.Address)).
160		Str("handle", p.Handle).
161		Bool("is_agent", p.IsAgent).
162		Str("tier", string(p.Tier)).
163		Uint64("settled_count", p.SettledCount).
164		Int64("earned_ugnot", p.EarnedUgnot).
165		Str("earned_formatted", hearthcore.FormatGNOT(p.EarnedUgnot)).
166		Str("rating_formatted", hearthcore.FormatRating(p.RatingSum, p.RatingCount)).
167		Uint64("endorsement_count", eCount).
168		Int64("upstream_total_bounties", int64(stats.TotalBounties)).
169		Int64("upstream_total_escrow_ugnot", stats.TotalEscrowUgnot).
170		String()
171}
172
173// Render serves the native gnoweb Guild Passport interface.
174func Render(path string) string {
175	return router.Render(path)
176}
177
178func safeText(s string) string {
179	return hearthbounty.NeutralizeGnoTags(hearthcore.EscapeMath(sanitize.InlineText(s)))
180}
181
182func safeCell(s string) string {
183	return hearthbounty.NeutralizeGnoTags(hearthcore.EscapeMath(sanitize.TableCell(s)))
184}
185
186func renderHome(res *mux.ResponseWriter, req *mux.Request) {
187	var b strings.Builder
188	stats := hearthbounty.GetStats()
189
190	b.WriteString("# <gno-icon name=\"shield\" /> HearthGuild Passport\n\n")
191	b.WriteString("Composable consumer realm reading live reputation and escrow metrics from `")
192	b.WriteString(safeText(hearthbounty.PkgPath()))
193	b.WriteString("` and rendering deterministic badges via `hearthcore/v0`.\n\n")
194
195	b.WriteString("<gno-columns>\n")
196	b.WriteString("<gno-frame>\n")
197	b.WriteString("### Upstream Bounty Board\n\n")
198	b.WriteString("- **Total Bounties:** **" + strconv.Itoa(stats.TotalBounties) + "**\n")
199	b.WriteString("- **Active Escrow:** **" + hearthcore.FormatGNOT(stats.TotalEscrowUgnot) + "**\n")
200	b.WriteString("- **Settled Volume:** **" + hearthcore.FormatGNOT(stats.TotalSettledUgnot) + "**\n")
201	b.WriteString("</gno-frame>\n")
202	b.WriteString("<gno-columns-sep />\n")
203	b.WriteString("<gno-frame>\n")
204	b.WriteString("### Guild Credential Registry\n\n")
205	b.WriteString("- **Upstream Profiles:** **" + strconv.Itoa(stats.ProfileCount) + "** (" + strconv.Itoa(stats.AgentCount) + " AI agents)\n")
206	b.WriteString("- **Peer Endorsements:** **" + strconv.Itoa(endorsements.Size()) + "**\n")
207	b.WriteString("- **Gate Helper:** `CheckEligibility(addr, minSettled, minEarnedUgnot)`\n")
208	b.WriteString("</gno-frame>\n")
209	b.WriteString("</gno-columns>\n\n")
210
211	b.WriteString("## Recent Peer Endorsements\n\n")
212	if endorsements.Size() == 0 {
213		b.WriteString("> [!INFO] No Endorsements Yet\n")
214		b.WriteString("> Endorse any registered `hearthbounty/v1` builder or AI agent below.\n\n")
215	} else {
216		b.WriteString("| ID | Builder | Skill | Endorser | Note |\n")
217		b.WriteString("|---:|---|---|---|---|\n")
218		shown := 0
219		endorsements.ReverseIterate("", "", func(_ string, val any) bool {
220			rec := val.(*endorsementRecord)
221			bLink := "[" + safeCell(hearthcore.ShortAddr(rec.Builder)) + "](" + guildWebBase + ":passport/" + string(rec.Builder) + ")"
222			b.WriteString("| `" + strconv.FormatUint(rec.ID, 10) + "` | " + bLink + " | **" + safeCell(rec.SkillTag) + "** | `" +
223				safeCell(hearthcore.ShortAddr(rec.Endorser)) + "` | " + safeCell(rec.Note) + " |\n")
224			shown++
225			return shown >= 25
226		})
227		b.WriteString("\n")
228	}
229
230	b.WriteString("## Endorse a Registered Builder or AI Agent\n\n")
231	b.WriteString("<gno-form exec=\"EndorseBuilder\">\n")
232	b.WriteString("<gno-input name=\"builder\" type=\"text\" placeholder=\"Builder or AI agent g1... address\" required=\"true\" />\n")
233	b.WriteString("<gno-input name=\"skillTag\" type=\"text\" placeholder=\"Skill tag (e.g. audit, avl, mcp)\" required=\"true\" />\n")
234	b.WriteString("<gno-textarea name=\"note\" placeholder=\"Verification note citing settled bounty work\" rows=\"3\" required=\"true\" />\n")
235	b.WriteString("</gno-form>\n")
236
237	res.Write(b.String())
238}
239
240func renderPassport(res *mux.ResponseWriter, req *mux.Request) {
241	addrStr := req.GetVar("addr")
242	addr := address(addrStr)
243	p, ok := hearthbounty.GetProfile(addr)
244	if !ok {
245		renderNotFound(res, req)
246		return
247	}
248
249	var b strings.Builder
250	b.WriteString("# <gno-icon name=\"shield\" /> Guild Passport: " + safeText(p.Handle) + "\n\n")
251	b.WriteString(`<gno-button href="` + guildWebBase + `" label="Back to HearthGuild" variant="outline" />` + "\n\n")
252	b.WriteString(hearthcore.RenderTierBadgeSVG(p.Handle, p.Tier, p.SettledCount, p.EarnedUgnot, p.IsAgent))
253	b.WriteString("\n\n")
254	b.WriteString("- **Address:** `" + safeText(string(p.Address)) + "`\n")
255	b.WriteString("- **Upstream Tier:** **" + string(p.Tier) + "** (" + strconv.FormatUint(p.SettledCount, 10) + " settled, " + hearthcore.FormatGNOT(p.EarnedUgnot) + " earned)\n")
256	b.WriteString("- **Peer Endorsements:** **" + strconv.FormatUint(EndorsementCount(addr), 10) + "**\n")
257	res.Write(b.String())
258}
259
260func renderNotFound(res *mux.ResponseWriter, req *mux.Request) {
261	res.Write("# Passport Not Found\n\nNo `hearthbounty/v1` profile matched `" + safeText(req.Path) + "`.\n")
262}