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 source pure

Package escrow holds the rules of a single-builder bounty escrow: the Quest type, its states and every legal move bet...

Overview

Package escrow holds the rules of a single-builder bounty escrow: the Quest type, its states and every legal move between them.

The package is pure. It holds no coins, no banker and no storage. A realm that imports it keeps the *Quest values, moves the coins, and asks this package whether a move is allowed. Every move takes the acting address and the current block height as arguments, so the rules can be tested without a chain and cannot be bypassed by the caller's own logic.

Moves that can release money (Cancel, Unclaim, Release, Approve, Reject, ClaimPayout) return a Payout telling the realm exactly whom to pay and how much; a zero Payout (Due() == false) means no coins move. A non-zero Payout is only ever returned together with a move into a final state (Paid or Cancelled), and a final quest accepts no further move, so a quest can produce at most one non-zero Payout.

A realm using this package must:

  • lock exactly Reward() in its own balance before calling NewQuest,
  • apply the move first and send the coins of the returned Payout after,
  • treat any returned error as fatal (panic) so the transaction reverts.

States and moves:

Example
 1(none)    Post (NewQuest)  anyone                        -> Open
 2Open      Claim            anyone except the poster      -> Claimed
 3Open      Cancel           poster                        -> Cancelled  refund Reward to poster
 4Claimed   Cancel ("close") poster                        -> Claimed, closing
 5Submitted Cancel ("close") poster                        -> Submitted, closing
 6Claimed   Submit(link)     builder, until work deadline  -> Submitted
 7Claimed   Unclaim          builder                       -> Open (closing: Cancelled, refund)
 8Claimed   Release          anyone, after work deadline   -> Open (closing: Cancelled, refund)
 9Submitted Approve          poster                        -> Paid       pay Reward to builder
10Submitted Reject(reason)   poster, until review deadline -> Open (closing: Cancelled, refund)
11Submitted ClaimPayout      builder, after review deadline-> Paid       pay Reward to builder

Closing: a poster cannot take the reward back while a builder holds the quest, but can close it. The current builder keeps every right (submit, get approved, claim the timeout payout); any move that would put the quest back to Open ends it instead and refunds the poster. So a builder can hold a closing quest for at most one work window, and nobody can re-claim it. Closing cannot be undone.

Deadlines are block heights. "Until" a deadline includes the deadline block itself; "after" a deadline starts at the next block.

Constants 3

const MaxTitleLen, MaxDetailsLen, MaxLinkLen, MaxReasonLen, MinWorkBlocks, MaxWorkBlocks, ReviewBlocks

 1const (
 2	MaxTitleLen   = 80
 3	MaxDetailsLen = 1000
 4	MaxLinkLen    = 300
 5	MaxReasonLen  = 300
 6
 7	// MinWorkBlocks and MaxWorkBlocks bound the work window a poster may set:
 8	// about 1 hour and about 90 days.
 9	MinWorkBlocks int64 = 1_000
10	MaxWorkBlocks int64 = 2_200_000
11
12	// ReviewBlocks is the fixed review window every quest gets after Submit,
13	// about 7 days. If the poster neither approves nor rejects within it, the
14	// builder may call ClaimPayout.
15	ReviewBlocks int64 = 172_800
16)
source

Limits. Text limits count Unicode characters (runes).

const PayBuilder, RefundPoster

1const (
2	// PayBuilder: send the reward to the builder (Approve, ClaimPayout).
3	PayBuilder PayoutKind = iota + 1
4	// RefundPoster: send the reward back to the poster (Cancel).
5	RefundPoster
6)
source

const Open, Claimed, Submitted, Paid, Cancelled

 1const (
 2	// Open: waiting for a builder.
 3	Open State = iota + 1
 4	// Claimed: a builder is working on it.
 5	Claimed
 6	// Submitted: work link sent, waiting for the poster's review.
 7	Submitted
 8	// Paid: reward paid to the builder. Final.
 9	Paid
10	// Cancelled: reward refunded to the poster. Final.
11	Cancelled
12)
source

Variables 1

var ErrInvalidPoster, ErrInvalidActor, ErrInvalidReward, ErrInvalidHeight, ErrTitleEmpty, ErrTitleTooLong, ErrTitleControlChar, ErrDetailsTooLong, ErrWorkBlocksTooLow, ErrWorkBlocksTooHigh, ErrLinkEmpty, ErrLinkTooLong, ErrLinkNotHTTPS, ErrLinkInvalid, ErrReasonEmpty, ErrReasonTooLong, ErrInvalidText, ErrWrongState, ErrNotPoster, ErrNotBuilder, ErrPosterCannotClaim, ErrWorkDeadlinePassed, ErrWorkDeadlineNotPassed, ErrReviewDeadlinePassed, ErrReviewDeadlineNotPassed, ErrAlreadyClosing

 1var (
 2	ErrInvalidPoster     = errors.New("escrow: invalid poster address")
 3	ErrInvalidActor      = errors.New("escrow: invalid caller address")
 4	ErrInvalidReward     = errors.New("escrow: reward must be positive")
 5	ErrInvalidHeight     = errors.New("escrow: invalid block height")
 6	ErrTitleEmpty        = errors.New("escrow: title is empty")
 7	ErrTitleTooLong      = errors.New("escrow: title is too long")
 8	ErrTitleControlChar  = errors.New("escrow: title must not contain control characters such as newlines or tabs")
 9	ErrDetailsTooLong    = errors.New("escrow: details are too long")
10	ErrWorkBlocksTooLow  = errors.New("escrow: work window is below the minimum")
11	ErrWorkBlocksTooHigh = errors.New("escrow: work window is above the maximum")
12	ErrLinkEmpty         = errors.New("escrow: work link is empty")
13	ErrLinkTooLong       = errors.New("escrow: work link is too long")
14	ErrLinkNotHTTPS      = errors.New("escrow: work link must start with https://")
15	ErrLinkInvalid       = errors.New("escrow: work link contains spaces or control characters")
16	ErrReasonEmpty       = errors.New("escrow: reject reason is empty")
17	ErrReasonTooLong     = errors.New("escrow: reject reason is too long")
18	ErrInvalidText       = errors.New("escrow: text is not valid UTF-8")
19
20	ErrWrongState              = errors.New("escrow: move not allowed in the quest's current state")
21	ErrNotPoster               = errors.New("escrow: only the poster can do this")
22	ErrNotBuilder              = errors.New("escrow: only the builder can do this")
23	ErrPosterCannotClaim       = errors.New("escrow: the poster cannot claim their own quest")
24	ErrWorkDeadlinePassed      = errors.New("escrow: work deadline has passed")
25	ErrWorkDeadlineNotPassed   = errors.New("escrow: work deadline has not passed yet")
26	ErrReviewDeadlinePassed    = errors.New("escrow: review deadline has passed")
27	ErrReviewDeadlineNotPassed = errors.New("escrow: review deadline has not passed yet")
28	ErrAlreadyClosing          = errors.New("escrow: the quest is already closing")
29)
source

Errors returned by the moves. Realms usually panic with err.Error().

Functions 6

func ValidateDetails

1func ValidateDetails(s string) error
source

ValidateDetails checks quest details: at most MaxDetailsLen characters (may be empty).

func ValidateReason

1func ValidateReason(s string) error
source

ValidateReason checks a reject reason: not blank, at most MaxReasonLen characters.

func ValidateTitle

1func ValidateTitle(s string) error
source

ValidateTitle checks a quest title: not blank, at most MaxTitleLen characters, and no control characters (newline, tab, etc.) or Unicode line and paragraph separators, because titles are shown inside Markdown tables and links.

func ValidateWorkBlocks

1func ValidateWorkBlocks(w int64) error
source

ValidateWorkBlocks checks that w is within [MinWorkBlocks, MaxWorkBlocks].

func NewQuest

1func NewQuest(id uint64, poster address, title, details string, reward, workBlocks, height int64) (*Quest, error)
source

NewQuest validates a new quest and returns it in the Open state. The caller must already hold reward (in its own coin) for this quest. id is chosen by the caller and is not checked for uniqueness here.

Types 4

type Payout

struct
1type Payout struct {
2	Kind   PayoutKind
3	To     address
4	Amount int64
5}
source

Payout is the transfer a realm must make after a successful move. The zero Payout means no transfer. When Due, Amount is always the quest's stored Reward.

Methods on Payout

func Due

method on Payout
1func (p Payout) Due() bool
source

Due reports whether coins must move.

type PayoutKind

ident
1type PayoutKind uint8
source

PayoutKind says why coins must move.

Methods on PayoutKind

func String

method on PayoutKind
1func (k PayoutKind) String() string
source

String returns the payout kind name.

type Quest

struct
 1type Quest struct {
 2	id          uint64
 3	poster      address
 4	title       string
 5	details     string
 6	reward      int64
 7	state       State
 8	builder     address
 9	workLink    string
10	workBlocks  int64
11	createdAt   int64
12	claimedAt   int64
13	submittedAt int64
14
15	rejectCount      int
16	lastRejectReason string
17
18	closing bool // set by Cancel on a Claimed or Submitted quest
19}
source

Quest is one bounty. Its fields are private so that only the moves below can change it; read them with the getters.

Methods on Quest

func Approve

method on Quest
1func (q *Quest) Approve(actor address, height int64) (Payout, error)
source

Approve lets the poster accept the submitted work. The returned Payout pays the reward to the builder. Approving after the review deadline is allowed: it pays the same builder as ClaimPayout would.

func Builder

method on Quest
1func (q *Quest) Builder() address
source

func CanClaimPayout

method on Quest
1func (q *Quest) CanClaimPayout(height int64) bool
source

CanClaimPayout reports whether the builder could call ClaimPayout at height.

func CanRelease

method on Quest
1func (q *Quest) CanRelease(height int64) bool
source

CanRelease reports whether Release would succeed at height.

func Cancel

method on Quest
1func (q *Quest) Cancel(actor address, height int64) (Payout, error)
source

Cancel is the poster's way out.

  • On an Open quest it ends the quest at once: Cancelled, and the returned Payout refunds the reward to the poster.
  • On a Claimed or Submitted quest it only marks the quest as closing and returns a zero Payout: the current builder keeps every right, and the reward goes back to the poster when the builder's turn ends without approval (Unclaim, Release or Reject). A second call fails.

func Claim

method on Quest
1func (q *Quest) Claim(actor address, height int64) error
source

Claim lets anyone except the poster take an Open quest. The work window starts at height.

func ClaimPayout

method on Quest
1func (q *Quest) ClaimPayout(actor address, height int64) (Payout, error)
source

ClaimPayout lets the builder take the reward when the poster has not answered within the review window (from ReviewDeadline()+1 on). The returned Payout pays the reward to the builder.

func Closing

method on Quest
1func (q *Quest) Closing() bool
source

Closing reports whether the poster has closed the quest while a builder held it: the reward returns to the poster when the builder's turn ends without approval.

func Details

method on Quest
1func (q *Quest) Details() string
source

func HoldsFunds

method on Quest
1func (q *Quest) HoldsFunds() bool
source

HoldsFunds reports whether the quest's reward is still held in escrow (Open, Claimed or Submitted). The sum of Reward() over quests that hold funds must equal what the realm holds for them.

func ID

method on Quest
1func (q *Quest) ID() uint64
source

func Poster

method on Quest
1func (q *Quest) Poster() address
source

func Reject

method on Quest
1func (q *Quest) Reject(actor address, reason string, height int64) (Payout, error)
source

Reject lets the poster refuse the submitted work, up to and including the review deadline block. The builder is cleared, and the reject count and last reason are kept on the quest for everyone to see. The quest becomes Open again, or, if it is closing, Cancelled with a refund to the poster.

func Release

method on Quest
1func (q *Quest) Release(actor address, height int64) (Payout, error)
source

Release lets anyone end the builder's turn on a Claimed quest once the work deadline has passed (from WorkDeadline()+1 on) without a submission. The quest becomes Open again, or, if it is closing, Cancelled with a refund to the poster.

func ReviewDeadline

method on Quest
1func (q *Quest) ReviewDeadline() int64
source

ReviewDeadline is the last block at which the poster may reject. It is 0 unless the quest is Submitted.

func Reward

method on Quest
1func (q *Quest) Reward() int64
source

func State

method on Quest
1func (q *Quest) State() State
source

func Submit

method on Quest
1func (q *Quest) Submit(actor address, link string, height int64) error
source

Submit lets the builder hand in a work link, up to and including the work deadline block. The review window starts at height.

func Title

method on Quest
1func (q *Quest) Title() string
source

func Unclaim

method on Quest
1func (q *Quest) Unclaim(actor address, height int64) (Payout, error)
source

Unclaim lets the builder give a Claimed quest back. It becomes Open again, or, if it is closing, Cancelled with a refund to the poster.

func WorkDeadline

method on Quest
1func (q *Quest) WorkDeadline() int64
source

WorkDeadline is the last block at which the builder may submit. It is 0 unless the quest is Claimed or Submitted.

type State

ident
1type State uint8
source

State is the stage a quest is in.

Methods on State

func IsFinal

method on State
1func (s State) IsFinal() bool
source

IsFinal reports whether no further move is possible from s.

func String

method on State
1func (s State) String() string
source

String returns the state name.

Dependencies

Imports 4

/p/g1llc3lq3zmjtjdk29dk52munrzxt5ne746cl7r8/escrow

Imported by indexer

Source Files 4