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.

state.gno

3.84 Kb · 151 lines
  1// Package cust0dy is the Cust0dy marketplace realm: a peer-to-peer
  2// order book and automated escrow engine for Web2 domains, social handles
  3// and Gno handles. Coins lock in this realm at purchase time and move only
  4// on settlement, refund, or arbiter resolution.
  5//
  6// Honesty note: the assets themselves move off-chain (a registrar push, a
  7// password handover). The realm holds the payment, records verification
  8// attestations, and gives both sides a referee (the arbiter) when they
  9// disagree. It cannot force the off-chain transfer itself.
 10package cust0dy
 11
 12import (
 13	"strconv"
 14	"time"
 15
 16	"chain"
 17
 18	core "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cust0dy_core"
 19	"gno.land/p/nt/avl/v0"
 20)
 21
 22// RefundWindow is how long a buyer can pull an automatic refund while the
 23// seller has not submitted any transfer. After the seller submits, the exit
 24// paths are buyer confirmation or a dispute.
 25const RefundWindow = 24 * time.Hour
 26
 27// Listing is one sell-side order.
 28type Listing struct {
 29	ID         int
 30	Seller     address
 31	Kind       core.AssetKind
 32	Value      string
 33	PriceUgnot int64
 34	State      core.ListingState
 35	Verified   bool
 36	Created    time.Time
 37}
 38
 39// Offer is one buy-side order. The offered amount is locked at creation.
 40type Offer struct {
 41	ID           int
 42	ListingID    int
 43	Maker        address
 44	AmountUgnot  int64
 45	Open         bool
 46	Created      time.Time
 47}
 48
 49// Escrow is one live trade.
 50type Escrow struct {
 51	ID            int
 52	ListingID     int
 53	Seller        address
 54	Buyer         address
 55	PriceUgnot    int64 // agreed price, before fees
 56	LockedUgnot   int64 // total coins locked by the buyer
 57	State         core.State
 58	FundedAt      time.Time
 59	DisputeReason string
 60}
 61
 62var (
 63	listings   avl.Tree // strconv id -> *Listing
 64	offers     avl.Tree // strconv id -> *Offer
 65	escrows    avl.Tree // strconv id -> *Escrow
 66	listingSeq int
 67	offerSeq   int
 68	escrowSeq  int
 69
 70	admin   address // marketplace operator; claims once, see ClaimAdmin
 71	arbiter address // dispute referee; set by admin
 72	feesUgnot int64 // total platform fees accrued (coins sit in the realm balance)
 73)
 74
 75func mustUserCall(cur realm) {
 76	if !cur.Previous().IsUserCall() {
 77		panic("direct wallet calls only")
 78	}
 79}
 80
 81func mustAdmin(cur realm) {
 82	mustUserCall(cur)
 83	if cur.Previous().Address() != admin {
 84		panic("admin only")
 85	}
 86}
 87
 88func mustArbiter(cur realm) {
 89	mustUserCall(cur)
 90	a := cur.Previous().Address()
 91	if a != arbiter && a != admin {
 92		panic("arbiter only")
 93	}
 94}
 95
 96func getListing(id int) *Listing {
 97	v := listings.Get(strconv.Itoa(id))
 98	if v == nil {
 99		panic("no such listing")
100	}
101	return v.(*Listing)
102}
103
104func getOffer(id int) *Offer {
105	v := offers.Get(strconv.Itoa(id))
106	if v == nil {
107		panic("no such offer")
108	}
109	return v.(*Offer)
110}
111
112func getEscrow(id int) *Escrow {
113	v := escrows.Get(strconv.Itoa(id))
114	if v == nil {
115		panic("no such escrow")
116	}
117	return v.(*Escrow)
118}
119
120// send moves coins out of the realm balance. Only callable after the state
121// transition has already been recorded by the caller.
122func send(cur realm, to address, ugnot int64) {
123	if ugnot <= 0 {
124		return
125	}
126	b := bankerNew(cur)
127	b.SendCoins(cur.Address(), to, chain.Coins{{Denom: "ugnot", Amount: ugnot}})
128}
129
130// ---- read helpers used by Render and qeval ----
131
132// ListingCount returns the number of listings ever created.
133func ListingCount() int { return listingSeq }
134
135// EscrowCount returns the number of escrows ever created.
136func EscrowCount() int { return escrowSeq }
137
138// GetListing returns one listing by id.
139func GetListing(id int) Listing { return *getListing(id) }
140
141// GetEscrow returns one escrow by id.
142func GetEscrow(id int) Escrow { return *getEscrow(id) }
143
144// AccruedFees returns the total platform fees collected, in ugnot.
145func AccruedFees() int64 { return feesUgnot }
146
147// Arbiter returns the current dispute referee.
148func Arbiter() address { return arbiter }
149
150// Admin returns the marketplace operator.
151func Admin() address { return admin }