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

4.52 Kb · 160 lines
  1// Package escrow is the pure escrow state machine behind Tavern Quests.
  2//
  3// It is a stateless library: it owns the quest record type and every rule
  4// for how a quest may move through its lifecycle, but it holds no chain
  5// state and moves no coins. The Tavern Quests realm (gno.land/r/nym-tavern777/quests)
  6// stores the records, verifies payments, and performs the actual coin
  7// transfers; every authorization and transition decision is delegated here
  8// so the rules can be unit-tested in isolation and reused by other realms.
  9//
 10// Lifecycle:
 11//
 12//	Open --Claim(claimer != poster)--> Claimed
 13//	Open --Cancel(poster)--> Cancelled            (escrow refunded by realm)
 14//	Claimed --Submit(claimant)--> Submitted
 15//	Submitted --Approve(poster)--> Completed      (escrow released by realm)
 16//	Submitted --Reject(poster)--> Claimed         (submission cleared, retry)
 17//
 18// Every transition validates first and mutates only on success, so a
 19// rejected transition never leaves partial state behind.
 20package escrow
 21
 22import (
 23	"errors"
 24	"strings"
 25)
 26
 27// Status is the lifecycle state of a quest.
 28type Status int
 29
 30const (
 31	Open Status = iota
 32	Claimed
 33	Submitted
 34	Completed
 35	Cancelled
 36)
 37
 38// String returns the lowercase board label for the status.
 39func (s Status) String() string {
 40	switch s {
 41	case Open:
 42		return "open"
 43	case Claimed:
 44		return "claimed"
 45	case Submitted:
 46		return "submitted"
 47	case Completed:
 48		return "completed"
 49	case Cancelled:
 50		return "cancelled"
 51	}
 52	return "unknown"
 53}
 54
 55// Quest is a single escrowed task on the board. Reward is denominated in
 56// ugnot and is held by the hosting realm between posting and payout/refund.
 57type Quest struct {
 58	ID          int64
 59	Poster      address
 60	Claimant    address
 61	Title       string
 62	Description string
 63	Reward      int64 // ugnot
 64	Status      Status
 65	Submission  string // claimant's delivery note / proof reference
 66}
 67
 68// ValidateNew checks the fields of a quest that is about to be posted.
 69// reward is the ugnot amount the poster actually sent, verified by the
 70// realm against the transaction before calling this.
 71func ValidateNew(title, description string, reward int64) error {
 72	if strings.TrimSpace(title) == "" {
 73		return errors.New("title must not be empty")
 74	}
 75	if len(title) > 140 {
 76		return errors.New("title too long (max 140 chars)")
 77	}
 78	if len(description) > 2048 {
 79		return errors.New("description too long (max 2048 chars)")
 80	}
 81	if reward <= 0 {
 82		return errors.New("reward must be a positive ugnot amount")
 83	}
 84	return nil
 85}
 86
 87// Claim moves an Open quest to Claimed for actor. The poster cannot claim
 88// their own quest, and a quest can have only one claimant.
 89func Claim(q *Quest, actor address) error {
 90	if q.Status != Open {
 91		return errors.New("quest is not open")
 92	}
 93	if actor == q.Poster {
 94		return errors.New("poster cannot claim their own quest")
 95	}
 96	q.Claimant = actor
 97	q.Status = Claimed
 98	return nil
 99}
100
101// Submit records the claimant's delivery note and moves a Claimed quest
102// to Submitted.
103func Submit(q *Quest, actor address, note string) error {
104	if q.Status != Claimed {
105		return errors.New("quest is not in claimed state")
106	}
107	if actor != q.Claimant {
108		return errors.New("only the claimant can submit work")
109	}
110	if strings.TrimSpace(note) == "" {
111		return errors.New("submission note must not be empty")
112	}
113	if len(note) > 1024 {
114		return errors.New("submission note too long (max 1024 chars)")
115	}
116	q.Submission = note
117	q.Status = Submitted
118	return nil
119}
120
121// Approve marks a Submitted quest Completed. The hosting realm releases
122// the escrowed reward to the claimant when this succeeds.
123func Approve(q *Quest, actor address) error {
124	if q.Status != Submitted {
125		return errors.New("quest is not in submitted state")
126	}
127	if actor != q.Poster {
128		return errors.New("only the poster can approve")
129	}
130	q.Status = Completed
131	return nil
132}
133
134// Reject sends a Submitted quest back to Claimed, clearing the delivery
135// note so the claimant can fix and resubmit.
136func Reject(q *Quest, actor address) error {
137	if q.Status != Submitted {
138		return errors.New("quest is not in submitted state")
139	}
140	if actor != q.Poster {
141		return errors.New("only the poster can reject")
142	}
143	q.Submission = ""
144	q.Status = Claimed
145	return nil
146}
147
148// Cancel closes an Open quest. The hosting realm refunds the escrowed
149// reward to the poster when this succeeds. A quest that has been claimed
150// can no longer be cancelled.
151func Cancel(q *Quest, actor address) error {
152	if q.Status != Open {
153		return errors.New("only an open quest can be cancelled")
154	}
155	if actor != q.Poster {
156		return errors.New("only the poster can cancel")
157	}
158	q.Status = Cancelled
159	return nil
160}