state.gno
7.43 Kb · 294 lines
1// Package hearthbounty implements the HearthBounty on-chain micro-bounty
2// escrow, verifiable proof registry, reputation leaderboard, and native
3// gnoweb interactive interface on Gno.land.
4package hearthbounty
5
6import (
7 "strings"
8
9 "gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
10 "gno.land/p/nt/avl/v0"
11 "gno.land/p/nt/mux/v0"
12 "gno.land/p/nt/seqid/v0"
13)
14
15// Bounty holds the canonical on-chain state of a micro-bounty.
16type bountyRecord struct {
17 ID uint64
18 Title string
19 Spec string
20 Category hearthcore.Category
21 Status hearthcore.Status
22 Sponsor address
23 Claimant address
24 ClaimNote string
25 EscrowUgnot int64
26 InitialUgnot int64
27 PayoutUgnot int64
28 RefundUgnot int64
29 TopUpCount uint64
30 DeadlineBlocks int64
31 CreatedHeight int64
32 ClaimedHeight int64
33 DeadlineHeight int64
34 SubmittedHeight int64
35 SettledHeight int64
36 ProofURL string
37 ProofDigest string
38 ProofSummary string
39 SubmissionCount uint64
40 RevisionFeedback string
41 Rating uint8
42 ReviewNote string
43 DisputeReason string
44 DisputeOpenedBy address
45}
46
47// profileRecord stores a builder or autonomous AI agent profile and on-chain stats.
48type profileRecord struct {
49 Address address
50 Handle string
51 Bio string
52 Skills string
53 AgentEndpoint string
54 IsAgent bool
55 PostedCount uint64
56 ClaimedCount uint64
57 SettledCount uint64
58 SponsoredSettled uint64
59 EarnedUgnot int64
60 SponsoredUgnot int64
61 RatingSum uint64
62 RatingCount uint64
63 RegisteredHeight int64
64}
65
66// BountyView is an immutable value-copy projection of a bounty safe for external callers.
67type BountyView struct {
68 ID uint64
69 Title string
70 Spec string
71 Category hearthcore.Category
72 Status hearthcore.Status
73 Sponsor address
74 Claimant address
75 ClaimNote string
76 EscrowUgnot int64
77 InitialUgnot int64
78 PayoutUgnot int64
79 RefundUgnot int64
80 TopUpCount uint64
81 DeadlineBlocks int64
82 CreatedHeight int64
83 ClaimedHeight int64
84 DeadlineHeight int64
85 SubmittedHeight int64
86 SettledHeight int64
87 ProofURL string
88 ProofDigest string
89 ProofSummary string
90 SubmissionCount uint64
91 RevisionFeedback string
92 Rating uint8
93 ReviewNote string
94 DisputeReason string
95 DisputeOpenedBy address
96}
97
98// ProfileView is an immutable value-copy projection of a builder or AI agent profile.
99type ProfileView struct {
100 Address address
101 Handle string
102 Bio string
103 Skills string
104 AgentEndpoint string
105 IsAgent bool
106 Tier hearthcore.Tier
107 PostedCount uint64
108 ClaimedCount uint64
109 SettledCount uint64
110 SponsoredSettled uint64
111 EarnedUgnot int64
112 SponsoredUgnot int64
113 RatingSum uint64
114 RatingCount uint64
115 RegisteredHeight int64
116}
117
118// BoardStats summarizes global protocol activity and escrow invariants.
119type BoardStats struct {
120 TotalBounties int
121 OpenCount int
122 ClaimedCount int
123 SubmittedCount int
124 DisputedCount int
125 SettledCount int
126 CancelledCount int
127 ProfileCount int
128 AgentCount int
129 TotalEscrowUgnot int64
130 TotalSettledUgnot int64
131 TotalRefundUgnot int64
132}
133
134// All state variables are unexported so no external package can mutate realm state directly.
135var (
136 arbiter address
137 realmWebBase string
138 bountySeq seqid.ID
139 bounties *avl.Tree // seqid string -> *bountyRecord
140 profiles *avl.Tree // address string -> *profileRecord
141 openCount int
142 claimedCount int
143 submittedCount int
144 disputedCount int
145 settledCount int
146 cancelledCount int
147 agentCount int
148 totalEscrowUgnot int64
149 totalSettledUgnot int64
150 totalRefundUgnot int64
151 router *mux.Router
152)
153
154func init(cur realm) {
155 arbiter = cur.Previous().Address()
156 realmWebBase = deriveWebBase(cur.PkgPath())
157 bounties = avl.NewTree()
158 profiles = avl.NewTree()
159 initRouter()
160}
161
162func deriveWebBase(pkgPath string) string {
163 idx := strings.Index(pkgPath, "/")
164 if idx == -1 {
165 return "/r/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthbounty/v0"
166 }
167 return pkgPath[idx:]
168}
169
170func bountyKey(id uint64) string {
171 return seqid.ID(id).String()
172}
173
174func getBountyRecord(id uint64) (*bountyRecord, bool) {
175 if id == 0 {
176 return nil, false
177 }
178 raw := bounties.Get(bountyKey(id))
179 if raw == nil {
180 return nil, false
181 }
182 return raw.(*bountyRecord), true
183}
184
185func getOrCreateProfile(addr address, height int64) *profileRecord {
186 key := string(addr)
187 if raw := profiles.Get(key); raw != nil {
188 return raw.(*profileRecord)
189 }
190 p := &profileRecord{
191 Address: addr,
192 Handle: hearthcore.ShortAddr(addr),
193 RegisteredHeight: height,
194 }
195 profiles.Set(key, p)
196 return p
197}
198
199func toBountyView(b *bountyRecord) BountyView {
200 return BountyView{
201 ID: b.ID,
202 Title: b.Title,
203 Spec: b.Spec,
204 Category: b.Category,
205 Status: b.Status,
206 Sponsor: b.Sponsor,
207 Claimant: b.Claimant,
208 ClaimNote: b.ClaimNote,
209 EscrowUgnot: b.EscrowUgnot,
210 InitialUgnot: b.InitialUgnot,
211 PayoutUgnot: b.PayoutUgnot,
212 RefundUgnot: b.RefundUgnot,
213 TopUpCount: b.TopUpCount,
214 DeadlineBlocks: b.DeadlineBlocks,
215 CreatedHeight: b.CreatedHeight,
216 ClaimedHeight: b.ClaimedHeight,
217 DeadlineHeight: b.DeadlineHeight,
218 SubmittedHeight: b.SubmittedHeight,
219 SettledHeight: b.SettledHeight,
220 ProofURL: b.ProofURL,
221 ProofDigest: b.ProofDigest,
222 ProofSummary: b.ProofSummary,
223 SubmissionCount: b.SubmissionCount,
224 RevisionFeedback: b.RevisionFeedback,
225 Rating: b.Rating,
226 ReviewNote: b.ReviewNote,
227 DisputeReason: b.DisputeReason,
228 DisputeOpenedBy: b.DisputeOpenedBy,
229 }
230}
231
232func toProfileView(p *profileRecord) ProfileView {
233 return ProfileView{
234 Address: p.Address,
235 Handle: p.Handle,
236 Bio: p.Bio,
237 Skills: p.Skills,
238 AgentEndpoint: p.AgentEndpoint,
239 IsAgent: p.IsAgent,
240 Tier: hearthcore.ComputeTier(p.SettledCount, p.EarnedUgnot, p.RatingSum, p.RatingCount),
241 PostedCount: p.PostedCount,
242 ClaimedCount: p.ClaimedCount,
243 SettledCount: p.SettledCount,
244 SponsoredSettled: p.SponsoredSettled,
245 EarnedUgnot: p.EarnedUgnot,
246 SponsoredUgnot: p.SponsoredUgnot,
247 RatingSum: p.RatingSum,
248 RatingCount: p.RatingCount,
249 RegisteredHeight: p.RegisteredHeight,
250 }
251}
252
253func decrementStatusCount(st hearthcore.Status) {
254 switch st {
255 case hearthcore.StatusOpen:
256 openCount--
257 case hearthcore.StatusClaimed:
258 claimedCount--
259 case hearthcore.StatusSubmitted:
260 submittedCount--
261 case hearthcore.StatusDisputed:
262 disputedCount--
263 case hearthcore.StatusSettled:
264 settledCount--
265 case hearthcore.StatusCancelled:
266 cancelledCount--
267 }
268}
269
270func incrementStatusCount(st hearthcore.Status) {
271 switch st {
272 case hearthcore.StatusOpen:
273 openCount++
274 case hearthcore.StatusClaimed:
275 claimedCount++
276 case hearthcore.StatusSubmitted:
277 submittedCount++
278 case hearthcore.StatusDisputed:
279 disputedCount++
280 case hearthcore.StatusSettled:
281 settledCount++
282 case hearthcore.StatusCancelled:
283 cancelledCount++
284 }
285}
286
287func transitionStatus(b *bountyRecord, next hearthcore.Status) {
288 if b.Status == next {
289 return
290 }
291 decrementStatusCount(b.Status)
292 b.Status = next
293 incrementStatusCount(next)
294}