render.gno
9.22 Kb · 245 lines
1package cust0dy
2
3import (
4 "strconv"
5 "strings"
6
7 core "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cust0dy_core"
8)
9
10// Render is the gnoweb entry point.
11// Routes: "" order book, "l/<id>" listing, "e/<id>" deal room, "u/<addr>" profile.
12func Render(path string) string {
13 switch {
14 case path == "":
15 return renderHome()
16 case strings.HasPrefix(path, "l/"):
17 if id, err := strconv.Atoi(strings.TrimPrefix(path, "l/")); err == nil && id >= 1 && id <= listingSeq {
18 return renderListing(getListing(id))
19 }
20 case strings.HasPrefix(path, "e/"):
21 if id, err := strconv.Atoi(strings.TrimPrefix(path, "e/")); err == nil && id >= 1 && id <= escrowSeq {
22 return renderEscrow(getEscrow(id))
23 }
24 case strings.HasPrefix(path, "u/"):
25 return renderProfile(strings.TrimPrefix(path, "u/"))
26 }
27 return "Unknown page. [Back to the order book](:)"
28}
29
30func gnot(ugnot int64) string {
31 whole := ugnot / 1_000_000
32 frac := ugnot % 1_000_000
33 if frac == 0 {
34 return strconv.FormatInt(whole, 10) + " GNOT"
35 }
36 fs := strconv.FormatInt(frac+1_000_000, 10)[1:]
37 fs = strings.TrimRight(fs, "0")
38 return strconv.FormatInt(whole, 10) + "." + fs + " GNOT"
39}
40
41func kindBadge(k core.AssetKind) string {
42 switch k {
43 case core.KindDomain:
44 return "domain"
45 case core.KindSocial:
46 return "social"
47 case core.KindGno:
48 return "gno handle"
49 default:
50 return "asset"
51 }
52}
53
54func renderHome() string {
55 var b strings.Builder
56 b.WriteString("# Cust0dy\n\n")
57 b.WriteString("**Peer-to-peer escrow marketplace for domains and handles.** ")
58 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")
59 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")
60
61 b.WriteString("## Order book\n\n")
62 open := collectOpenListings()
63 if len(open) == 0 {
64 b.WriteString("No open listings yet. Use the Actions tab to `List` the first one.\n")
65 } else {
66 b.WriteString("| Listing | Asset | Price | Verified | Seller |\n")
67 b.WriteString("| --- | --- | --- | --- | --- |\n")
68 for _, l := range open {
69 v := "-"
70 if l.Verified {
71 v = "**yes**"
72 }
73 b.WriteString("| [#" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ") | " +
74 kindBadge(l.Kind) + " `" + l.Value + "` | " + gnot(l.PriceUgnot) + " | " + v +
75 " | `" + shortAddr(l.Seller.String()) + "` |\n")
76 }
77 }
78
79 b.WriteString("\n## How it works\n\n")
80 b.WriteString("1. **List.** The seller lists the asset and gets a verification code.\n")
81 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")
82 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")
83 b.WriteString("4. **Transfer.** The seller moves the asset off-chain and marks the transfer submitted.\n")
84 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")
85 b.WriteString("Safety rails: if the seller never submits a transfer, the buyer gets a full automatic refund after " +
86 strconv.Itoa(int(RefundWindow.Hours())) + " hours. If either side disagrees, `OpenDispute` freezes the trade for the arbiter, whose ruling is public.\n")
87 if arbiter != address("") {
88 b.WriteString("\nArbiter of record: `" + arbiter.String() + "`\n")
89 }
90 b.WriteString("\n_Fees collected so far: " + gnot(feesUgnot) + "_\n")
91 return b.String()
92}
93
94func renderListing(l *Listing) string {
95 var b strings.Builder
96 b.WriteString("# Listing #" + strconv.Itoa(l.ID) + ": `" + l.Value + "`\n\n")
97 b.WriteString("| | |\n| --- | --- |\n")
98 b.WriteString("| kind | " + kindBadge(l.Kind) + " |\n")
99 b.WriteString("| price | **" + gnot(l.PriceUgnot) + "** |\n")
100 b.WriteString("| buyer total (with 2.5% fee) | " + gnot(core.BuyerTotal(l.PriceUgnot)) + " |\n")
101 b.WriteString("| seller would receive | " + gnot(core.SellerPayout(l.PriceUgnot)) + " |\n")
102 b.WriteString("| state | " + l.State.String() + " |\n")
103 v := "not yet"
104 if l.Verified {
105 v = "**verified owner**"
106 }
107 b.WriteString("| ownership | " + v + " |\n")
108 b.WriteString("| seller | `" + l.Seller.String() + "` |\n")
109
110 if l.State == core.ListingOpen {
111 b.WriteString("\n## Verification\n\n")
112 b.WriteString("The seller proves control by publishing this code where only the owner can:\n\n")
113 b.WriteString("```\n" + VerificationCode(l.ID) + "\n```\n\n")
114 switch l.Kind {
115 case core.KindDomain:
116 b.WriteString("As a DNS TXT record on `" + l.Value + "`. Anyone can check it with `verifier/dns_verify.py` from the repo.\n")
117 default:
118 b.WriteString("In the account's bio or a public post, then shared with the arbiter.\n")
119 }
120 b.WriteString("\n## Buy or offer\n\n")
121 b.WriteString("From the Actions tab (or `gnokey maketx call`):\n\n")
122 b.WriteString("- `Buy` with `-send " + strconv.FormatInt(core.BuyerTotal(l.PriceUgnot), 10) + "ugnot` to buy at full price.\n")
123 b.WriteString("- `MakeOffer` with your all-in amount attached to offer less.\n")
124 } else {
125 b.WriteString("\nThis listing is " + l.State.String() + ".\n")
126 }
127 b.WriteString("\n[Back to the order book](:)\n")
128 return b.String()
129}
130
131func renderEscrow(e *Escrow) string {
132 l := getListing(e.ListingID)
133 var b strings.Builder
134 b.WriteString("# Deal room #" + strconv.Itoa(e.ID) + "\n\n")
135 b.WriteString("Asset: `" + l.Value + "` (" + kindBadge(l.Kind) + ") - [listing #" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ")\n\n")
136
137 steps := []struct {
138 name string
139 done bool
140 }{
141 {"payment locked", true},
142 {"transfer submitted", e.State != core.StateFunded},
143 {"settled", e.State == core.StateCompleted || e.State == core.StateResolvedSeller},
144 }
145 for i, s := range steps {
146 mark := " "
147 if s.done {
148 mark = "x"
149 }
150 b.WriteString("- [" + mark + "] " + strconv.Itoa(i+1) + ". " + s.name + "\n")
151 }
152
153 b.WriteString("\n| | |\n| --- | --- |\n")
154 b.WriteString("| state | **" + e.State.String() + "** |\n")
155 b.WriteString("| agreed price | " + gnot(e.PriceUgnot) + " |\n")
156 b.WriteString("| locked | " + gnot(e.LockedUgnot) + " |\n")
157 b.WriteString("| buyer | `" + e.Buyer.String() + "` |\n")
158 b.WriteString("| seller | `" + e.Seller.String() + "` |\n")
159 b.WriteString("| funded | " + e.FundedAt.UTC().Format("2006-01-02 15:04 UTC") + " |\n")
160 if e.DisputeReason != "" {
161 b.WriteString("| dispute | " + e.DisputeReason + " |\n")
162 }
163
164 b.WriteString("\n## Next moves\n\n")
165 switch e.State {
166 case core.StateFunded:
167 b.WriteString("- Seller: move the asset off-chain, then `MarkTransferSubmitted`.\n")
168 b.WriteString("- Buyer: if the seller does nothing for " + strconv.Itoa(int(RefundWindow.Hours())) + "h, `ClaimTimeoutRefund` returns everything.\n")
169 case core.StateTransferSubmitted:
170 b.WriteString("- Buyer: once the asset is in your control, `ConfirmReceived` settles. Confirm only then; it is final.\n")
171 b.WriteString("- Either side: `OpenDispute` if something is wrong.\n")
172 case core.StateDisputed:
173 b.WriteString("- Waiting on the arbiter (`" + arbiter.String() + "`) to rule with `ResolveDispute`.\n")
174 default:
175 b.WriteString("This trade is closed (" + e.State.String() + ").\n")
176 }
177 b.WriteString("\n[Back to the order book](:)\n")
178 return b.String()
179}
180
181func renderProfile(addr string) string {
182 var b strings.Builder
183 b.WriteString("# Trader `" + addr + "`\n\n")
184 var sells, buys []*Escrow
185 escrows.IterateByOffset(0, escrowSeq+1, func(_ string, v any) bool {
186 e := v.(*Escrow)
187 if e.Seller.String() == addr {
188 sells = append(sells, e)
189 }
190 if e.Buyer.String() == addr {
191 buys = append(buys, e)
192 }
193 return false
194 })
195 var mine []*Listing
196 listings.IterateByOffset(0, listingSeq+1, func(_ string, v any) bool {
197 l := v.(*Listing)
198 if l.Seller.String() == addr {
199 mine = append(mine, l)
200 }
201 return false
202 })
203 b.WriteString("## Listings\n\n")
204 if len(mine) == 0 {
205 b.WriteString("None.\n")
206 }
207 for _, l := range mine {
208 b.WriteString("- [#" + strconv.Itoa(l.ID) + "](:l/" + strconv.Itoa(l.ID) + ") `" + l.Value + "` - " + gnot(l.PriceUgnot) + " - " + l.State.String() + "\n")
209 }
210 b.WriteString("\n## Trades as buyer\n\n")
211 if len(buys) == 0 {
212 b.WriteString("None.\n")
213 }
214 for _, e := range buys {
215 b.WriteString("- [deal #" + strconv.Itoa(e.ID) + "](:e/" + strconv.Itoa(e.ID) + ") - " + e.State.String() + " - " + gnot(e.LockedUgnot) + " locked\n")
216 }
217 b.WriteString("\n## Trades as seller\n\n")
218 if len(sells) == 0 {
219 b.WriteString("None.\n")
220 }
221 for _, e := range sells {
222 b.WriteString("- [deal #" + strconv.Itoa(e.ID) + "](:e/" + strconv.Itoa(e.ID) + ") - " + e.State.String() + " - " + gnot(e.PriceUgnot) + "\n")
223 }
224 b.WriteString("\n[Back to the order book](:)\n")
225 return b.String()
226}
227
228func collectOpenListings() []*Listing {
229 var out []*Listing
230 listings.IterateByOffset(0, listingSeq+1, func(_ string, v any) bool {
231 l := v.(*Listing)
232 if l.State == core.ListingOpen {
233 out = append(out, l)
234 }
235 return false
236 })
237 return out
238}
239
240func shortAddr(a string) string {
241 if len(a) <= 14 {
242 return a
243 }
244 return a[:8] + "..." + a[len(a)-4:]
245}