package cust0dy import ( "strconv" "strings" core "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cust0dy_core" ) // Render is the gnoweb entry point. // Routes: "" order book, "l/" listing, "e/" deal room, "u/" profile. func Render(path string) string { switch { case path == "": return renderHome() case strings.HasPrefix(path, "l/"): if id, err := strconv.Atoi(strings.TrimPrefix(path, "l/")); err == nil && id >= 1 && id <= listingSeq { return renderListing(getListing(id)) } case strings.HasPrefix(path, "e/"): if id, err := strconv.Atoi(strings.TrimPrefix(path, "e/")); err == nil && id >= 1 && id <= escrowSeq { return renderEscrow(getEscrow(id)) } case strings.HasPrefix(path, "u/"): return renderProfile(strings.TrimPrefix(path, "u/")) } return "Unknown page. [Back to the order book](:)" } func gnot(ugnot int64) string { whole := ugnot / 1_000_000 frac := ugnot % 1_000_000 if frac == 0 { return strconv.FormatInt(whole, 10) + " GNOT" } fs := strconv.FormatInt(frac+1_000_000, 10)[1:] fs = strings.TrimRight(fs, "0") return strconv.FormatInt(whole, 10) + "." + fs + " GNOT" } func kindBadge(k core.AssetKind) string { switch k { case core.KindDomain: return "domain" case core.KindSocial: return "social" case core.KindGno: return "gno handle" default: return "asset" } } func renderHome() string { var b strings.Builder b.WriteString("# Cust0dy\n\n") b.WriteString("**Peer-to-peer escrow marketplace for domains and handles.** ") b.WriteString("Sell a domain, an X handle or a Gno handle; the buyer's payment locks in this realm and is only released when the buyer confirms the asset arrived. If anything goes wrong, a named arbiter steps in - every decision is on the record.\n\n") b.WriteString("> Honest fine print: the assets move off-chain (a registrar push, an account handover). This realm holds the money, records the verification, and referees. It cannot push the domain for you.\n\n") b.WriteString("## Order book\n\n") open := collectOpenListings() if len(open) == 0 { b.WriteString("No open listings yet. Use the Actions tab to `List` the first one.\n") } else { b.WriteString("| Listing | Asset | Price | Verified | Seller |\n") b.WriteString("| --- | --- | --- | --- | --- |\n") for _, l := range open { v := "-" if l.Verified { v = "**yes**" } b.WriteString("| [#" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ") | " + kindBadge(l.Kind) + " `" + l.Value + "` | " + gnot(l.PriceUgnot) + " | " + v + " | `" + shortAddr(l.Seller.String()) + "` |\n") } } b.WriteString("\n## How it works\n\n") b.WriteString("1. **List.** The seller lists the asset and gets a verification code.\n") b.WriteString("2. **Verify.** The seller publishes that code where only the owner can - a DNS TXT record for a domain, a bio or post for a handle - and the arbiter attests it on-chain after checking.\n") b.WriteString("3. **Fund.** The buyer calls `Buy` (or makes an offer the seller accepts). The payment locks in the realm. Nobody can touch it now except the rules below.\n") b.WriteString("4. **Transfer.** The seller moves the asset off-chain and marks the transfer submitted.\n") b.WriteString("5. **Settle.** The buyer confirms receipt; the seller is paid minus a 5% seller fee. Buys carry a 2.5% buyer fee; offers are all-in with no buyer fee.\n\n") b.WriteString("Safety rails: if the seller never submits a transfer, the buyer gets a full automatic refund after " + strconv.Itoa(int(RefundWindow.Hours())) + " hours. If either side disagrees, `OpenDispute` freezes the trade for the arbiter, whose ruling is public.\n") if arbiter != address("") { b.WriteString("\nArbiter of record: `" + arbiter.String() + "`\n") } b.WriteString("\n_Fees collected so far: " + gnot(feesUgnot) + "_\n") return b.String() } func renderListing(l *Listing) string { var b strings.Builder b.WriteString("# Listing #" + strconv.Itoa(l.ID) + ": `" + l.Value + "`\n\n") b.WriteString("| | |\n| --- | --- |\n") b.WriteString("| kind | " + kindBadge(l.Kind) + " |\n") b.WriteString("| price | **" + gnot(l.PriceUgnot) + "** |\n") b.WriteString("| buyer total (with 2.5% fee) | " + gnot(core.BuyerTotal(l.PriceUgnot)) + " |\n") b.WriteString("| seller would receive | " + gnot(core.SellerPayout(l.PriceUgnot)) + " |\n") b.WriteString("| state | " + l.State.String() + " |\n") v := "not yet" if l.Verified { v = "**verified owner**" } b.WriteString("| ownership | " + v + " |\n") b.WriteString("| seller | `" + l.Seller.String() + "` |\n") if l.State == core.ListingOpen { b.WriteString("\n## Verification\n\n") b.WriteString("The seller proves control by publishing this code where only the owner can:\n\n") b.WriteString("```\n" + VerificationCode(l.ID) + "\n```\n\n") switch l.Kind { case core.KindDomain: b.WriteString("As a DNS TXT record on `" + l.Value + "`. Anyone can check it with `verifier/dns_verify.py` from the repo.\n") default: b.WriteString("In the account's bio or a public post, then shared with the arbiter.\n") } b.WriteString("\n## Buy or offer\n\n") b.WriteString("From the Actions tab (or `gnokey maketx call`):\n\n") b.WriteString("- `Buy` with `-send " + strconv.FormatInt(core.BuyerTotal(l.PriceUgnot), 10) + "ugnot` to buy at full price.\n") b.WriteString("- `MakeOffer` with your all-in amount attached to offer less.\n") } else { b.WriteString("\nThis listing is " + l.State.String() + ".\n") } b.WriteString("\n[Back to the order book](:)\n") return b.String() } func renderEscrow(e *Escrow) string { l := getListing(e.ListingID) var b strings.Builder b.WriteString("# Deal room #" + strconv.Itoa(e.ID) + "\n\n") b.WriteString("Asset: `" + l.Value + "` (" + kindBadge(l.Kind) + ") - [listing #" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ")\n\n") steps := []struct { name string done bool }{ {"payment locked", true}, {"transfer submitted", e.State != core.StateFunded}, {"settled", e.State == core.StateCompleted || e.State == core.StateResolvedSeller}, } for i, s := range steps { mark := " " if s.done { mark = "x" } b.WriteString("- [" + mark + "] " + strconv.Itoa(i+1) + ". " + s.name + "\n") } b.WriteString("\n| | |\n| --- | --- |\n") b.WriteString("| state | **" + e.State.String() + "** |\n") b.WriteString("| agreed price | " + gnot(e.PriceUgnot) + " |\n") b.WriteString("| locked | " + gnot(e.LockedUgnot) + " |\n") b.WriteString("| buyer | `" + e.Buyer.String() + "` |\n") b.WriteString("| seller | `" + e.Seller.String() + "` |\n") b.WriteString("| funded | " + e.FundedAt.UTC().Format("2006-01-02 15:04 UTC") + " |\n") if e.DisputeReason != "" { b.WriteString("| dispute | " + e.DisputeReason + " |\n") } b.WriteString("\n## Next moves\n\n") switch e.State { case core.StateFunded: b.WriteString("- Seller: move the asset off-chain, then `MarkTransferSubmitted`.\n") b.WriteString("- Buyer: if the seller does nothing for " + strconv.Itoa(int(RefundWindow.Hours())) + "h, `ClaimTimeoutRefund` returns everything.\n") case core.StateTransferSubmitted: b.WriteString("- Buyer: once the asset is in your control, `ConfirmReceived` settles. Confirm only then; it is final.\n") b.WriteString("- Either side: `OpenDispute` if something is wrong.\n") case core.StateDisputed: b.WriteString("- Waiting on the arbiter (`" + arbiter.String() + "`) to rule with `ResolveDispute`.\n") default: b.WriteString("This trade is closed (" + e.State.String() + ").\n") } b.WriteString("\n[Back to the order book](:)\n") return b.String() } func renderProfile(addr string) string { var b strings.Builder b.WriteString("# Trader `" + addr + "`\n\n") var sells, buys []*Escrow escrows.IterateByOffset(0, escrowSeq+1, func(_ string, v any) bool { e := v.(*Escrow) if e.Seller.String() == addr { sells = append(sells, e) } if e.Buyer.String() == addr { buys = append(buys, e) } return false }) var mine []*Listing listings.IterateByOffset(0, listingSeq+1, func(_ string, v any) bool { l := v.(*Listing) if l.Seller.String() == addr { mine = append(mine, l) } return false }) b.WriteString("## Listings\n\n") if len(mine) == 0 { b.WriteString("None.\n") } for _, l := range mine { b.WriteString("- [#" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ") `" + l.Value + "` - " + gnot(l.PriceUgnot) + " - " + l.State.String() + "\n") } b.WriteString("\n## Trades as buyer\n\n") if len(buys) == 0 { b.WriteString("None.\n") } for _, e := range buys { b.WriteString("- [deal #" + strconv.Itoa(e.ID) + "](:e/" + strconv.Itoa(e.ID) + ") - " + e.State.String() + " - " + gnot(e.LockedUgnot) + " locked\n") } b.WriteString("\n## Trades as seller\n\n") if len(sells) == 0 { b.WriteString("None.\n") } for _, e := range sells { b.WriteString("- [deal #" + strconv.Itoa(e.ID) + "](:e/" + strconv.Itoa(e.ID) + ") - " + e.State.String() + " - " + gnot(e.PriceUgnot) + "\n") } b.WriteString("\n[Back to the order book](:)\n") return b.String() } func collectOpenListings() []*Listing { var out []*Listing listings.IterateByOffset(0, listingSeq+1, func(_ string, v any) bool { l := v.(*Listing) if l.State == core.ListingOpen { out = append(out, l) } return false }) return out } func shortAddr(a string) string { if len(a) <= 14 { return a } return a[:8] + "..." + a[len(a)-4:] }