// Package cust0dy is the Cust0dy marketplace realm: a peer-to-peer // order book and automated escrow engine for Web2 domains, social handles // and Gno handles. Coins lock in this realm at purchase time and move only // on settlement, refund, or arbiter resolution. // // Honesty note: the assets themselves move off-chain (a registrar push, a // password handover). The realm holds the payment, records verification // attestations, and gives both sides a referee (the arbiter) when they // disagree. It cannot force the off-chain transfer itself. package cust0dy import ( "strconv" "time" "chain" core "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cust0dy_core" "gno.land/p/nt/avl/v0" ) // RefundWindow is how long a buyer can pull an automatic refund while the // seller has not submitted any transfer. After the seller submits, the exit // paths are buyer confirmation or a dispute. const RefundWindow = 24 * time.Hour // Listing is one sell-side order. type Listing struct { ID int Seller address Kind core.AssetKind Value string PriceUgnot int64 State core.ListingState Verified bool Created time.Time } // Offer is one buy-side order. The offered amount is locked at creation. type Offer struct { ID int ListingID int Maker address AmountUgnot int64 Open bool Created time.Time } // Escrow is one live trade. type Escrow struct { ID int ListingID int Seller address Buyer address PriceUgnot int64 // agreed price, before fees LockedUgnot int64 // total coins locked by the buyer State core.State FundedAt time.Time DisputeReason string } var ( listings avl.Tree // strconv id -> *Listing offers avl.Tree // strconv id -> *Offer escrows avl.Tree // strconv id -> *Escrow listingSeq int offerSeq int escrowSeq int admin address // marketplace operator; claims once, see ClaimAdmin arbiter address // dispute referee; set by admin feesUgnot int64 // total platform fees accrued (coins sit in the realm balance) ) func mustUserCall(cur realm) { if !cur.Previous().IsUserCall() { panic("direct wallet calls only") } } func mustAdmin(cur realm) { mustUserCall(cur) if cur.Previous().Address() != admin { panic("admin only") } } func mustArbiter(cur realm) { mustUserCall(cur) a := cur.Previous().Address() if a != arbiter && a != admin { panic("arbiter only") } } func getListing(id int) *Listing { v := listings.Get(strconv.Itoa(id)) if v == nil { panic("no such listing") } return v.(*Listing) } func getOffer(id int) *Offer { v := offers.Get(strconv.Itoa(id)) if v == nil { panic("no such offer") } return v.(*Offer) } func getEscrow(id int) *Escrow { v := escrows.Get(strconv.Itoa(id)) if v == nil { panic("no such escrow") } return v.(*Escrow) } // send moves coins out of the realm balance. Only callable after the state // transition has already been recorded by the caller. func send(cur realm, to address, ugnot int64) { if ugnot <= 0 { return } b := bankerNew(cur) b.SendCoins(cur.Address(), to, chain.Coins{{Denom: "ugnot", Amount: ugnot}}) } // ---- read helpers used by Render and qeval ---- // ListingCount returns the number of listings ever created. func ListingCount() int { return listingSeq } // EscrowCount returns the number of escrows ever created. func EscrowCount() int { return escrowSeq } // GetListing returns one listing by id. func GetListing(id int) Listing { return *getListing(id) } // GetEscrow returns one escrow by id. func GetEscrow(id int) Escrow { return *getEscrow(id) } // AccruedFees returns the total platform fees collected, in ugnot. func AccruedFees() int64 { return feesUgnot } // Arbiter returns the current dispute referee. func Arbiter() address { return arbiter } // Admin returns the marketplace operator. func Admin() address { return admin }