package quillbounty import ( "chain" "chain/banker" "chain/runtime" "chain/runtime/unsafe" "math/overflow" "strconv" "strings" "gno.land/p/nt/avl/v0" ) var ( bounties *avl.Tree // padID(id) -> *Bounty builders *avl.Tree // address.String() -> *BuilderProfile nextBountyID uint64 = 1 totalFunded int64 totalPaidOut int64 totalCompleted int ) func init() { bounties = avl.NewTree() builders = avl.NewTree() } // CreateBounty creates a new ugnot-escrowed bounty on Gno.land. // Caller must attach native ugnot via transaction send envelope. // Security: enforces runtime.AssertOriginCall() + cur.Previous().IsUserCall() // before inspecting unsafe.OriginSend() per Interrealm v2 payment guidelines. func CreateBounty( cur realm, title string, category string, spec string, reviewerAddr string, durationBlocks int64, ) uint64 { runtime.AssertOriginCall() prev := cur.Previous() if !prev.IsUserCall() { panic("CreateBounty: direct EOA call required") } title = strings.TrimSpace(title) spec = strings.TrimSpace(spec) if len(title) < 3 || len(title) > MaxTitleLen { panic("CreateBounty: title must be 3..120 characters") } if len(spec) < 10 || len(spec) > MaxSpecLen { panic("CreateBounty: spec must be 10..4000 characters") } if durationBlocks < MinDurationBlocks || durationBlocks > MaxDurationBlocks { panic("CreateBounty: durationBlocks out of allowed range") } sent := unsafe.OriginSend() if len(sent) != 1 || sent[0].Denom != DenomUgnot || sent[0].Amount <= 0 { panic("CreateBounty: must send positive ugnot only") } amount := sent[0].Amount sponsor := prev.Address() reviewer := sponsor reviewerAddr = strings.TrimSpace(reviewerAddr) if reviewerAddr != "" { rev := address(reviewerAddr) if !rev.IsValid() { panic("CreateBounty: invalid reviewer address") } reviewer = rev } newTotalFunded, ok := overflow.Add64(totalFunded, amount) if !ok { panic("CreateBounty: totalFunded overflow") } height := runtime.ChainHeight() id := nextBountyID nextBountyID++ b := &Bounty{ ID: id, Sponsor: sponsor, Reviewer: reviewer, Title: title, Category: normalizeCategory(category), Spec: spec, TotalFunded: amount, RemainingUgnot: amount, CreatedHeight: height, DeadlineHeight: height + durationBlocks, Status: StatusOpen, Submissions: make([]*Submission, 0), } bounties.Set(padID(id), b) totalFunded = newTotalFunded chain.Emit( "BountyCreated", "bounty_id", strconv.FormatUint(id, 10), "sponsor", sponsor.String(), "reviewer", reviewer.String(), "category", b.Category, "amount_ugnot", strconv.FormatInt(amount, 10), "deadline_height", strconv.FormatInt(b.DeadlineHeight, 10), ) return id } // TopUpBounty allows anyone to add additional ugnot funding to an open bounty. func TopUpBounty(cur realm, bountyID uint64) { runtime.AssertOriginCall() prev := cur.Previous() if !prev.IsUserCall() { panic("TopUpBounty: direct EOA call required") } b := mustGetBounty(bountyID) if b.Status != StatusOpen { panic("TopUpBounty: bounty is not open") } if runtime.ChainHeight() >= b.DeadlineHeight { panic("TopUpBounty: bounty deadline already passed") } sent := unsafe.OriginSend() if len(sent) != 1 || sent[0].Denom != DenomUgnot || sent[0].Amount <= 0 { panic("TopUpBounty: must send positive ugnot only") } amount := sent[0].Amount newRem, ok1 := overflow.Add64(b.RemainingUgnot, amount) newTot, ok2 := overflow.Add64(b.TotalFunded, amount) newGlobal, ok3 := overflow.Add64(totalFunded, amount) if !ok1 || !ok2 || !ok3 { panic("TopUpBounty: arithmetic overflow") } b.RemainingUgnot = newRem b.TotalFunded = newTot totalFunded = newGlobal chain.Emit( "BountyToppedUp", "bounty_id", strconv.FormatUint(bountyID, 10), "funder", prev.Address().String(), "amount_ugnot", strconv.FormatInt(amount, 10), "remaining_ugnot", strconv.FormatInt(b.RemainingUgnot, 10), ) } // SubmitWork submits verifiable proof-of-work (PR/repo/gnoweb link + commit hash) on-chain. func SubmitWork( cur realm, bountyID uint64, proofURL string, commitHash string, summary string, ) uint64 { b := mustGetBounty(bountyID) if b.Status != StatusOpen { panic("SubmitWork: bounty is not open") } if runtime.ChainHeight() >= b.DeadlineHeight { panic("SubmitWork: bounty deadline has passed") } builder := cur.Previous().Address() if builder == b.Sponsor { panic("SubmitWork: sponsor cannot submit to own bounty") } proofURL = strings.TrimSpace(proofURL) commitHash = strings.TrimSpace(commitHash) summary = strings.TrimSpace(summary) if len(proofURL) < 8 || len(proofURL) > MaxURLLen || (!strings.HasPrefix(proofURL, "https://") && !strings.HasPrefix(proofURL, "gno.land/")) { panic("SubmitWork: proofURL must start with https:// or gno.land/") } if len(summary) < 5 || len(summary) > MaxSummaryLen { panic("SubmitWork: summary must be 5..1000 characters") } subID := uint64(len(b.Submissions) + 1) sub := &Submission{ ID: subID, BountyID: bountyID, Builder: builder, ProofURL: proofURL, CommitHash: commitHash, Summary: summary, Status: SubPending, SubmittedAt: runtime.ChainHeight(), } b.Submissions = append(b.Submissions, sub) chain.Emit( "WorkSubmitted", "bounty_id", strconv.FormatUint(bountyID, 10), "submission_id", strconv.FormatUint(subID, 10), "builder", builder.String(), "proof_url", proofURL, ) return subID } // RequestRevision records an on-chain review note asking the builder to refine their submission. func RequestRevision(cur realm, bountyID uint64, submissionID uint64, feedback string) { b := mustGetBounty(bountyID) caller := cur.Previous().Address() if caller != b.Sponsor && caller != b.Reviewer { panic("RequestRevision: unauthorized reviewer") } if b.Status != StatusOpen { panic("RequestRevision: bounty is not open") } sub := mustGetSubmission(b, submissionID) if sub.Status == SubApproved { panic("RequestRevision: submission already approved") } feedback = strings.TrimSpace(feedback) if len(feedback) < 3 || len(feedback) > MaxSummaryLen { panic("RequestRevision: feedback must be 3..1000 characters") } sub.Status = SubNeedsRevision sub.ReviewNote = feedback sub.ReviewedAt = runtime.ChainHeight() chain.Emit( "RevisionRequested", "bounty_id", strconv.FormatUint(bountyID, 10), "submission_id", strconv.FormatUint(submissionID, 10), "reviewer", caller.String(), ) } // ApproveSubmission approves a submission and atomically releases escrowed ugnot. // Pass payoutUgnot == 0 to release the entire remaining bounty balance (100% single-winner payout), // or pass 0 < payoutUgnot <= RemainingUgnot for multi-winner split payouts. func ApproveSubmission( cur realm, bountyID uint64, submissionID uint64, payoutUgnot int64, reviewNote string, ) { b := mustGetBounty(bountyID) caller := cur.Previous().Address() if caller != b.Sponsor && caller != b.Reviewer { panic("ApproveSubmission: unauthorized reviewer") } if b.Status != StatusOpen { panic("ApproveSubmission: bounty is not open") } if b.RemainingUgnot <= 0 { panic("ApproveSubmission: no remaining escrow") } sub := mustGetSubmission(b, submissionID) if sub.Status == SubApproved { panic("ApproveSubmission: submission already approved") } payout := payoutUgnot if payout == 0 { payout = b.RemainingUgnot } if payout <= 0 || payout > b.RemainingUgnot { panic("ApproveSubmission: invalid payout amount") } newGlobalPaid, ok := overflow.Add64(totalPaidOut, payout) if !ok { panic("ApproveSubmission: totalPaidOut overflow") } // Effects first (Checks-Effects-Interactions) sub.Status = SubApproved sub.PayoutUgnot = payout sub.ReviewNote = strings.TrimSpace(reviewNote) sub.ReviewedAt = runtime.ChainHeight() b.RemainingUgnot -= payout if b.RemainingUgnot == 0 { b.Status = StatusCompleted totalCompleted++ } totalPaidOut = newGlobalPaid recordBuilderReputation(sub.Builder, payout, bountyID) // Interaction last: release escrowed ugnot from realm vault to winning builder bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur) bnk.SendCoins(cur.Address(), sub.Builder, chain.NewCoins(chain.NewCoin(DenomUgnot, payout))) chain.Emit( "SubmissionApproved", "bounty_id", strconv.FormatUint(bountyID, 10), "submission_id", strconv.FormatUint(submissionID, 10), "builder", sub.Builder.String(), "payout_ugnot", strconv.FormatInt(payout, 10), "remaining_ugnot", strconv.FormatInt(b.RemainingUgnot, 10), ) } // RefundExpired returns remaining escrowed ugnot to the sponsor after deadlineHeight passes. func RefundExpired(cur realm, bountyID uint64) int64 { b := mustGetBounty(bountyID) caller := cur.Previous().Address() if caller != b.Sponsor { panic("RefundExpired: only sponsor can reclaim expired funds") } if b.Status != StatusOpen { panic("RefundExpired: bounty is not open") } if runtime.ChainHeight() < b.DeadlineHeight { panic("RefundExpired: deadline height not reached yet") } if b.RemainingUgnot <= 0 { panic("RefundExpired: nothing to refund") } refundAmt := b.RemainingUgnot b.RemainingUgnot = 0 b.Status = StatusExpired bnk := banker.NewBanker(banker.BankerTypeRealmSend, cur) bnk.SendCoins(cur.Address(), b.Sponsor, chain.NewCoins(chain.NewCoin(DenomUgnot, refundAmt))) chain.Emit( "BountyRefunded", "bounty_id", strconv.FormatUint(bountyID, 10), "sponsor", b.Sponsor.String(), "refund_ugnot", strconv.FormatInt(refundAmt, 10), ) return refundAmt } // GetBounty returns a read-only copy of a bounty for cross-realm composability. func GetBounty(bountyID uint64) *Bounty { raw := bounties.Get(padID(bountyID)) if raw == nil { return nil } b := raw.(*Bounty) cp := *b cp.Submissions = make([]*Submission, len(b.Submissions)) for i, s := range b.Submissions { sc := *s cp.Submissions[i] = &sc } return &cp } // GetBuilderProfile returns on-chain reputation stats for a builder address. func GetBuilderProfile(addr address) *BuilderProfile { raw := builders.Get(addr.String()) if raw == nil { return nil } p := raw.(*BuilderProfile) cp := *p return &cp } // Stats returns aggregate protocol metrics (totalBounties, totalCompleted, totalFundedUgnot, totalPaidOutUgnot). func Stats() (int, int, int64, int64) { return bounties.Size(), totalCompleted, totalFunded, totalPaidOut } func mustGetBounty(bountyID uint64) *Bounty { raw := bounties.Get(padID(bountyID)) if raw == nil { panic("bounty not found") } return raw.(*Bounty) } func mustGetSubmission(b *Bounty, submissionID uint64) *Submission { if submissionID == 0 || int(submissionID) > len(b.Submissions) { panic("submission not found") } return b.Submissions[submissionID-1] } func recordBuilderReputation(addr address, payout int64, bountyID uint64) { key := addr.String() var prof *BuilderProfile if raw := builders.Get(key); raw != nil { prof = raw.(*BuilderProfile) } else { prof = &BuilderProfile{Address: addr} builders.Set(key, prof) } prof.CompletedCount++ newEarned, ok := overflow.Add64(prof.TotalEarnedUgnot, payout) if !ok { panic("builder earned overflow") } prof.TotalEarnedUgnot = newEarned prof.LastBountyID = bountyID }