Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

escrow.gno

14.20 Kb · 483 lines
  1package hearthbounty
  2
  3import (
  4	"chain"
  5	"chain/banker"
  6	"chain/runtime"
  7	"chain/runtime/unsafe"
  8	"strconv"
  9	"strings"
 10
 11	"gno.land/p/g1x582angdzrqadgv4382j6y7ju6fj5wll6yuuq4/hearthcore/v0"
 12)
 13
 14// SetArbiter transfers dispute-resolution authority to a new valid Gno address.
 15func SetArbiter(cur realm, next address) {
 16	caller := cur.Previous().Address()
 17	if caller != arbiter {
 18		panic("hearthbounty: only current arbiter may transfer arbiter role")
 19	}
 20	if !next.IsValid() {
 21		panic("hearthbounty: next arbiter address is invalid")
 22	}
 23	arbiter = next
 24	chain.Emit(
 25		"ArbiterUpdated",
 26		"previous", string(caller),
 27		"arbiter", string(next),
 28	)
 29}
 30
 31// RegisterProfile creates or updates a builder or autonomous AI agent profile.
 32// Providing a non-empty agentEndpoint marks the profile as an MCP-capable AI agent.
 33func RegisterProfile(cur realm, handle, bio, skills, agentEndpoint string) {
 34	caller := cur.Previous().Address()
 35	cleanHandle := hearthcore.ValidateText("handle", handle, 2, hearthcore.MaxHandleLen)
 36	cleanBio := hearthcore.ValidateText("bio", bio, 0, hearthcore.MaxBioLen)
 37	cleanSkills := hearthcore.ValidateText("skills", skills, 0, hearthcore.MaxSkillsLen)
 38	cleanEndpoint := hearthcore.ValidateText("agentEndpoint", agentEndpoint, 0, hearthcore.MaxEndpointLen)
 39
 40	p := getOrCreateProfile(caller, runtime.ChainHeight())
 41	wasAgent := p.IsAgent
 42	p.Handle = cleanHandle
 43	p.Bio = cleanBio
 44	p.Skills = cleanSkills
 45	p.AgentEndpoint = cleanEndpoint
 46	p.IsAgent = cleanEndpoint != ""
 47
 48	if !wasAgent && p.IsAgent {
 49		agentCount++
 50	} else if wasAgent && !p.IsAgent {
 51		agentCount--
 52	}
 53
 54	chain.Emit(
 55		"ProfileRegistered",
 56		"address", string(caller),
 57		"handle", cleanHandle,
 58		"is_agent", strconv.FormatBool(p.IsAgent),
 59	)
 60}
 61
 62// PostBounty creates a new micro-bounty funded with attached ugnot escrow.
 63// Requires a direct EOA call (AssertOriginCall + cur.Previous().IsUserCall()).
 64func PostBounty(cur realm, title, spec, category string, deadlineBlocks int64) uint64 {
 65	runtime.AssertOriginCall()
 66	if !cur.Previous().IsUserCall() {
 67		panic("hearthbounty: direct EOA call required to fund escrow")
 68	}
 69
 70	amount := requireSingleUgnotSend(hearthcore.MinRewardUgnot)
 71	cleanTitle := hearthcore.ValidateText("title", title, 4, hearthcore.MaxTitleLen)
 72	cleanSpec := hearthcore.ValidateText("spec", spec, 10, hearthcore.MaxSpecLen)
 73	cat, ok := hearthcore.ParseCategory(category)
 74	if !ok {
 75		panic("hearthbounty: invalid category (use realm, tool, audit, docs, or agent)")
 76	}
 77	blocks := hearthcore.ValidateDeadlineBlocks(deadlineBlocks)
 78
 79	sponsor := cur.Previous().Address()
 80	height := runtime.ChainHeight()
 81	nextID := uint64(bountySeq.Next())
 82
 83	rec := &bountyRecord{
 84		ID:             nextID,
 85		Title:          cleanTitle,
 86		Spec:           cleanSpec,
 87		Category:       cat,
 88		Status:         hearthcore.StatusOpen,
 89		Sponsor:        sponsor,
 90		EscrowUgnot:    amount,
 91		InitialUgnot:   amount,
 92		DeadlineBlocks: blocks,
 93		CreatedHeight:  height,
 94	}
 95
 96	bounties.Set(bountyKey(nextID), rec)
 97	openCount++
 98	totalEscrowUgnot += amount
 99
100	sp := getOrCreateProfile(sponsor, height)
101	sp.PostedCount++
102	sp.SponsoredUgnot += amount
103
104	chain.Emit(
105		"BountyPosted",
106		"id", strconv.FormatUint(nextID, 10),
107		"sponsor", string(sponsor),
108		"category", string(cat),
109		"escrow_ugnot", strconv.FormatInt(amount, 10),
110		"deadline_blocks", strconv.FormatInt(blocks, 10),
111	)
112
113	return nextID
114}
115
116// TopUpBounty allows any supporter to crowd-fund additional ugnot into an open
117// or claimed bounty's escrow pool.
118func TopUpBounty(cur realm, id uint64) int64 {
119	runtime.AssertOriginCall()
120	if !cur.Previous().IsUserCall() {
121		panic("hearthbounty: direct EOA call required to top up escrow")
122	}
123
124	amount := requireSingleUgnotSend(1)
125	b, ok := getBountyRecord(id)
126	if !ok {
127		panic("hearthbounty: bounty not found")
128	}
129	if b.Status != hearthcore.StatusOpen && b.Status != hearthcore.StatusClaimed {
130		panic("hearthbounty: can only top up open or claimed bounties")
131	}
132
133	donor := cur.Previous().Address()
134	b.EscrowUgnot += amount
135	b.TopUpCount++
136	totalEscrowUgnot += amount
137
138	dp := getOrCreateProfile(donor, runtime.ChainHeight())
139	dp.SponsoredUgnot += amount
140
141	chain.Emit(
142		"BountyToppedUp",
143		"id", strconv.FormatUint(id, 10),
144		"donor", string(donor),
145		"added_ugnot", strconv.FormatInt(amount, 10),
146		"escrow_ugnot", strconv.FormatInt(b.EscrowUgnot, 10),
147	)
148
149	return b.EscrowUgnot
150}
151
152// ClaimBounty reserves an open bounty for a builder or autonomous AI agent.
153// Sponsors cannot claim their own bounty to preserve reputation integrity.
154func ClaimBounty(cur realm, id uint64, agentNote string) {
155	b, ok := getBountyRecord(id)
156	if !ok {
157		panic("hearthbounty: bounty not found")
158	}
159	if b.Status != hearthcore.StatusOpen {
160		panic("hearthbounty: bounty is not open for claiming")
161	}
162
163	caller := cur.Previous().Address()
164	if caller == b.Sponsor {
165		panic("hearthbounty: sponsor cannot claim own bounty")
166	}
167	cleanNote := hearthcore.ValidateText("agentNote", agentNote, 0, hearthcore.MaxNoteLen)
168
169	height := runtime.ChainHeight()
170	transitionStatus(b, hearthcore.StatusClaimed)
171	b.Claimant = caller
172	b.ClaimNote = cleanNote
173	b.ClaimedHeight = height
174	b.DeadlineHeight = height + b.DeadlineBlocks
175
176	hp := getOrCreateProfile(caller, height)
177	hp.ClaimedCount++
178
179	chain.Emit(
180		"BountyClaimed",
181		"id", strconv.FormatUint(id, 10),
182		"claimant", string(caller),
183		"deadline_height", strconv.FormatInt(b.DeadlineHeight, 10),
184	)
185}
186
187// ReleaseExpiredClaim returns a claimed bounty to Open when either the claimant
188// voluntarily steps down or the deterministic block-height deadline has elapsed.
189func ReleaseExpiredClaim(cur realm, id uint64) {
190	b, ok := getBountyRecord(id)
191	if !ok {
192		panic("hearthbounty: bounty not found")
193	}
194	if b.Status != hearthcore.StatusClaimed {
195		panic("hearthbounty: bounty is not currently claimed")
196	}
197
198	caller := cur.Previous().Address()
199	height := runtime.ChainHeight()
200	if caller != b.Claimant && height <= b.DeadlineHeight {
201		panic("hearthbounty: claim deadline has not expired yet")
202	}
203
204	prevClaimant := b.Claimant
205	transitionStatus(b, hearthcore.StatusOpen)
206	b.Claimant = ""
207	b.ClaimNote = ""
208	b.ClaimedHeight = 0
209	b.DeadlineHeight = 0
210	b.RevisionFeedback = ""
211
212	chain.Emit(
213		"ClaimReleased",
214		"id", strconv.FormatUint(id, 10),
215		"previous_claimant", string(prevClaimant),
216		"released_by", string(caller),
217	)
218}
219
220// SubmitProof records a verifiable deliverable URL, cryptographic digest, and
221// implementation summary for review by the sponsor.
222func SubmitProof(cur realm, id uint64, proofURL, proofDigest, summary string) {
223	b, ok := getBountyRecord(id)
224	if !ok {
225		panic("hearthbounty: bounty not found")
226	}
227	if b.Status != hearthcore.StatusClaimed {
228		panic("hearthbounty: bounty must be claimed before submitting proof")
229	}
230
231	caller := cur.Previous().Address()
232	if caller != b.Claimant {
233		panic("hearthbounty: only the active claimant may submit proof")
234	}
235
236	cleanURL := hearthcore.ValidateProofURL(proofURL)
237	cleanDigest := hearthcore.ValidateProofDigest(proofDigest)
238	cleanSummary := hearthcore.ValidateText("summary", summary, 6, hearthcore.MaxNoteLen)
239
240	transitionStatus(b, hearthcore.StatusSubmitted)
241	b.ProofURL = cleanURL
242	b.ProofDigest = cleanDigest
243	b.ProofSummary = cleanSummary
244	b.SubmittedHeight = runtime.ChainHeight()
245	b.SubmissionCount++
246	b.RevisionFeedback = ""
247
248	chain.Emit(
249		"ProofSubmitted",
250		"id", strconv.FormatUint(id, 10),
251		"claimant", string(caller),
252		"proof_digest", cleanDigest,
253		"submission_count", strconv.FormatUint(b.SubmissionCount, 10),
254	)
255}
256
257// RequestRevision lets the sponsor send a submitted bounty back to Claimed with
258// actionable feedback and a refreshed deadline window.
259func RequestRevision(cur realm, id uint64, feedback string) {
260	b, ok := getBountyRecord(id)
261	if !ok {
262		panic("hearthbounty: bounty not found")
263	}
264	if b.Status != hearthcore.StatusSubmitted {
265		panic("hearthbounty: bounty has no pending submission")
266	}
267
268	caller := cur.Previous().Address()
269	if caller != b.Sponsor {
270		panic("hearthbounty: only the sponsor may request a revision")
271	}
272
273	cleanFeedback := hearthcore.ValidateText("feedback", feedback, 4, hearthcore.MaxNoteLen)
274	transitionStatus(b, hearthcore.StatusClaimed)
275	b.RevisionFeedback = cleanFeedback
276	b.DeadlineHeight = runtime.ChainHeight() + b.DeadlineBlocks
277
278	chain.Emit(
279		"RevisionRequested",
280		"id", strconv.FormatUint(id, 10),
281		"sponsor", string(caller),
282		"new_deadline_height", strconv.FormatInt(b.DeadlineHeight, 10),
283	)
284}
285
286// ApproveAndSettle releases the full ugnot escrow to the claimant, records a
287// 1-5 quality rating, and updates on-chain reputation using CEI ordering.
288func ApproveAndSettle(cur realm, id uint64, rating uint8, reviewNote string) {
289	b, ok := getBountyRecord(id)
290	if !ok {
291		panic("hearthbounty: bounty not found")
292	}
293	if b.Status != hearthcore.StatusSubmitted {
294		panic("hearthbounty: bounty is not awaiting approval")
295	}
296
297	caller := cur.Previous().Address()
298	if caller != b.Sponsor {
299		panic("hearthbounty: only the sponsor may approve and settle")
300	}
301
302	validRating := hearthcore.ValidateRating(rating)
303	cleanNote := hearthcore.ValidateText("reviewNote", reviewNote, 0, hearthcore.MaxNoteLen)
304
305	payout := b.EscrowUgnot
306	recipient := b.Claimant
307	height := runtime.ChainHeight()
308
309	// Effects before external bank interaction (CEI).
310	b.EscrowUgnot = 0
311	b.PayoutUgnot = payout
312	b.Rating = validRating
313	b.ReviewNote = cleanNote
314	b.SettledHeight = height
315	transitionStatus(b, hearthcore.StatusSettled)
316	totalEscrowUgnot -= payout
317	totalSettledUgnot += payout
318
319	hp := getOrCreateProfile(recipient, height)
320	hp.SettledCount++
321	hp.EarnedUgnot += payout
322	hp.RatingSum += uint64(validRating)
323	hp.RatingCount++
324
325	sp := getOrCreateProfile(b.Sponsor, height)
326	sp.SponsoredSettled++
327
328	// Interaction: send escrowed ugnot from realm address to claimant.
329	if payout > 0 {
330		bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
331		bnk.SendCoins(cur.Address(), recipient, chain.Coins{{Denom: hearthcore.Denom, Amount: payout}})
332	}
333
334	chain.Emit(
335		"BountySettled",
336		"id", strconv.FormatUint(id, 10),
337		"sponsor", string(b.Sponsor),
338		"claimant", string(recipient),
339		"payout_ugnot", strconv.FormatInt(payout, 10),
340		"rating", strconv.Itoa(int(validRating)),
341	)
342}
343
344// CancelBounty refunds escrowed ugnot to the sponsor. Only permitted while the
345// bounty is Open so sponsors cannot withdraw funds from an active claim.
346func CancelBounty(cur realm, id uint64) {
347	b, ok := getBountyRecord(id)
348	if !ok {
349		panic("hearthbounty: bounty not found")
350	}
351	if b.Status != hearthcore.StatusOpen {
352		panic("hearthbounty: only open bounties can be cancelled")
353	}
354
355	caller := cur.Previous().Address()
356	if caller != b.Sponsor {
357		panic("hearthbounty: only the sponsor may cancel")
358	}
359
360	refund := b.EscrowUgnot
361	b.EscrowUgnot = 0
362	b.RefundUgnot = refund
363	transitionStatus(b, hearthcore.StatusCancelled)
364	totalEscrowUgnot -= refund
365	totalRefundUgnot += refund
366
367	if refund > 0 {
368		bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
369		bnk.SendCoins(cur.Address(), b.Sponsor, chain.Coins{{Denom: hearthcore.Denom, Amount: refund}})
370	}
371
372	chain.Emit(
373		"BountyCancelled",
374		"id", strconv.FormatUint(id, 10),
375		"sponsor", string(b.Sponsor),
376		"refund_ugnot", strconv.FormatInt(refund, 10),
377	)
378}
379
380// OpenDispute escalates a submitted bounty to arbiter review. Either the
381// sponsor or the active claimant may open a dispute.
382func OpenDispute(cur realm, id uint64, reason string) {
383	b, ok := getBountyRecord(id)
384	if !ok {
385		panic("hearthbounty: bounty not found")
386	}
387	if b.Status != hearthcore.StatusSubmitted {
388		panic("hearthbounty: disputes can only be opened on submitted bounties")
389	}
390
391	caller := cur.Previous().Address()
392	if caller != b.Sponsor && caller != b.Claimant {
393		panic("hearthbounty: only sponsor or claimant may open a dispute")
394	}
395
396	cleanReason := hearthcore.ValidateText("reason", reason, 6, hearthcore.MaxNoteLen)
397	transitionStatus(b, hearthcore.StatusDisputed)
398	b.DisputeReason = cleanReason
399	b.DisputeOpenedBy = caller
400
401	chain.Emit(
402		"DisputeOpened",
403		"id", strconv.FormatUint(id, 10),
404		"opened_by", string(caller),
405		"reason", cleanReason,
406	)
407}
408
409// ResolveDispute settles a disputed bounty using deterministic basis-point math
410// (hunterBps in 0..10000). The remainder is refunded atomically to the sponsor.
411func ResolveDispute(cur realm, id uint64, hunterBps uint64, resolution string) {
412	caller := cur.Previous().Address()
413	if caller != arbiter {
414		panic("hearthbounty: only the arbiter may resolve disputes")
415	}
416
417	b, ok := getBountyRecord(id)
418	if !ok {
419		panic("hearthbounty: bounty not found")
420	}
421	if b.Status != hearthcore.StatusDisputed {
422		panic("hearthbounty: bounty is not disputed")
423	}
424
425	cleanResolution := hearthcore.ValidateText("resolution", resolution, 4, hearthcore.MaxNoteLen)
426	escrow := b.EscrowUgnot
427	hunterShare, sponsorShare := hearthcore.ComputeSplit(escrow, hunterBps)
428	height := runtime.ChainHeight()
429
430	// Effects before interactions (CEI).
431	b.EscrowUgnot = 0
432	b.PayoutUgnot = hunterShare
433	b.RefundUgnot = sponsorShare
434	b.ReviewNote = "[Arbiter Resolution] " + cleanResolution
435	b.SettledHeight = height
436	transitionStatus(b, hearthcore.StatusSettled)
437	totalEscrowUgnot -= escrow
438	totalSettledUgnot += hunterShare
439	totalRefundUgnot += sponsorShare
440
441	if hunterShare > 0 {
442		hp := getOrCreateProfile(b.Claimant, height)
443		hp.SettledCount++
444		hp.EarnedUgnot += hunterShare
445	}
446
447	bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur)
448	if hunterShare > 0 {
449		bnk.SendCoins(cur.Address(), b.Claimant, chain.Coins{{Denom: hearthcore.Denom, Amount: hunterShare}})
450	}
451	if sponsorShare > 0 {
452		bnk.SendCoins(cur.Address(), b.Sponsor, chain.Coins{{Denom: hearthcore.Denom, Amount: sponsorShare}})
453	}
454
455	chain.Emit(
456		"DisputeResolved",
457		"id", strconv.FormatUint(id, 10),
458		"arbiter", string(caller),
459		"hunter_bps", strconv.FormatUint(hunterBps, 10),
460		"hunter_ugnot", strconv.FormatInt(hunterShare, 10),
461		"sponsor_refund_ugnot", strconv.FormatInt(sponsorShare, 10),
462	)
463}
464
465// requireSingleUgnotSend validates that the caller attached exclusively ugnot
466// at or above minAmount so unsupported denominations cannot get trapped.
467func requireSingleUgnotSend(minAmount int64) int64 {
468	sent := unsafe.OriginSend()
469	if len(sent) == 0 {
470		panic("hearthbounty: must attach ugnot escrow coins")
471	}
472	if len(sent) != 1 || sent[0].Denom != hearthcore.Denom {
473		panic("hearthbounty: only ugnot coins are accepted")
474	}
475	if sent[0].Amount < minAmount {
476		panic("hearthbounty: attached ugnot is below minimum required amount")
477	}
478	return sent[0].Amount
479}
480
481func normalizeFilter(raw string) string {
482	return strings.ToLower(strings.TrimSpace(raw))
483}