Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

grc20reg.gno

3.71 Kb · 133 lines
  1package grc20reg
  2
  3import (
  4	"chain"
  5	"strings"
  6
  7	"gno.land/p/demo/tokens/grc20"
  8	"gno.land/p/moul/md"
  9	"gno.land/p/nt/avl/v0"
 10	"gno.land/p/nt/avl/v0/rotree"
 11	"gno.land/p/nt/fqname/v0"
 12	"gno.land/p/nt/ufmt/v0"
 13)
 14
 15var registry = avl.NewTree() // rlmPath.symbol -> *Token
 16
 17// Construction lives in grc20.NewToken — it takes rlm realm last
 18// and binds origRealm from rlm.PkgPath() under an IsCurrent assertion.
 19// The registry key is the canonical fqname rlmPath.symbol (one token per
 20// realm+symbol), independent of Token.ID()'s trailing sequence id, so
 21// callers can look a token up from the (realm, symbol) pair they already
 22// know:
 23//
 24//	Token, ledger := grc20.NewToken(name, symbol, decimals, id, cur)
 25//	key := grc20reg.Register(cross(cur), Token, "")
 26
 27// Register records token under its rlmPath.symbol key and returns that key.
 28// Token.ID() carries a trailing sequence id (rlmPath.symbol.<id>) that keeps
 29// token identities/events unique, but the registry deliberately keys by
 30// rlmPath.symbol so lookups don't need to know the id, and so a realm cannot
 31// register two tokens under the same symbol (overwrite/alias guard).
 32func Register(cur realm, token *grc20.Token, slug string) string {
 33	if token == nil {
 34		panic("grc20reg: nil token")
 35	}
 36	if slug != "" {
 37		validateSlug(slug)
 38	}
 39	rlmPath := cur.Previous().PkgPath()
 40	key := fqname.Construct(rlmPath, token.GetSymbol())
 41	// Token.ID() == key + "." + <id>; verify the token originates from the
 42	// registering realm and symbol.
 43	if !strings.HasPrefix(token.ID(), key+".") {
 44		panic("grc20reg: token must be registered from its own realm")
 45	}
 46	if registry.Has(key) {
 47		panic("grc20reg: token already registered")
 48	}
 49	registry.Set(key, token)
 50	chain.Emit(
 51		registerEvent,
 52		"token_path", key,
 53		"pkgpath", rlmPath,
 54		"slug", slug,
 55		"symbol", token.GetSymbol(),
 56	)
 57	return key
 58}
 59
 60func Get(key string) *grc20.Token {
 61	token := registry.Get(key)
 62	if token == nil {
 63		return nil
 64	}
 65	return token.(*grc20.Token)
 66}
 67
 68func MustGet(key string) *grc20.Token {
 69	token := Get(key)
 70	if token == nil {
 71		panic("unknown token: " + key)
 72	}
 73	return token
 74}
 75
 76func Render(path string) string {
 77	switch {
 78	case path == "": // home
 79		// TODO: add pagination
 80		s := ""
 81		count := 0
 82		registry.Iterate("", "", func(key string, tokenI any) bool {
 83			count++
 84			token := tokenI.(*grc20.Token)
 85			rlmPath, tokenID := fqname.Parse(key)
 86			rlmLink := fqname.RenderLink(rlmPath, tokenID)
 87			infoLink := "/r/demo/grc20reg:" + key
 88			s += "- " + md.Bold(md.EscapeText(token.GetName())) + " - " + rlmLink + " - " + md.Link("info", infoLink) + "\n"
 89			return false
 90		})
 91		if count == 0 {
 92			return "No registered token."
 93		}
 94		return s
 95	default: // specific token
 96		key := path
 97		token := MustGet(key)
 98		rlmPath, tokenID := fqname.Parse(key)
 99		rlmLink := fqname.RenderLink(rlmPath, tokenID)
100		s := ufmt.Sprintf("# %s\n", md.EscapeText(token.GetName()))
101		s += "- symbol: " + md.Bold(md.EscapeText(token.GetSymbol())) + "\n"
102		s += ufmt.Sprintf("- realm: %s\n", rlmLink)
103		s += ufmt.Sprintf("- decimals: %d\n", token.GetDecimals())
104		s += ufmt.Sprintf("- total supply: %d\n", token.TotalSupply())
105		return s
106	}
107}
108
109const (
110	registerEvent = "register"
111	maxSlugLen    = 128
112)
113
114func GetRegistry() *rotree.ReadOnlyTree {
115	return rotree.Wrap(registry, nil)
116}
117
118// validateSlug panics if the slug is too long or contains non-alphanumeric characters.
119// Only letters, digits, dashes, and underscores are allowed.
120func validateSlug(slug string) {
121	if len(slug) > maxSlugLen {
122		panic("grc20reg: slug too long")
123	}
124	for _, c := range slug {
125		if !isAlphanumeric(c) && c != '_' && c != '-' {
126			panic("grc20reg: invalid slug character: " + string(c))
127		}
128	}
129}
130
131func isAlphanumeric(c rune) bool {
132	return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')
133}