types.gno
9.67 Kb · 380 lines
1// Package hearthcore provides deterministic escrow math, bounty lifecycle
2// primitives, reputation tier calculation, RFC-8259 JSON encoding, and
3// on-chain SVG visual builders for HearthBounty and any Gno.land realm that
4// imports it.
5package hearthcore
6
7import (
8 "strconv"
9 "strings"
10)
11
12const (
13 // Denom is the native settlement denomination on Gno.land.
14 Denom = "ugnot"
15
16 // UgnotPerGNOT is the number of micro-GNOT in 1 GNOT.
17 UgnotPerGNOT int64 = 1000000
18
19 // MinRewardUgnot enforces a 0.1 GNOT floor to discourage dust spam.
20 MinRewardUgnot int64 = 100000
21
22 // Deadline block bounds ensure deterministic block-height timeouts.
23 MinDeadlineBlocks int64 = 10
24 MaxDeadlineBlocks int64 = 2000000
25 DefaultDeadlineBlocks int64 = 50000
26
27 // MaxBps represents 100.00% in integer basis points.
28 MaxBps uint64 = 10000
29
30 // Text length limits keep storage deposits bounded and predictable.
31 MaxTitleLen = 120
32 MaxSpecLen = 4000
33 MaxNoteLen = 600
34 MaxURLLen = 300
35 MaxDigestLen = 128
36 MaxHandleLen = 40
37 MaxBioLen = 300
38 MaxSkillsLen = 200
39 MaxEndpointLen = 240
40)
41
42// Status represents a bounty's lifecycle state.
43type Status string
44
45const (
46 StatusOpen Status = "open"
47 StatusClaimed Status = "claimed"
48 StatusSubmitted Status = "submitted"
49 StatusDisputed Status = "disputed"
50 StatusSettled Status = "settled"
51 StatusCancelled Status = "cancelled"
52)
53
54// IsValid reports whether s is a known lifecycle status.
55func (s Status) IsValid() bool {
56 switch s {
57 case StatusOpen, StatusClaimed, StatusSubmitted, StatusDisputed, StatusSettled, StatusCancelled:
58 return true
59 default:
60 return false
61 }
62}
63
64// Label returns a concise display label for s.
65func (s Status) Label() string {
66 switch s {
67 case StatusOpen:
68 return "Open"
69 case StatusClaimed:
70 return "Claimed"
71 case StatusSubmitted:
72 return "Submitted"
73 case StatusDisputed:
74 return "Disputed"
75 case StatusSettled:
76 return "Settled"
77 case StatusCancelled:
78 return "Cancelled"
79 default:
80 return "Unknown"
81 }
82}
83
84// IconName returns a verified gnoweb icon identifier for s.
85func (s Status) IconName() string {
86 switch s {
87 case StatusOpen:
88 return "flame"
89 case StatusClaimed:
90 return "clock"
91 case StatusSubmitted:
92 return "upload"
93 case StatusDisputed:
94 return "warning"
95 case StatusSettled:
96 return "check-circle"
97 case StatusCancelled:
98 return "trash"
99 default:
100 return "info"
101 }
102}
103
104// StepIndex returns the 0-based progression index for active lifecycle steps.
105func (s Status) StepIndex() int {
106 switch s {
107 case StatusOpen:
108 return 0
109 case StatusClaimed:
110 return 1
111 case StatusSubmitted:
112 return 2
113 case StatusSettled:
114 return 3
115 case StatusDisputed:
116 return 2
117 default:
118 return -1
119 }
120}
121
122// Category classifies the kind of engineering work a bounty requests.
123type Category string
124
125const (
126 CatRealm Category = "realm"
127 CatTool Category = "tool"
128 CatAudit Category = "audit"
129 CatDocs Category = "docs"
130 CatAgent Category = "agent"
131)
132
133// AllCategories returns all supported categories in canonical order.
134func AllCategories() []Category {
135 return []Category{CatRealm, CatTool, CatAudit, CatDocs, CatAgent}
136}
137
138// ParseCategory normalizes and validates a category string.
139func ParseCategory(raw string) (Category, bool) {
140 c := Category(strings.ToLower(strings.TrimSpace(raw)))
141 switch c {
142 case CatRealm, CatTool, CatAudit, CatDocs, CatAgent:
143 return c, true
144 default:
145 return "", false
146 }
147}
148
149// Label returns a human-friendly title for c.
150func (c Category) Label() string {
151 switch c {
152 case CatRealm:
153 return "Realm"
154 case CatTool:
155 return "Tooling"
156 case CatAudit:
157 return "Security Audit"
158 case CatDocs:
159 return "Docs & Guides"
160 case CatAgent:
161 return "AI Agent"
162 default:
163 return "General"
164 }
165}
166
167// IconName returns a verified gnoweb icon identifier for c.
168func (c Category) IconName() string {
169 switch c {
170 case CatRealm:
171 return "code"
172 case CatTool:
173 return "settings"
174 case CatAudit:
175 return "shield"
176 case CatDocs:
177 return "book"
178 case CatAgent:
179 return "ai"
180 default:
181 return "tag"
182 }
183}
184
185// Tier represents a builder or agent's earned on-chain reputation rank.
186type Tier string
187
188const (
189 TierApprentice Tier = "Apprentice"
190 TierJourneyman Tier = "Journeyman"
191 TierMaster Tier = "Master"
192 TierGuildmaster Tier = "Guildmaster"
193)
194
195// ComputeTier deterministically assigns a reputation tier using pure integer math.
196func ComputeTier(settledCount uint64, earnedUgnot int64, ratingSum uint64, ratingCount uint64) Tier {
197 highQuality := ratingCount == 0 || ratingSum*10 >= ratingCount*45
198 if (settledCount >= 10 || earnedUgnot >= 500*UgnotPerGNOT) && highQuality {
199 return TierGuildmaster
200 }
201 if settledCount >= 5 || earnedUgnot >= 100*UgnotPerGNOT {
202 return TierMaster
203 }
204 if settledCount >= 2 || earnedUgnot >= 10*UgnotPerGNOT {
205 return TierJourneyman
206 }
207 return TierApprentice
208}
209
210// TierColor returns a high-contrast hex accent color for SVG badges.
211func (t Tier) Color() string {
212 switch t {
213 case TierGuildmaster:
214 return "#f59e0b"
215 case TierMaster:
216 return "#a78bfa"
217 case TierJourneyman:
218 return "#38bdf8"
219 default:
220 return "#34d399"
221 }
222}
223
224// ComputeSplit splits an escrowed ugnot balance between hunter and sponsor
225// using integer basis points (0..10000). It guarantees exact conservation:
226// hunterUgnot + sponsorRefundUgnot == escrowUgnot.
227func ComputeSplit(escrowUgnot int64, hunterBps uint64) (hunterUgnot int64, sponsorRefundUgnot int64) {
228 if escrowUgnot < 0 {
229 panic("hearthcore: negative escrow amount")
230 }
231 if hunterBps > MaxBps {
232 panic("hearthcore: hunterBps exceeds 10000")
233 }
234 if hunterBps == 0 {
235 return 0, escrowUgnot
236 }
237 if hunterBps == MaxBps {
238 return escrowUgnot, 0
239 }
240 // Divide first to prevent int64 multiplication overflow on large balances.
241 q := escrowUgnot / int64(MaxBps)
242 r := escrowUgnot % int64(MaxBps)
243 hunterUgnot = q*int64(hunterBps) + (r*int64(hunterBps))/int64(MaxBps)
244 sponsorRefundUgnot = escrowUgnot - hunterUgnot
245 return hunterUgnot, sponsorRefundUgnot
246}
247
248// FormatGNOT formats micro-GNOT into a deterministic human-readable GNOT string
249// without floating-point operations.
250func FormatGNOT(ugnot int64) string {
251 neg := false
252 if ugnot < 0 {
253 neg = true
254 ugnot = -ugnot
255 }
256 whole := ugnot / UgnotPerGNOT
257 frac := ugnot % UgnotPerGNOT
258
259 prefix := ""
260 if neg {
261 prefix = "-"
262 }
263 if frac == 0 {
264 return prefix + strconv.FormatInt(whole, 10) + ".00 GNOT"
265 }
266
267 fracStr := strconv.FormatInt(frac, 10)
268 for len(fracStr) < 6 {
269 fracStr = "0" + fracStr
270 }
271 // Keep at least 2 decimal digits, trimming trailing zeros beyond 2.
272 trimmed := strings.TrimRight(fracStr, "0")
273 if len(trimmed) < 2 {
274 trimmed = fracStr[:2]
275 }
276 return prefix + strconv.FormatInt(whole, 10) + "." + trimmed + " GNOT"
277}
278
279// FormatRating formats an integer rating sum/count into "X.Y / 5.0" or "Unrated".
280func FormatRating(sum uint64, count uint64) string {
281 if count == 0 {
282 return "Unrated"
283 }
284 tenths := (sum*10 + count/2) / count
285 whole := tenths / 10
286 rem := tenths % 10
287 return strconv.FormatUint(whole, 10) + "." + strconv.FormatUint(rem, 10) + " / 5.0"
288}
289
290// ShortAddr shortens a bech32 Gno address for compact tables and badges.
291func ShortAddr(addr address) string {
292 s := string(addr)
293 if len(s) <= 14 {
294 return s
295 }
296 return s[:8] + "..." + s[len(s)-4:]
297}
298
299// EscapeMath neutralizes unescaped dollar signs after Markdown sanitization so
300// gnoweb's math extension does not treat prices or user text as TeX formulas.
301func EscapeMath(s string) string {
302 return strings.ReplaceAll(s, "$", `\$`)
303}
304
305// NeutralizeGnoTags escapes any remaining <gno- or </gno- tag sequences
306// (case-insensitive) as defense-in-depth on top of p/nt/markdown/sanitize/v0
307// so user-authored text can never smuggle gnoweb UI directives.
308func NeutralizeGnoTags(s string) string {
309 lower := strings.ToLower(s)
310 if !strings.Contains(lower, "<gno-") && !strings.Contains(lower, "</gno-") {
311 return s
312 }
313 var b strings.Builder
314 b.Grow(len(s) + 8)
315 for i := 0; i < len(s); i++ {
316 if s[i] == '<' {
317 rem := lower[i:]
318 if strings.HasPrefix(rem, "<gno-") || strings.HasPrefix(rem, "</gno-") {
319 b.WriteString("<")
320 continue
321 }
322 }
323 b.WriteByte(s[i])
324 }
325 return b.String()
326}
327
328// ValidateText verifies a required or optional text field within rune/byte bounds.
329func ValidateText(field, value string, minLen, maxLen int) string {
330 trimmed := strings.TrimSpace(value)
331 if len(trimmed) < minLen {
332 panic("hearthcore: " + field + " is too short")
333 }
334 if len(trimmed) > maxLen {
335 panic("hearthcore: " + field + " exceeds maximum length")
336 }
337 return trimmed
338}
339
340// ValidateProofURL enforces safe http/https/ipfs proof links.
341func ValidateProofURL(raw string) string {
342 trimmed := ValidateText("proof URL", raw, 8, MaxURLLen)
343 if !strings.HasPrefix(trimmed, "https://") &&
344 !strings.HasPrefix(trimmed, "http://") &&
345 !strings.HasPrefix(trimmed, "ipfs://") {
346 panic("hearthcore: proof URL must start with https://, http://, or ipfs://")
347 }
348 if strings.ContainsAny(trimmed, " \t\r\n") {
349 panic("hearthcore: proof URL must not contain whitespace")
350 }
351 return trimmed
352}
353
354// ValidateProofDigest verifies a non-empty hexadecimal or prefixed content digest.
355func ValidateProofDigest(raw string) string {
356 trimmed := ValidateText("proof digest", raw, 8, MaxDigestLen)
357 if strings.ContainsAny(trimmed, " \t\r\n") {
358 panic("hearthcore: proof digest must not contain whitespace")
359 }
360 return trimmed
361}
362
363// ValidateDeadlineBlocks checks that a bounty deadline window is within safe bounds.
364func ValidateDeadlineBlocks(blocks int64) int64 {
365 if blocks == 0 {
366 return DefaultDeadlineBlocks
367 }
368 if blocks < MinDeadlineBlocks || blocks > MaxDeadlineBlocks {
369 panic("hearthcore: deadlineBlocks out of allowed range")
370 }
371 return blocks
372}
373
374// ValidateRating checks that a settlement rating is in [1, 5].
375func ValidateRating(rating uint8) uint8 {
376 if rating < 1 || rating > 5 {
377 panic("hearthcore: rating must be between 1 and 5")
378 }
379 return rating
380}