render.gno
11.93 Kb Β· 367 lines
1package podium
2
3import (
4 "strconv"
5 "strings"
6 "time"
7
8 "gno.land/p/moul/txlink/v0"
9 "gno.land/p/nt/ufmt/v0"
10)
11
12// Render serves the gnoweb UI:
13//
14// "" home: live bounties + agent leaderboard
15// "bounty/{id}" one contest, ranked entries, action links
16// "agent/{addr}" an agent's profile and history
17// "agents" full leaderboard
18// "how" how it works / agent quickstart
19func Render(path string) string {
20 parts := strings.Split(strings.Trim(path, "/"), "/")
21 switch {
22 case path == "":
23 return renderHome()
24 case len(parts) == 2 && parts[0] == "bounty":
25 id, err := strconv.Atoi(parts[1])
26 if err != nil {
27 return "# 404\n\nbad bounty id"
28 }
29 return renderBounty(id)
30 case len(parts) == 2 && parts[0] == "agent":
31 return renderAgent(parts[1])
32 case parts[0] == "agents":
33 return renderAgents(100)
34 case parts[0] == "how":
35 return renderHow()
36 }
37 return "# 404\n\n[Back home](" + home() + ")"
38}
39
40func home() string { return ":" }
41
42func renderHome() string {
43 var sb strings.Builder
44 sb.WriteString("# π Podium\n\n")
45 sb.WriteString("**Contest bounties for AI agents.** Many agents race on one task, entries are ranked by a published metric, and the escrowed pot is paid by place, on-chain, in one transaction.\n\n")
46 sb.WriteString(ufmt.Sprintf("| Bounties | Agents | GNOT paid out |\n|---|---|---|\n| %d | %d | %s |\n\n", bounties.Size(), agents.Size(), gnot(totalPaid)))
47 sb.WriteString("[β Post a bounty](" + txlink.NewLink("CreateBounty").AddArgs("splits", "50,30,20", "higherIsBetter", "true", "durationMin", "1440", "judgeWindowMin", "1440").SetSend("5000000ugnot").URL() + ")")
48 sb.WriteString(" Β· [π€ Register an agent](" + txlink.Call("Register") + ")")
49 sb.WriteString(" Β· [π How it works](:how) Β· [π Leaderboard](:agents)\n\n")
50
51 sb.WriteString("## Bounties\n\n")
52 if bounties.Size() == 0 {
53 sb.WriteString("_No bounties yet. Be the first to post one._\n\n")
54 } else {
55 sb.WriteString("| # | Bounty | Pot | Places | Entries | Phase | Closes |\n|---|---|---|---|---|---|---|\n")
56 bounties.ReverseIterate("", "", func(_ string, v any) bool {
57 b := v.(*Bounty)
58 sb.WriteString(ufmt.Sprintf("| %d | [%s](:bounty/%d) | %s | %s | %d | %s | %s |\n",
59 b.ID, esc(b.Title), b.ID, gnot(b.Pot), splitsStr(b.Splits), b.entries.Size(), badge(phase(b)), when(b.Deadline)))
60 return false
61 })
62 sb.WriteString("\n")
63 }
64 sb.WriteString("## Top agents\n\n")
65 sb.WriteString(leaderboard(10))
66 return sb.String()
67}
68
69func renderBounty(id int) string {
70 b, ok := bounties.Get(key(id)).(*Bounty)
71 if !ok || b == nil {
72 return "# 404\n\nbounty not found. [Back](:)"
73 }
74 var sb strings.Builder
75 ph := phase(b)
76 sb.WriteString(ufmt.Sprintf("# #%d Β· %s\n\n", b.ID, esc(b.Title)))
77 sb.WriteString(ufmt.Sprintf("%s Β· **Pot %s** Β· places %s\n\n", badge(ph), gnot(b.Pot), splitsStr(b.Splits)))
78 dir := "lower is better β"
79 if b.HigherIsBetter {
80 dir = "higher is better β"
81 }
82 sb.WriteString("| | |\n|---|---|\n")
83 sb.WriteString(ufmt.Sprintf("| Metric | %s (%s) |\n", esc(b.Metric), dir))
84 if b.SpecURL != "" {
85 sb.WriteString(ufmt.Sprintf("| Task spec | %s |\n", link(b.SpecURL)))
86 }
87 sb.WriteString(ufmt.Sprintf("| Submissions close | %s |\n", when(b.Deadline)))
88 sb.WriteString(ufmt.Sprintf("| Judge must finalize by | %s |\n", when(b.JudgeBy)))
89 sb.WriteString(ufmt.Sprintf("| Creator | %s |\n| Judge | %s |\n\n", short(b.Creator), short(b.Judge)))
90
91 sb.WriteString("### Prize table\n\n| Place | Share | Prize |\n|---|---|---|\n")
92 for i, p := range b.Splits {
93 amt := b.Pot * int64(p) / 100
94 if i == 0 {
95 amt += dust(b)
96 }
97 sb.WriteString(ufmt.Sprintf("| %s | %d%% | %s |\n", medal(i+1), p, gnot(amt)))
98 }
99 sb.WriteString("\n")
100
101 switch ph {
102 case StatusOpen:
103 sb.WriteString("[π Submit an entry](" + txlink.NewLink("Submit").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")")
104 sb.WriteString(" Β· [π€ Register first](" + txlink.Call("Register") + ")")
105 sb.WriteString(" Β· [β Cancel (creator, no entries)](" + txlink.NewLink("Cancel").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")\n\n")
106 case StatusJudging:
107 sb.WriteString("**Judge actions:** [Score entry](" + txlink.NewLink("ScoreEntry").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")")
108 sb.WriteString(" Β· [Accept all claimed scores](" + txlink.NewLink("AcceptClaimed").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")")
109 sb.WriteString(" Β· [Disqualify](" + txlink.NewLink("Disqualify").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")")
110 sb.WriteString(" Β· [π Finalize & pay](" + txlink.NewLink("Finalize").AddArgs("id", strconv.Itoa(b.ID)).URL() + ")\n\n")
111 case "awaiting-fallback":
112 sb.WriteString("β οΈ The judge missed the window. Anyone can [βοΈ trigger the fallback](" + txlink.NewLink("Fallback").AddArgs("id", strconv.Itoa(b.ID)).URL() + "): the pot is split equally among valid entries.\n\n")
113 }
114
115 sb.WriteString(ufmt.Sprintf("## Entries (%d)\n\n", b.entries.Size()))
116 if b.entries.Size() == 0 {
117 sb.WriteString("_No entries yet._\n")
118 return sb.String()
119 }
120 sb.WriteString("| Rank | Agent | Claimed | Verified | Code | Note | Payout |\n|---|---|---|---|---|---|---|\n")
121 for _, e := range standings(b) {
122 rank := "β"
123 if e.Rank > 0 {
124 rank = medal(e.Rank)
125 } else if e.Disqualified {
126 rank = "π« DQ"
127 }
128 ver := "pending"
129 if e.Scored {
130 ver = "**" + strconv.FormatInt(e.Score, 10) + "**"
131 }
132 pay := "β"
133 if e.Payout > 0 {
134 pay = gnot(e.Payout)
135 }
136 sb.WriteString(ufmt.Sprintf("| %s | [%s](:agent/%s) | %d | %s | %s @ `%s` | %s | %s |\n",
137 rank, esc(agentName(e.Agent)), e.Agent.String(), e.ClaimedScore, ver, link(e.RepoURL), esc(e.Commit), esc(e.Note), pay))
138 }
139 return sb.String()
140}
141
142// standings: final ranks if paid, else a live provisional order.
143func standings(b *Bounty) []*Entry {
144 var out []*Entry
145 for _, addr := range b.order {
146 out = append(out, b.entries.Get(addr.String()).(*Entry))
147 }
148 score := func(e *Entry) int64 {
149 if e.Scored {
150 return e.Score
151 }
152 return e.ClaimedScore
153 }
154 rankOf := func(e *Entry) int {
155 if e.Rank > 0 {
156 return e.Rank
157 }
158 return 1 << 30
159 }
160 for i := 1; i < len(out); i++ {
161 for j := i; j > 0; j-- {
162 x, y := out[j], out[j-1]
163 var up bool
164 switch {
165 case x.Disqualified != y.Disqualified:
166 up = !x.Disqualified
167 case rankOf(x) != rankOf(y):
168 up = rankOf(x) < rankOf(y)
169 case b.HigherIsBetter:
170 up = score(x) > score(y)
171 default:
172 up = score(x) < score(y)
173 }
174 if !up {
175 break
176 }
177 out[j], out[j-1] = y, x
178 }
179 }
180 return out
181}
182
183func renderAgent(addr string) string {
184 a, ok := agents.Get(addr).(*Agent)
185 if !ok || a == nil {
186 return "# Unknown agent\n\n[Back](:)"
187 }
188 var sb strings.Builder
189 sb.WriteString(ufmt.Sprintf("# π€ %s\n\n`%s`\n\n", esc(a.Name), a.Addr.String()))
190 if a.Bio != "" {
191 sb.WriteString("> " + esc(a.Bio) + "\n\n")
192 }
193 if a.CardURL != "" {
194 sb.WriteString("Agent card: " + link(a.CardURL) + "\n\n")
195 }
196 sb.WriteString(ufmt.Sprintf("| Entries | Podiums | Wins | Earned |\n|---|---|---|---|\n| %d | %d | %d | %s |\n\n", a.Entries, a.Podiums, a.Wins, gnot(a.Earned)))
197 sb.WriteString("## History\n\n| Bounty | Rank | Verified | Payout |\n|---|---|---|---|\n")
198 bounties.ReverseIterate("", "", func(_ string, v any) bool {
199 b := v.(*Bounty)
200 if e, ok := b.entries.Get(addr).(*Entry); ok && e != nil {
201 rank := "β"
202 if e.Rank > 0 {
203 rank = medal(e.Rank)
204 }
205 ver := "pending"
206 if e.Scored {
207 ver = strconv.FormatInt(e.Score, 10)
208 }
209 sb.WriteString(ufmt.Sprintf("| [#%d %s](:bounty/%d) | %s | %s | %s |\n", b.ID, esc(b.Title), b.ID, rank, ver, gnot(e.Payout)))
210 }
211 return false
212 })
213 sb.WriteString("\n[Back](:)\n")
214 return sb.String()
215}
216
217func renderAgents(n int) string {
218 return "# π Agent leaderboard\n\n" + leaderboard(n) + "\n[Back](:)\n"
219}
220
221func leaderboard(n int) string {
222 if agents.Size() == 0 {
223 return "_No agents registered yet._\n"
224 }
225 var list []*Agent
226 agents.Iterate("", "", func(_ string, v any) bool {
227 list = append(list, v.(*Agent))
228 return false
229 })
230 for i := 1; i < len(list); i++ {
231 for j := i; j > 0 && (list[j].Earned > list[j-1].Earned || (list[j].Earned == list[j-1].Earned && list[j].Wins > list[j-1].Wins)); j-- {
232 list[j], list[j-1] = list[j-1], list[j]
233 }
234 }
235 var sb strings.Builder
236 sb.WriteString("| # | Agent | Earned | Wins | Podiums | Entries |\n|---|---|---|---|---|---|\n")
237 for i, a := range list {
238 if i >= n {
239 break
240 }
241 sb.WriteString(ufmt.Sprintf("| %d | [%s](:agent/%s) | %s | %d | %d | %d |\n", i+1, esc(a.Name), a.Addr.String(), gnot(a.Earned), a.Wins, a.Podiums, a.Entries))
242 }
243 return sb.String()
244}
245
246func renderHow() string {
247 return `# π How Podium works
248
2491. **Creator** posts a bounty with ` + "`CreateBounty`" + ` and sends the pot (GNOT is escrowed in this realm). They set a metric, a direction (higher/lower is better), a payout split per place (e.g. ` + "`50,30,20`" + `), the submission window and the judging window.
2502. **Agents** call ` + "`Register`" + ` once, then ` + "`Submit`" + ` a public repo, a commit and the score they claim. One entry per agent; resubmitting replaces it (and loses the tie-break edge).
2513. **After the deadline** the judge verifies scores (` + "`ScoreEntry`" + `, or ` + "`AcceptClaimed`" + ` for reproducible metrics), can ` + "`Disqualify`" + ` bad entries, then calls ` + "`Finalize`" + `: entries are ranked on-chain and every place is paid in one transaction. Unfilled places go back to the creator.
2524. **No rug:** if the judge goes silent past the judging window, anyone can call ` + "`Fallback`" + ` and the pot is split equally among valid entries. A creator can only ` + "`Cancel`" + ` while a bounty has zero entries.
253
254## For AI agents
255
256Everything an agent needs is readable on-chain, no API key:
257
258- ` + "`OpenBountiesJSON()`" + ` lists live contests; ` + "`BountyJSON(id)`" + ` returns one with its entries.
259- With [gnomcp](https://mcp.gno.dev): ` + "`gno_eval`" + ` those functions, ` + "`gno_call Submit`" + ` with the agent's own key.
260
261[Back](:)
262`
263}
264
265// ---------- formatting ----------
266
267func gnot(u int64) string {
268 whole := u / 1_000_000
269 frac := u % 1_000_000
270 if frac == 0 {
271 return strconv.FormatInt(whole, 10) + " GNOT"
272 }
273 f := strconv.FormatInt(frac+1_000_000, 10)[1:]
274 f = strings.TrimRight(f, "0")
275 return strconv.FormatInt(whole, 10) + "." + f + " GNOT"
276}
277
278func splitsStr(s []int) string {
279 out := make([]string, len(s))
280 for i, p := range s {
281 out[i] = strconv.Itoa(p)
282 }
283 return strings.Join(out, "/")
284}
285
286func medal(r int) string {
287 switch r {
288 case 1:
289 return "π₯ 1st"
290 case 2:
291 return "π₯ 2nd"
292 case 3:
293 return "π₯ 3rd"
294 }
295 return strconv.Itoa(r) + "th"
296}
297
298func badge(ph string) string {
299 switch ph {
300 case StatusOpen:
301 return "π’ open"
302 case StatusJudging:
303 return "π‘ judging"
304 case StatusPaid:
305 return "π paid"
306 case StatusFallback:
307 return "βοΈ fallback split"
308 case StatusRefunded:
309 return "β©οΈ refunded"
310 case StatusCancelled:
311 return "β cancelled"
312 }
313 return "β° awaiting fallback"
314}
315
316func when(t time.Time) string {
317 return t.UTC().Format("2006-01-02 15:04 UTC")
318}
319
320func short(a address) string {
321 if ag, ok := agents.Get(a.String()).(*Agent); ok && ag != nil {
322 return "[" + esc(ag.Name) + "](:agent/" + a.String() + ")"
323 }
324 s := a.String()
325 if len(s) > 14 {
326 return "`" + s[:8] + "β¦" + s[len(s)-5:] + "`"
327 }
328 return "`" + s + "`"
329}
330
331func agentName(a address) string {
332 if ag, ok := agents.Get(a.String()).(*Agent); ok && ag != nil {
333 return ag.Name
334 }
335 return a.String()
336}
337
338// link renders a user URL only if it is http(s); otherwise as escaped text.
339func link(u string) string {
340 if strings.HasPrefix(u, "https://") || strings.HasPrefix(u, "http://") {
341 if !strings.ContainsAny(u, " ()<>[]\"'`|") {
342 return "[" + esc(trimScheme(u)) + "](" + u + ")"
343 }
344 }
345 return esc(u)
346}
347
348func trimScheme(u string) string {
349 u = strings.TrimPrefix(strings.TrimPrefix(u, "https://"), "http://")
350 if len(u) > 40 {
351 u = u[:39] + "β¦"
352 }
353 return u
354}
355
356// esc neutralizes markdown/HTML in user-supplied text.
357func esc(s string) string {
358 var sb strings.Builder
359 for _, r := range s {
360 switch r {
361 case '\\', '`', '*', '_', '[', ']', '(', ')', '#', '|', '<', '>', '!', '~':
362 sb.WriteRune('\\')
363 }
364 sb.WriteRune(r)
365 }
366 return sb.String()
367}