query.gno
7.40 Kb · 253 lines
1package hearthbounty
2
3import (
4 "chain/runtime"
5 "sort"
6
7 "gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
8)
9
10// Arbiter returns the current dispute arbiter address.
11func Arbiter() address {
12 return arbiter
13}
14
15// GetStats returns an immutable snapshot of protocol counters and escrow totals.
16func GetStats() BoardStats {
17 return BoardStats{
18 TotalBounties: bounties.Size(),
19 OpenCount: openCount,
20 ClaimedCount: claimedCount,
21 SubmittedCount: submittedCount,
22 DisputedCount: disputedCount,
23 SettledCount: settledCount,
24 CancelledCount: cancelledCount,
25 ProfileCount: profiles.Size(),
26 AgentCount: agentCount,
27 TotalEscrowUgnot: totalEscrowUgnot,
28 TotalSettledUgnot: totalSettledUgnot,
29 TotalRefundUgnot: totalRefundUgnot,
30 }
31}
32
33// GetBounty returns a value copy of a single bounty by ID.
34func GetBounty(id uint64) (BountyView, bool) {
35 b, ok := getBountyRecord(id)
36 if !ok {
37 return BountyView{}, false
38 }
39 return toBountyView(b), true
40}
41
42// GetProfile returns a value copy of a builder or AI agent profile.
43func GetProfile(addr address) (ProfileView, bool) {
44 raw := profiles.Get(string(addr))
45 if raw == nil {
46 return ProfileView{}, false
47 }
48 return toProfileView(raw.(*profileRecord)), true
49}
50
51// ExportStatsJSON returns global protocol stats as deterministic JSON for MCP agents.
52func ExportStatsJSON() string {
53 s := GetStats()
54 return hearthcore.NewJSONObject().
55 Str("realm_path", realmWebBase).
56 Str("arbiter", string(arbiter)).
57 Int64("chain_height", runtime.ChainHeight()).
58 Int64("total_bounties", int64(s.TotalBounties)).
59 Int64("open_count", int64(s.OpenCount)).
60 Int64("claimed_count", int64(s.ClaimedCount)).
61 Int64("submitted_count", int64(s.SubmittedCount)).
62 Int64("disputed_count", int64(s.DisputedCount)).
63 Int64("settled_count", int64(s.SettledCount)).
64 Int64("cancelled_count", int64(s.CancelledCount)).
65 Int64("profile_count", int64(s.ProfileCount)).
66 Int64("agent_count", int64(s.AgentCount)).
67 Int64("total_escrow_ugnot", s.TotalEscrowUgnot).
68 Int64("total_settled_ugnot", s.TotalSettledUgnot).
69 Int64("total_refund_ugnot", s.TotalRefundUgnot).
70 String()
71}
72
73// ExportBountyJSON returns a single bounty as deterministic JSON (or {"found":false}).
74func ExportBountyJSON(id uint64) string {
75 b, ok := getBountyRecord(id)
76 if !ok {
77 return `{"found":false}`
78 }
79 return encodeBountyJSON(b, true)
80}
81
82// ExportBountiesJSON lists bounties in reverse chronological order with optional
83// status, category, and minimum reward filters for autonomous agent discovery.
84func ExportBountiesJSON(statusFilter, categoryFilter string, minRewardUgnot int64, limit int) string {
85 st := normalizeFilter(statusFilter)
86 cat := normalizeFilter(categoryFilter)
87 if limit <= 0 || limit > 100 {
88 limit = 50
89 }
90
91 items := make([]string, 0)
92 bounties.ReverseIterate("", "", func(_ string, val any) bool {
93 b := val.(*bountyRecord)
94 if st != "" && string(b.Status) != st {
95 return false
96 }
97 if cat != "" && string(b.Category) != cat {
98 return false
99 }
100 reward := effectiveReward(b)
101 if minRewardUgnot > 0 && reward < minRewardUgnot {
102 return false
103 }
104 items = append(items, encodeBountyJSON(b, false))
105 return len(items) >= limit
106 })
107
108 return hearthcore.NewJSONObject().
109 Int64("chain_height", runtime.ChainHeight()).
110 Int64("count", int64(len(items))).
111 Raw("bounties", hearthcore.JoinJSONArray(items)).
112 String()
113}
114
115// ExportLeaderboardJSON returns deterministically sorted builders & AI agents.
116func ExportLeaderboardJSON(limit int) string {
117 if limit <= 0 || limit > 100 {
118 limit = 25
119 }
120 ranked := rankedProfiles()
121 if len(ranked) > limit {
122 ranked = ranked[:limit]
123 }
124
125 items := make([]string, 0, len(ranked))
126 for i, p := range ranked {
127 items = append(items, encodeProfileJSON(p, i+1))
128 }
129
130 return hearthcore.NewJSONObject().
131 Int64("count", int64(len(items))).
132 Raw("leaderboard", hearthcore.JoinJSONArray(items)).
133 String()
134}
135
136// ExportProfileJSON returns a single profile as deterministic JSON.
137func ExportProfileJSON(addrStr string) string {
138 raw := profiles.Get(addrStr)
139 if raw == nil {
140 return `{"found":false}`
141 }
142 p := raw.(*profileRecord)
143 return encodeProfileJSON(p, 0)
144}
145
146func effectiveReward(b *bountyRecord) int64 {
147 if b.EscrowUgnot > 0 {
148 return b.EscrowUgnot
149 }
150 if b.PayoutUgnot > 0 {
151 return b.PayoutUgnot
152 }
153 if b.RefundUgnot > 0 {
154 return b.RefundUgnot
155 }
156 return b.InitialUgnot
157}
158
159func encodeBountyJSON(b *bountyRecord, includeFound bool) string {
160 jb := hearthcore.NewJSONObject()
161 if includeFound {
162 jb.Bool("found", true)
163 }
164 jb.Uint64("id", b.ID).
165 Str("title", b.Title).
166 Str("spec", b.Spec).
167 Str("category", string(b.Category)).
168 Str("status", string(b.Status)).
169 Str("sponsor", string(b.Sponsor)).
170 Str("claimant", string(b.Claimant)).
171 Str("claim_note", b.ClaimNote).
172 Int64("escrow_ugnot", b.EscrowUgnot).
173 Int64("initial_ugnot", b.InitialUgnot).
174 Int64("payout_ugnot", b.PayoutUgnot).
175 Int64("refund_ugnot", b.RefundUgnot).
176 Str("reward_formatted", hearthcore.FormatGNOT(effectiveReward(b))).
177 Uint64("top_up_count", b.TopUpCount).
178 Int64("deadline_blocks", b.DeadlineBlocks).
179 Int64("created_height", b.CreatedHeight).
180 Int64("claimed_height", b.ClaimedHeight).
181 Int64("deadline_height", b.DeadlineHeight).
182 Int64("submitted_height", b.SubmittedHeight).
183 Int64("settled_height", b.SettledHeight).
184 Str("proof_url", b.ProofURL).
185 Str("proof_digest", b.ProofDigest).
186 Str("proof_summary", b.ProofSummary).
187 Uint64("submission_count", b.SubmissionCount).
188 Str("revision_feedback", b.RevisionFeedback).
189 Uint64("rating", uint64(b.Rating)).
190 Str("review_note", b.ReviewNote).
191 Str("dispute_reason", b.DisputeReason).
192 Str("dispute_opened_by", string(b.DisputeOpenedBy))
193 return jb.String()
194}
195
196func encodeProfileJSON(p *profileRecord, rank int) string {
197 tier := hearthcore.ComputeTier(p.SettledCount, p.EarnedUgnot, p.RatingSum, p.RatingCount)
198 jb := hearthcore.NewJSONObject()
199 if rank == 0 {
200 jb.Bool("found", true)
201 } else {
202 jb.Int64("rank", int64(rank))
203 }
204 jb.Str("address", string(p.Address)).
205 Str("handle", p.Handle).
206 Str("bio", p.Bio).
207 Str("skills", p.Skills).
208 Str("agent_endpoint", p.AgentEndpoint).
209 Bool("is_agent", p.IsAgent).
210 Str("tier", string(tier)).
211 Uint64("posted_count", p.PostedCount).
212 Uint64("claimed_count", p.ClaimedCount).
213 Uint64("settled_count", p.SettledCount).
214 Uint64("sponsored_settled", p.SponsoredSettled).
215 Int64("earned_ugnot", p.EarnedUgnot).
216 Str("earned_formatted", hearthcore.FormatGNOT(p.EarnedUgnot)).
217 Int64("sponsored_ugnot", p.SponsoredUgnot).
218 Str("rating_formatted", hearthcore.FormatRating(p.RatingSum, p.RatingCount)).
219 Uint64("rating_sum", p.RatingSum).
220 Uint64("rating_count", p.RatingCount)
221 return jb.String()
222}
223
224type profileRankSlice []*profileRecord
225
226func (s profileRankSlice) Len() int { return len(s) }
227func (s profileRankSlice) Swap(i, j int) {
228 s[i], s[j] = s[j], s[i]
229}
230func (s profileRankSlice) Less(i, j int) bool {
231 a, b := s[i], s[j]
232 if a.EarnedUgnot != b.EarnedUgnot {
233 return a.EarnedUgnot > b.EarnedUgnot
234 }
235 if a.SettledCount != b.SettledCount {
236 return a.SettledCount > b.SettledCount
237 }
238 if a.SponsoredUgnot != b.SponsoredUgnot {
239 return a.SponsoredUgnot > b.SponsoredUgnot
240 }
241 return string(a.Address) < string(b.Address)
242}
243
244func rankedProfiles() []*profileRecord {
245 list := make([]*profileRecord, 0, profiles.Size())
246 profiles.Iterate("", "", func(_ string, val any) bool {
247 list = append(list, val.(*profileRecord))
248 return false
249 })
250
251 sort.Sort(profileRankSlice(list))
252 return list
253}