render.gno

3.19 Kb ยท 145 lines
  1package tokenhub
  2
  3import (
  4	"strings"
  5
  6	"gno.land/p/demo/avl/pager"
  7	"gno.land/p/demo/grc/grc20"
  8	"gno.land/p/demo/grc/grc721"
  9	"gno.land/p/demo/ufmt"
 10	"gno.land/p/moul/md"
 11	"gno.land/r/demo/grc20reg"
 12)
 13
 14const (
 15	token = "token" // grc20
 16	nft   = "nft"   // grc721
 17	mt    = "mt"    // grc1155
 18)
 19
 20func Render(path string) string {
 21	var out string
 22
 23	switch {
 24	case path == "":
 25		out = renderHome()
 26
 27	case strings.HasPrefix(path, token):
 28		out = renderToken(path)
 29
 30	case strings.HasPrefix(path, nft):
 31		out = renderNFT(path)
 32
 33	case strings.HasPrefix(path, mt):
 34		out = renderMT(path)
 35	}
 36
 37	return out
 38}
 39
 40func renderHome() string {
 41	out := md.H1("Token Hub")
 42	out += md.Paragraph("A central registry for GRC721 NFTs, GRC20 tokens, and GRC1155 multi-tokens on gno.land")
 43
 44	links := []string{
 45		"[GRC20 Tokens](/r/matijamarjanovic/tokenhub:tokens)",
 46		"[GRC721 NFTs](/r/matijamarjanovic/tokenhub:nfts)",
 47		"[GRC1155 Multi-Tokens](/r/matijamarjanovic/tokenhub:mts)",
 48	}
 49	out += md.BulletList(links)
 50
 51	return out
 52}
 53
 54func renderToken(path string) string {
 55	out := md.H1("GRC20 Tokens")
 56	var tokenItems []string
 57
 58	tokenPager := pager.NewPager(grc20reg.GetRegistry(), pageSize, false)
 59	page := tokenPager.MustGetPageByPath(path)
 60
 61	for _, item := range page.Items {
 62		tokenGetter := item.Value.(grc20.TokenGetter)
 63		token := tokenGetter()
 64		tokenItems = append(tokenItems, ufmt.Sprintf("%s (%s) [%s]",
 65			md.Bold(token.GetName()),
 66			md.InlineCode(token.GetSymbol()),
 67			md.InlineCode(item.Key)))
 68	}
 69
 70	out += renderItemsList(tokenItems, page, "No tokens registered yet")
 71	out += renderFooter()
 72	return out
 73}
 74
 75func renderNFT(path string) string {
 76	out := md.H1("GRC721 NFTs")
 77
 78	var nftItems []string
 79	nftPager := pager.NewPager(registeredNFTs, pageSize, false)
 80	page := nftPager.MustGetPageByPath(path)
 81
 82	for _, item := range page.Items {
 83		nftGetter := item.Value.(grc721.NFTGetter)
 84		nft := nftGetter()
 85		metadata, ok := nft.(grc721.IGRC721CollectionMetadata)
 86		if !ok {
 87			continue
 88		}
 89
 90		nftItems = append(nftItems, ufmt.Sprintf("%s (%s) [%s]",
 91			md.Bold(metadata.Name()),
 92			md.InlineCode(metadata.Symbol()),
 93			md.InlineCode(item.Key)))
 94	}
 95
 96	out += renderItemsList(nftItems, page, "No NFTs registered yet")
 97	out += renderFooter()
 98	return out
 99}
100
101func renderMT(path string) string {
102	out := md.H1("GRC1155 Multi-Tokens")
103	var mtItems []string
104
105	mtPager := pager.NewPager(registeredMTs, pageSize, false)
106	page := mtPager.MustGetPageByPath(path)
107
108	for _, item := range page.Items {
109		info := item.Value.(GRC1155TokenInfo)
110		mtItems = append(mtItems, ufmt.Sprintf("%s: %s [%s]",
111			md.Bold("TokenID"),
112			md.InlineCode(info.TokenID),
113			md.InlineCode(item.Key)))
114	}
115
116	out += renderItemsList(mtItems, page, "No multi-tokens registered yet")
117	out += renderFooter()
118	return out
119}
120
121func renderItemsList(items []string, page *pager.Page, emptyMessage string) string {
122	var out string
123	if len(items) == 0 {
124		out += md.Italic(emptyMessage)
125		out += "\n"
126		return out
127	}
128
129	out += md.BulletList(items)
130	out += "\n"
131	out += md.HorizontalRule()
132
133	picker := page.Picker(page.Pager.PageQueryParam)
134	if picker != "" {
135		out += md.Paragraph(picker)
136	}
137
138	return out
139}
140
141func renderFooter() string {
142	out := "\n"
143	out += md.Link("Back to home", "/r/matijamarjanovic/tokenhub")
144	return out
145}