quillbounty.gno
11.03 Kb · 407 lines
1package quillbounty
2
3import (
4 "chain"
5 "chain/banker"
6 "chain/runtime"
7 "chain/runtime/unsafe"
8 "math/overflow"
9 "strconv"
10 "strings"
11
12 "gno.land/p/nt/avl/v0"
13)
14
15var (
16 bounties *avl.Tree // padID(id) -> *Bounty
17 builders *avl.Tree // address.String() -> *BuilderProfile
18 nextBountyID uint64 = 1
19 totalFunded int64
20 totalPaidOut int64
21 totalCompleted int
22)
23
24func init() {
25 bounties = avl.NewTree()
26 builders = avl.NewTree()
27}
28
29// CreateBounty creates a new ugnot-escrowed bounty on Gno.land.
30// Caller must attach native ugnot via transaction send envelope.
31// Security: enforces runtime.AssertOriginCall() + cur.Previous().IsUserCall()
32// before inspecting unsafe.OriginSend() per Interrealm v2 payment guidelines.
33func CreateBounty(
34 cur realm,
35 title string,
36 category string,
37 spec string,
38 reviewerAddr string,
39 durationBlocks int64,
40) uint64 {
41 runtime.AssertOriginCall()
42 prev := cur.Previous()
43 if !prev.IsUserCall() {
44 panic("CreateBounty: direct EOA call required")
45 }
46
47 title = strings.TrimSpace(title)
48 spec = strings.TrimSpace(spec)
49 if len(title) < 3 || len(title) > MaxTitleLen {
50 panic("CreateBounty: title must be 3..120 characters")
51 }
52 if len(spec) < 10 || len(spec) > MaxSpecLen {
53 panic("CreateBounty: spec must be 10..4000 characters")
54 }
55 if durationBlocks < MinDurationBlocks || durationBlocks > MaxDurationBlocks {
56 panic("CreateBounty: durationBlocks out of allowed range")
57 }
58
59 sent := unsafe.OriginSend()
60 if len(sent) != 1 || sent[0].Denom != DenomUgnot || sent[0].Amount <= 0 {
61 panic("CreateBounty: must send positive ugnot only")
62 }
63 amount := sent[0].Amount
64
65 sponsor := prev.Address()
66 reviewer := sponsor
67 reviewerAddr = strings.TrimSpace(reviewerAddr)
68 if reviewerAddr != "" {
69 rev := address(reviewerAddr)
70 if !rev.IsValid() {
71 panic("CreateBounty: invalid reviewer address")
72 }
73 reviewer = rev
74 }
75
76 newTotalFunded, ok := overflow.Add64(totalFunded, amount)
77 if !ok {
78 panic("CreateBounty: totalFunded overflow")
79 }
80
81 height := runtime.ChainHeight()
82 id := nextBountyID
83 nextBountyID++
84
85 b := &Bounty{
86 ID: id,
87 Sponsor: sponsor,
88 Reviewer: reviewer,
89 Title: title,
90 Category: normalizeCategory(category),
91 Spec: spec,
92 TotalFunded: amount,
93 RemainingUgnot: amount,
94 CreatedHeight: height,
95 DeadlineHeight: height + durationBlocks,
96 Status: StatusOpen,
97 Submissions: make([]*Submission, 0),
98 }
99 bounties.Set(padID(id), b)
100 totalFunded = newTotalFunded
101
102 chain.Emit(
103 "BountyCreated",
104 "bounty_id", strconv.FormatUint(id, 10),
105 "sponsor", sponsor.String(),
106 "reviewer", reviewer.String(),
107 "category", b.Category,
108 "amount_ugnot", strconv.FormatInt(amount, 10),
109 "deadline_height", strconv.FormatInt(b.DeadlineHeight, 10),
110 )
111 return id
112}
113
114// TopUpBounty allows anyone to add additional ugnot funding to an open bounty.
115func TopUpBounty(cur realm, bountyID uint64) {
116 runtime.AssertOriginCall()
117 prev := cur.Previous()
118 if !prev.IsUserCall() {
119 panic("TopUpBounty: direct EOA call required")
120 }
121
122 b := mustGetBounty(bountyID)
123 if b.Status != StatusOpen {
124 panic("TopUpBounty: bounty is not open")
125 }
126 if runtime.ChainHeight() >= b.DeadlineHeight {
127 panic("TopUpBounty: bounty deadline already passed")
128 }
129
130 sent := unsafe.OriginSend()
131 if len(sent) != 1 || sent[0].Denom != DenomUgnot || sent[0].Amount <= 0 {
132 panic("TopUpBounty: must send positive ugnot only")
133 }
134 amount := sent[0].Amount
135
136 newRem, ok1 := overflow.Add64(b.RemainingUgnot, amount)
137 newTot, ok2 := overflow.Add64(b.TotalFunded, amount)
138 newGlobal, ok3 := overflow.Add64(totalFunded, amount)
139 if !ok1 || !ok2 || !ok3 {
140 panic("TopUpBounty: arithmetic overflow")
141 }
142
143 b.RemainingUgnot = newRem
144 b.TotalFunded = newTot
145 totalFunded = newGlobal
146
147 chain.Emit(
148 "BountyToppedUp",
149 "bounty_id", strconv.FormatUint(bountyID, 10),
150 "funder", prev.Address().String(),
151 "amount_ugnot", strconv.FormatInt(amount, 10),
152 "remaining_ugnot", strconv.FormatInt(b.RemainingUgnot, 10),
153 )
154}
155
156// SubmitWork submits verifiable proof-of-work (PR/repo/gnoweb link + commit hash) on-chain.
157func SubmitWork(
158 cur realm,
159 bountyID uint64,
160 proofURL string,
161 commitHash string,
162 summary string,
163) uint64 {
164 b := mustGetBounty(bountyID)
165 if b.Status != StatusOpen {
166 panic("SubmitWork: bounty is not open")
167 }
168 if runtime.ChainHeight() >= b.DeadlineHeight {
169 panic("SubmitWork: bounty deadline has passed")
170 }
171
172 builder := cur.Previous().Address()
173 if builder == b.Sponsor {
174 panic("SubmitWork: sponsor cannot submit to own bounty")
175 }
176
177 proofURL = strings.TrimSpace(proofURL)
178 commitHash = strings.TrimSpace(commitHash)
179 summary = strings.TrimSpace(summary)
180 if len(proofURL) < 8 || len(proofURL) > MaxURLLen ||
181 (!strings.HasPrefix(proofURL, "https://") && !strings.HasPrefix(proofURL, "gno.land/")) {
182 panic("SubmitWork: proofURL must start with https:// or gno.land/")
183 }
184 if len(summary) < 5 || len(summary) > MaxSummaryLen {
185 panic("SubmitWork: summary must be 5..1000 characters")
186 }
187
188 subID := uint64(len(b.Submissions) + 1)
189 sub := &Submission{
190 ID: subID,
191 BountyID: bountyID,
192 Builder: builder,
193 ProofURL: proofURL,
194 CommitHash: commitHash,
195 Summary: summary,
196 Status: SubPending,
197 SubmittedAt: runtime.ChainHeight(),
198 }
199 b.Submissions = append(b.Submissions, sub)
200
201 chain.Emit(
202 "WorkSubmitted",
203 "bounty_id", strconv.FormatUint(bountyID, 10),
204 "submission_id", strconv.FormatUint(subID, 10),
205 "builder", builder.String(),
206 "proof_url", proofURL,
207 )
208 return subID
209}
210
211// RequestRevision records an on-chain review note asking the builder to refine their submission.
212func RequestRevision(cur realm, bountyID uint64, submissionID uint64, feedback string) {
213 b := mustGetBounty(bountyID)
214 caller := cur.Previous().Address()
215 if caller != b.Sponsor && caller != b.Reviewer {
216 panic("RequestRevision: unauthorized reviewer")
217 }
218 if b.Status != StatusOpen {
219 panic("RequestRevision: bounty is not open")
220 }
221
222 sub := mustGetSubmission(b, submissionID)
223 if sub.Status == SubApproved {
224 panic("RequestRevision: submission already approved")
225 }
226 feedback = strings.TrimSpace(feedback)
227 if len(feedback) < 3 || len(feedback) > MaxSummaryLen {
228 panic("RequestRevision: feedback must be 3..1000 characters")
229 }
230
231 sub.Status = SubNeedsRevision
232 sub.ReviewNote = feedback
233 sub.ReviewedAt = runtime.ChainHeight()
234
235 chain.Emit(
236 "RevisionRequested",
237 "bounty_id", strconv.FormatUint(bountyID, 10),
238 "submission_id", strconv.FormatUint(submissionID, 10),
239 "reviewer", caller.String(),
240 )
241}
242
243// ApproveSubmission approves a submission and atomically releases escrowed ugnot.
244// Pass payoutUgnot == 0 to release the entire remaining bounty balance (100% single-winner payout),
245// or pass 0 < payoutUgnot <= RemainingUgnot for multi-winner split payouts.
246func ApproveSubmission(
247 cur realm,
248 bountyID uint64,
249 submissionID uint64,
250 payoutUgnot int64,
251 reviewNote string,
252) {
253 b := mustGetBounty(bountyID)
254 caller := cur.Previous().Address()
255 if caller != b.Sponsor && caller != b.Reviewer {
256 panic("ApproveSubmission: unauthorized reviewer")
257 }
258 if b.Status != StatusOpen {
259 panic("ApproveSubmission: bounty is not open")
260 }
261 if b.RemainingUgnot <= 0 {
262 panic("ApproveSubmission: no remaining escrow")
263 }
264
265 sub := mustGetSubmission(b, submissionID)
266 if sub.Status == SubApproved {
267 panic("ApproveSubmission: submission already approved")
268 }
269
270 payout := payoutUgnot
271 if payout == 0 {
272 payout = b.RemainingUgnot
273 }
274 if payout <= 0 || payout > b.RemainingUgnot {
275 panic("ApproveSubmission: invalid payout amount")
276 }
277
278 newGlobalPaid, ok := overflow.Add64(totalPaidOut, payout)
279 if !ok {
280 panic("ApproveSubmission: totalPaidOut overflow")
281 }
282
283 // Effects first (Checks-Effects-Interactions)
284 sub.Status = SubApproved
285 sub.PayoutUgnot = payout
286 sub.ReviewNote = strings.TrimSpace(reviewNote)
287 sub.ReviewedAt = runtime.ChainHeight()
288
289 b.RemainingUgnot -= payout
290 if b.RemainingUgnot == 0 {
291 b.Status = StatusCompleted
292 totalCompleted++
293 }
294 totalPaidOut = newGlobalPaid
295 recordBuilderReputation(sub.Builder, payout, bountyID)
296
297 // Interaction last: release escrowed ugnot from realm vault to winning builder
298 bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
299 bnk.SendCoins(cur.Address(), sub.Builder, chain.NewCoins(chain.NewCoin(DenomUgnot, payout)))
300
301 chain.Emit(
302 "SubmissionApproved",
303 "bounty_id", strconv.FormatUint(bountyID, 10),
304 "submission_id", strconv.FormatUint(submissionID, 10),
305 "builder", sub.Builder.String(),
306 "payout_ugnot", strconv.FormatInt(payout, 10),
307 "remaining_ugnot", strconv.FormatInt(b.RemainingUgnot, 10),
308 )
309}
310
311// RefundExpired returns remaining escrowed ugnot to the sponsor after deadlineHeight passes.
312func RefundExpired(cur realm, bountyID uint64) int64 {
313 b := mustGetBounty(bountyID)
314 caller := cur.Previous().Address()
315 if caller != b.Sponsor {
316 panic("RefundExpired: only sponsor can reclaim expired funds")
317 }
318 if b.Status != StatusOpen {
319 panic("RefundExpired: bounty is not open")
320 }
321 if runtime.ChainHeight() < b.DeadlineHeight {
322 panic("RefundExpired: deadline height not reached yet")
323 }
324 if b.RemainingUgnot <= 0 {
325 panic("RefundExpired: nothing to refund")
326 }
327
328 refundAmt := b.RemainingUgnot
329 b.RemainingUgnot = 0
330 b.Status = StatusExpired
331
332 bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
333 bnk.SendCoins(cur.Address(), b.Sponsor, chain.NewCoins(chain.NewCoin(DenomUgnot, refundAmt)))
334
335 chain.Emit(
336 "BountyRefunded",
337 "bounty_id", strconv.FormatUint(bountyID, 10),
338 "sponsor", b.Sponsor.String(),
339 "refund_ugnot", strconv.FormatInt(refundAmt, 10),
340 )
341 return refundAmt
342}
343
344// GetBounty returns a read-only copy of a bounty for cross-realm composability.
345func GetBounty(bountyID uint64) *Bounty {
346 raw := bounties.Get(padID(bountyID))
347 if raw == nil {
348 return nil
349 }
350 b := raw.(*Bounty)
351 cp := *b
352 cp.Submissions = make([]*Submission, len(b.Submissions))
353 for i, s := range b.Submissions {
354 sc := *s
355 cp.Submissions[i] = &sc
356 }
357 return &cp
358}
359
360// GetBuilderProfile returns on-chain reputation stats for a builder address.
361func GetBuilderProfile(addr address) *BuilderProfile {
362 raw := builders.Get(addr.String())
363 if raw == nil {
364 return nil
365 }
366 p := raw.(*BuilderProfile)
367 cp := *p
368 return &cp
369}
370
371// Stats returns aggregate protocol metrics (totalBounties, totalCompleted, totalFundedUgnot, totalPaidOutUgnot).
372func Stats() (int, int, int64, int64) {
373 return bounties.Size(), totalCompleted, totalFunded, totalPaidOut
374}
375
376func mustGetBounty(bountyID uint64) *Bounty {
377 raw := bounties.Get(padID(bountyID))
378 if raw == nil {
379 panic("bounty not found")
380 }
381 return raw.(*Bounty)
382}
383
384func mustGetSubmission(b *Bounty, submissionID uint64) *Submission {
385 if submissionID == 0 || int(submissionID) > len(b.Submissions) {
386 panic("submission not found")
387 }
388 return b.Submissions[submissionID-1]
389}
390
391func recordBuilderReputation(addr address, payout int64, bountyID uint64) {
392 key := addr.String()
393 var prof *BuilderProfile
394 if raw := builders.Get(key); raw != nil {
395 prof = raw.(*BuilderProfile)
396 } else {
397 prof = &BuilderProfile{Address: addr}
398 builders.Set(key, prof)
399 }
400 prof.CompletedCount++
401 newEarned, ok := overflow.Add64(prof.TotalEarnedUgnot, payout)
402 if !ok {
403 panic("builder earned overflow")
404 }
405 prof.TotalEarnedUgnot = newEarned
406 prof.LastBountyID = bountyID
407}