Skip to content Search Apps Documentation Source Content 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.

views.gno

4.01 Kb · 166 lines
  1package market
  2
  3import "strconv"
  4
  5// InfoJSON is the collection header. ready is false until Init.
  6func InfoJSON() string {
  7	if !ready {
  8		return `{"ready":false}`
  9	}
 10	return `{"ready":true,"name":` + quote(token.GetName()) +
 11		`,"symbol":` + quote(token.GetSymbol()) +
 12		`,"creator":` + quote(creator.String()) +
 13		`,"supply":` + i64(token.TotalSupply()) +
 14		`,"listings":` + strconv.Itoa(listings.Size()) + `}`
 15}
 16
 17// ListingsJSON returns one page of listings, oldest key first.
 18func ListingsJSON(offset, limit int) string {
 19	if !ready {
 20		return "[]"
 21	}
 22	offset, limit = page(offset, limit)
 23	parts := make([]string, 0)
 24	listings.IterateByOffset(offset, limit, func(key string, value any) bool {
 25		parts = append(parts, lotJSON(key, value.(listing)))
 26		return false
 27	})
 28	return "[" + join(parts) + "]"
 29}
 30
 31// ListingJSON returns one listing or "null".
 32func ListingJSON(tid string) string {
 33	if !ready {
 34		return "null"
 35	}
 36	id := parseTid(tid)
 37	got := listings.Get(string(id))
 38	if got == nil {
 39		return "null"
 40	}
 41	return lotJSON(string(id), got.(listing))
 42}
 43
 44// TokenJSON returns one token, listed or not.
 45func TokenJSON(tid string) string {
 46	if !ready {
 47		return "null"
 48	}
 49	id := parseTid(tid)
 50	owner := ownerOf(id)
 51	name, image := metaNameImage(id)
 52	body := `{"tid":` + quote(string(id)) +
 53		`,"owner":` + quote(owner.String()) +
 54		`,"name":` + quote(name) +
 55		`,"image":` + quote(image)
 56	got := listings.Get(string(id))
 57	if got == nil {
 58		return body + `,"listed":false}`
 59	}
 60	lot := got.(listing)
 61	return body + `,"listed":true,"seller":` + quote(lot.Seller.String()) +
 62		`,"price":` + i64(lot.Price) +
 63		`,"live":` + boolJSON(owner == lot.Seller) + `}`
 64}
 65
 66// TokensJSON lists tokens owned by owner.
 67func TokensJSON(owner address, offset, limit int) string {
 68	if !ready {
 69		return "[]"
 70	}
 71	if !owner.IsValid() {
 72		panic("market: owner")
 73	}
 74	offset, limit = page(offset, limit)
 75	balance, err := token.BalanceOf(owner)
 76	if err != nil {
 77		panic("market: balance")
 78	}
 79	parts := make([]string, 0)
 80	end := int64(offset + limit)
 81	for i := int64(offset); i < balance && i < end; i++ {
 82		id, err := enumView.TokenOfOwnerByIndex(owner, i)
 83		if err != nil {
 84			panic("market: index")
 85		}
 86		name, image := metaNameImage(id)
 87		item := `{"tid":` + quote(string(id)) + `,"name":` + quote(name) + `,"image":` + quote(image)
 88		got := listings.Get(string(id))
 89		if got == nil {
 90			item += `,"listed":false}`
 91		} else {
 92			lot := got.(listing)
 93			item += `,"listed":true,"price":` + i64(lot.Price) + `}`
 94		}
 95		parts = append(parts, item)
 96	}
 97	return "[" + join(parts) + "]"
 98}
 99
100// SalesJSON returns the newest sales first.
101func SalesJSON(offset, limit int) string {
102	if !ready {
103		return "[]"
104	}
105	offset, limit = page(offset, limit)
106	n := saleN
107	if n > saleCap {
108		n = saleCap
109	}
110	parts := make([]string, 0)
111	for i := offset; i < offset+limit && i < n; i++ {
112		// i = 0 is the newest stored sale.
113		idx := saleN - 1 - i
114		s := sales[idx%saleCap]
115		parts = append(parts, `{"tid":`+quote(s.Tid)+
116			`,"name":`+quote(s.Name)+
117			`,"seller":`+quote(s.Seller.String())+
118			`,"buyer":`+quote(s.Buyer.String())+
119			`,"price":`+i64(s.Price)+`}`)
120	}
121	return "[" + join(parts) + "]"
122}
123
124func lotJSON(tid string, lot listing) string {
125	id := parseTid(tid)
126	owner := ownerOf(id)
127	name, image := metaNameImage(id)
128	return `{"tid":` + quote(tid) +
129		`,"seller":` + quote(lot.Seller.String()) +
130		`,"owner":` + quote(owner.String()) +
131		`,"price":` + i64(lot.Price) +
132		`,"name":` + quote(name) +
133		`,"image":` + quote(image) +
134		`,"live":` + boolJSON(owner == lot.Seller) + `}`
135}
136
137func boolJSON(v bool) string {
138	if v {
139		return "true"
140	}
141	return "false"
142}
143
144func join(parts []string) string {
145	out := ""
146	for i, p := range parts {
147		if i > 0 {
148			out += ","
149		}
150		out += p
151	}
152	return out
153}
154
155func Render(path string) string {
156	if path != "" {
157		return "404\n"
158	}
159	if !ready {
160		return "# market\n\ncall Init first\n"
161	}
162	return "# " + token.GetName() + " ($" + token.GetSymbol() + ")\n\n" +
163		"* creator: " + creator.String() + "\n" +
164		"* supply: " + i64(token.TotalSupply()) + "\n" +
165		"* listings: " + strconv.Itoa(listings.Size()) + "\n"
166}