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.

Playground

escrow.gno

// Package escrow is the pure escrow state machine behind Tavern Quests.
//
// It is a stateless library: it owns the quest record type and every rule
// for how a quest may move through its lifecycle, but it holds no chain
// state and moves no coins. The Tavern Quests realm (gno.land/r/nym-tavern777/quests)
// stores the records, verifies payments, and performs the actual coin
// transfers; every authorization and transition decision is delegated here
// so the rules can be unit-tested in isolation and reused by other realms.
//
// Lifecycle:
//
//	Open --Claim(claimer != poster)--> Claimed
//	Open --Cancel(poster)--> Cancelled            (escrow refunded by realm)
//	Claimed --Submit(claimant)--> Submitted
//	Submitted --Approve(poster)--> Completed      (escrow released by realm)
//	Submitted --Reject(poster)--> Claimed         (submission cleared, retry)
//
// Every transition validates first and mutates only on success, so a
// rejected transition never leaves partial state behind.
package escrow

import (
	"errors"
	"strings"
)

// Status is the lifecycle state of a quest.
type Status int

const (
	Open Status = iota
	Claimed
	Submitted
	Completed
	Cancelled
)

// String returns the lowercase board label for the status.
func (s Status) String() string {
	switch s {
	case Open:
		return "open"
	case Claimed:
		return "claimed"
	case Submitted:
		return "submitted"
	case Completed:
		return "completed"
	case Cancelled:
		return "cancelled"
	}
	return "unknown"
}

// Quest is a single escrowed task on the board. Reward is denominated in
// ugnot and is held by the hosting realm between posting and payout/refund.
type Quest struct {
	ID          int64
	Poster      address
	Claimant    address
	Title       string
	Description string
	Reward      int64 // ugnot
	Status      Status
	Submission  string // claimant's delivery note / proof reference
}

// ValidateNew checks the fields of a quest that is about to be posted.
// reward is the ugnot amount the poster actually sent, verified by the
// realm against the transaction before calling this.
func ValidateNew(title, description string, reward int64) error {
	if strings.TrimSpace(title) == "" {
		return errors.New("title must not be empty")
	}
	if len(title) > 140 {
		return errors.New("title too long (max 140 chars)")
	}
	if len(description) > 2048 {
		return errors.New("description too long (max 2048 chars)")
	}
	if reward <= 0 {
		return errors.New("reward must be a positive ugnot amount")
	}
	return nil
}

// Claim moves an Open quest to Claimed for actor. The poster cannot claim
// their own quest, and a quest can have only one claimant.
func Claim(q *Quest, actor address) error {
	if q.Status != Open {
		return errors.New("quest is not open")
	}
	if actor == q.Poster {
		return errors.New("poster cannot claim their own quest")
	}
	q.Claimant = actor
	q.Status = Claimed
	return nil
}

// Submit records the claimant's delivery note and moves a Claimed quest
// to Submitted.
func Submit(q *Quest, actor address, note string) error {
	if q.Status != Claimed {
		return errors.New("quest is not in claimed state")
	}
	if actor != q.Claimant {
		return errors.New("only the claimant can submit work")
	}
	if strings.TrimSpace(note) == "" {
		return errors.New("submission note must not be empty")
	}
	if len(note) > 1024 {
		return errors.New("submission note too long (max 1024 chars)")
	}
	q.Submission = note
	q.Status = Submitted
	return nil
}

// Approve marks a Submitted quest Completed. The hosting realm releases
// the escrowed reward to the claimant when this succeeds.
func Approve(q *Quest, actor address) error {
	if q.Status != Submitted {
		return errors.New("quest is not in submitted state")
	}
	if actor != q.Poster {
		return errors.New("only the poster can approve")
	}
	q.Status = Completed
	return nil
}

// Reject sends a Submitted quest back to Claimed, clearing the delivery
// note so the claimant can fix and resubmit.
func Reject(q *Quest, actor address) error {
	if q.Status != Submitted {
		return errors.New("quest is not in submitted state")
	}
	if actor != q.Poster {
		return errors.New("only the poster can reject")
	}
	q.Submission = ""
	q.Status = Claimed
	return nil
}

// Cancel closes an Open quest. The hosting realm refunds the escrowed
// reward to the poster when this succeeds. A quest that has been claimed
// can no longer be cancelled.
func Cancel(q *Quest, actor address) error {
	if q.Status != Open {
		return errors.New("only an open quest can be cancelled")
	}
	if actor != q.Poster {
		return errors.New("only the poster can cancel")
	}
	q.Status = Cancelled
	return nil
}

escrow_test.gno

package escrow

import (
	"strings"
	"testing"
)

const (
	posterAddr   = address("g1wdjkuer9wgc47h6lta047h6lta047h6l56jtjc")
	claimerAddr  = address("g1wfjkx6tsd9jkuap3ta047h6lta047h6lkk20gv")
	strangerAddr = address("g1wfjkx6tsd9jkuapjta047h6lta047h6lducc3v")
)

func newQuest() *Quest {
	return &Quest{
		ID:          1,
		Poster:      posterAddr,
		Title:       "Write the tavern chronicle",
		Description: "A short history of the tavern, in markdown.",
		Reward:      1000,
		Status:      Open,
	}
}

func mustOK(t *testing.T, err error) {
	t.Helper()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}
}

func mustErr(t *testing.T, err error, want string) {
	t.Helper()
	if err == nil {
		t.Fatalf("expected error containing %q, got nil", want)
	}
	if !strings.Contains(err.Error(), want) {
		t.Fatalf("expected error containing %q, got %q", want, err.Error())
	}
}

func TestValidateNew(t *testing.T) {
	mustOK(t, ValidateNew("A quest", "details", 1))
	mustErr(t, ValidateNew("", "details", 1), "title must not be empty")
	mustErr(t, ValidateNew("   ", "details", 1), "title must not be empty")
	mustErr(t, ValidateNew("A quest", "details", 0), "reward must be a positive")
	mustErr(t, ValidateNew("A quest", "details", -5), "reward must be a positive")
}

func TestHappyPath(t *testing.T) {
	q := newQuest()

	mustOK(t, Claim(q, claimerAddr))
	if q.Status != Claimed || q.Claimant != claimerAddr {
		t.Fatalf("after claim: status=%v claimant=%v", q.Status, q.Claimant)
	}

	mustOK(t, Submit(q, claimerAddr, "delivered: ipfs://chronicle-v1"))
	if q.Status != Submitted || q.Submission == "" {
		t.Fatalf("after submit: status=%v submission=%q", q.Status, q.Submission)
	}

	mustOK(t, Approve(q, posterAddr))
	if q.Status != Completed {
		t.Fatalf("after approve: status=%v", q.Status)
	}
}

func TestRejectReopensForResubmit(t *testing.T) {
	q := newQuest()
	mustOK(t, Claim(q, claimerAddr))
	mustOK(t, Submit(q, claimerAddr, "first draft"))

	mustOK(t, Reject(q, posterAddr))
	if q.Status != Claimed {
		t.Fatalf("after reject: status=%v, want claimed", q.Status)
	}
	if q.Submission != "" {
		t.Fatalf("after reject: submission not cleared: %q", q.Submission)
	}

	mustOK(t, Submit(q, claimerAddr, "second draft"))
	mustOK(t, Approve(q, posterAddr))
	if q.Status != Completed {
		t.Fatalf("final status=%v, want completed", q.Status)
	}
}

func TestCancelOpen(t *testing.T) {
	q := newQuest()
	mustOK(t, Cancel(q, posterAddr))
	if q.Status != Cancelled {
		t.Fatalf("after cancel: status=%v", q.Status)
	}
}

func TestDoubleClaimRejected(t *testing.T) {
	q := newQuest()
	mustOK(t, Claim(q, claimerAddr))
	mustErr(t, Claim(q, strangerAddr), "quest is not open")
	if q.Claimant != claimerAddr {
		t.Fatalf("claimant changed after double claim: %v", q.Claimant)
	}
}

func TestPosterCannotClaimOwn(t *testing.T) {
	q := newQuest()
	mustErr(t, Claim(q, posterAddr), "poster cannot claim their own quest")
	if q.Status != Open {
		t.Fatalf("status changed after self-claim attempt: %v", q.Status)
	}
}

func TestWrongApproverRejected(t *testing.T) {
	q := newQuest()
	mustOK(t, Claim(q, claimerAddr))
	mustOK(t, Submit(q, claimerAddr, "done"))
	mustErr(t, Approve(q, claimerAddr), "only the poster can approve")
	mustErr(t, Approve(q, strangerAddr), "only the poster can approve")
	mustErr(t, Reject(q, claimerAddr), "only the poster can reject")
	if q.Status != Submitted {
		t.Fatalf("status changed after wrong-approver attempts: %v", q.Status)
	}
}

func TestCancelAfterClaimRejected(t *testing.T) {
	q := newQuest()
	mustOK(t, Claim(q, claimerAddr))
	mustErr(t, Cancel(q, posterAddr), "only an open quest can be cancelled")
	if q.Status != Claimed {
		t.Fatalf("status changed after cancel-after-claim: %v", q.Status)
	}
}

func TestSubmitRules(t *testing.T) {
	q := newQuest()
	mustErr(t, Submit(q, claimerAddr, "too early"), "quest is not in claimed state")
	mustOK(t, Claim(q, claimerAddr))
	mustErr(t, Submit(q, strangerAddr, "not mine"), "only the claimant can submit work")
	mustErr(t, Submit(q, claimerAddr, "  "), "submission note must not be empty")
}

func TestTerminalStatesAreFinal(t *testing.T) {
	q := newQuest()
	mustOK(t, Cancel(q, posterAddr))
	mustErr(t, Claim(q, claimerAddr), "quest is not open")
	mustErr(t, Cancel(q, posterAddr), "only an open quest can be cancelled")

	q2 := newQuest()
	mustOK(t, Claim(q2, claimerAddr))
	mustOK(t, Submit(q2, claimerAddr, "done"))
	mustOK(t, Approve(q2, posterAddr))
	mustErr(t, Approve(q2, posterAddr), "quest is not in submitted state")
	mustErr(t, Cancel(q2, posterAddr), "only an open quest can be cancelled")
}

gnomod.toml

module = "gno.land/p/nym-tavern777/escrow"
gno = "0.9"

[addpkg]
  creator = "g19nem0u6uhzsqncyr9ql8e2qmasu6rq405cy6ea"
  height = 1353495
  max_deposit = "100000000ugnot"