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.

render.gno

5.27 Kb · 192 lines
  1package vouch
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/num/v0"
  7	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
  8	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/md/v0"
  9	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/realmpath/v0"
 10	vo "gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/social/vouch/v0"
 11)
 12
 13// IndexEdges is how many vouch edges one address's page lists in each
 14// direction.
 15//
 16// Finding 325-cost-at-caps-1: both tables rendered every edge. Anyone can add
 17// an edge to somebody else's page, with a zero bond, and nothing sweeps them,
 18// so the cost every reader of that page pays was set by whoever had been most
 19// persistent.
 20const IndexEdges = 20
 21
 22// Render routes two views: the realm's own index, and one address's page.
 23//
 24// An address carries no slash, so the per-address route is a path element and
 25// not a prefix strip.
 26func Render(path string) string {
 27	req := realmpath.Parse(path)
 28
 29	switch req.PathPart(0) {
 30	case "":
 31		return renderIndex()
 32	case "addr":
 33		return renderAddr(req.PathPart(1))
 34	default:
 35		return md.H1("Not found") + "No such view: " + ui.Inline(req.Path)
 36	}
 37}
 38
 39func renderIndex() string {
 40	out := md.H1("Vouch")
 41	out += "A web of trust other realms gate on. One address vouches for another, " +
 42		"in writing, optionally with GNOT locked behind it.\n\n"
 43
 44	t := ui.NewTable("vouches", "people vouched for", "bonded", "awaiting withdrawal")
 45	t.Row(
 46		strconv.Itoa(graph.Count()),
 47		strconv.Itoa(graph.People()),
 48		num.GNOTf(graph.TotalBonded()),
 49		num.GNOTf(graph.TotalOwed()),
 50	)
 51	out += t.String()
 52
 53	out += md.H2("Most vouched for")
 54	out += board()
 55
 56	out += md.H2("The gate")
 57	out += "One line in the realm that wants it, and it costs no transaction:\n\n"
 58	out += md.LanguageCodeBlock("go", `import "`+realmPath+`"
 59
 60if !vouch.IsTrusted(addr, 2) {
 61	panic("get two people to vouch for you first")
 62}`)
 63	out += md.Italic("A score counts distinct vouchers, so restating a vouch does not " +
 64		"raise it and revoking lowers it. It does not count distinct people: a pair " +
 65		"vouching for each other both reach one, which is why a real gate asks for " +
 66		"more.") + "\n\n"
 67
 68	out += md.H2("Take part")
 69	out += ui.Action("\U0001F91D Vouch", "Vouch", "target", "", "reason", "") +
 70		" · " + ui.Action("\U0001F4B0 Withdraw a refunded bond", "Withdraw") + "\n\n"
 71	out += md.Italic("Coins sent with Vouch are locked as a bond on that vouch and come " +
 72		"back when you revoke it. There is no slashing: deciding that a vouch was a lie " +
 73		"needs an arbiter, and choosing one is the whole design question.") + "\n"
 74
 75	return out
 76}
 77
 78func board() string {
 79	rows := graph.Leaderboard(BoardSize)
 80	if len(rows) == 0 {
 81		return ui.Empty("Nobody has been vouched for yet.")
 82	}
 83	t := ui.NewTable("", "address", "vouchers", "bonded")
 84	for i, r := range rows {
 85		t.Row(
 86			ui.Podium(i),
 87			md.Link(ui.AddrText(r.Addr), vo.AddrURL(realmPath, r.Addr)),
 88			strconv.Itoa(r.Score),
 89			num.GNOTf(r.Bonded),
 90		)
 91	}
 92	return t.String()
 93}
 94
 95func renderAddr(raw string) string {
 96	addr := address(raw)
 97	if !addr.IsValid() {
 98		return md.H1("Vouch") + "Not an address: " + ui.Inline(raw)
 99	}
100
101	out := md.H1("Vouch for " + ui.AddrText(addr))
102	out += ui.AddrFull(addr) + "\n\n"
103
104	score := graph.ScoreOf(addr)
105	t := ui.NewTable("vouchers", "bonded on them", "trusted at 2", "awaiting withdrawal")
106	t.Row(
107		strconv.Itoa(score),
108		num.GNOTf(graph.BondedFor(addr)),
109		yesno(graph.IsTrusted(addr, 2)),
110		num.GNOTf(graph.Owed(addr)),
111	)
112	out += t.String()
113
114	out += md.H2("Vouched for by")
115	out += inbound(addr)
116
117	out += md.H2("Vouches for")
118	out += outbound(addr)
119
120	out += "\n" + ui.Action("\U0001F91D Vouch for this address", "Vouch",
121		"target", addr.String(), "reason", "") +
122		" · " + ui.Action("\U0000274C Revoke mine", "Revoke", "target", addr.String()) + "\n"
123	return out
124}
125
126func inbound(addr address) string {
127	from := graph.VouchedBy(addr)
128	if len(from) == 0 {
129		return ui.Empty("Nobody vouches for this address.")
130	}
131	shown := from
132	if len(shown) > IndexEdges {
133		shown = shown[len(shown)-IndexEdges:]
134	}
135	t := ui.NewTable("voucher", "reason", "bond", "mutual", "since")
136	for _, f := range shown {
137		v, ok := graph.Get(f, addr)
138		if !ok {
139			continue
140		}
141		t.Row(
142			md.Link(ui.AddrText(f), vo.AddrURL(realmPath, f)),
143			ui.Cell(v.Reason),
144			num.GNOTf(v.Bond),
145			yesno(graph.Mutual(f, addr)),
146			strconv.FormatInt(v.At, 10),
147		)
148	}
149	out := t.String()
150	if len(from) > IndexEdges {
151		out += md.Italic("Showing "+strconv.Itoa(IndexEdges)+" of "+
152			strconv.Itoa(len(from))+" vouchers.") + "\n"
153	}
154	return out
155}
156
157func outbound(addr address) string {
158	to := graph.VouchesOf(addr)
159	if len(to) == 0 {
160		return ui.Empty("This address vouches for nobody.")
161	}
162	shownTo := to
163	if len(shownTo) > IndexEdges {
164		shownTo = shownTo[len(shownTo)-IndexEdges:]
165	}
166	t := ui.NewTable("for", "reason", "bond", "their vouchers")
167	for _, target := range shownTo {
168		v, ok := graph.Get(addr, target)
169		if !ok {
170			continue
171		}
172		t.Row(
173			md.Link(ui.AddrText(target), vo.AddrURL(realmPath, target)),
174			ui.Cell(v.Reason),
175			num.GNOTf(v.Bond),
176			strconv.Itoa(graph.ScoreOf(target)),
177		)
178	}
179	out := t.String()
180	if len(to) > IndexEdges {
181		out += md.Italic("Showing "+strconv.Itoa(IndexEdges)+" of "+
182			strconv.Itoa(len(to))+" vouches.") + "\n"
183	}
184	return out
185}
186
187func yesno(b bool) string {
188	if b {
189		return "yes"
190	}
191	return "no"
192}