Search Apps Documentation Source Content File Folder Download Copy Actions Download

sales.gno

1.30 Kb · 52 lines
 1package gnogle_nftmarket
 2
 3import (
 4	"chain/runtime"
 5	"strings"
 6
 7	"gno.land/p/nt/ufmt/v0"
 8)
 9
10// Sale is one settled trade, kept in a bounded on-chain history so the UI can
11// show recent activity and compute volume without scanning events.
12type Sale struct {
13	collID  string
14	tokenID string
15	from    address
16	to      address
17	price   int64
18	kind    string // "buy" | "offer" | "auction"
19	height  int64
20}
21
22var sales []*Sale
23
24// recordSale appends a settled trade, keeping only the most recent 200.
25func recordSale(collID, tokenID string, from, to address, price int64, kind string) {
26	sales = append(sales, &Sale{collID, tokenID, from, to, price, kind, runtime.ChainHeight()})
27	if len(sales) > 200 {
28		sales = sales[len(sales)-200:]
29	}
30}
31
32// SalesJSON returns the most recent sales, newest first, up to limit (default 50).
33func SalesJSON(limit int) string {
34	if limit <= 0 || limit > 200 {
35		limit = 50
36	}
37	var b strings.Builder
38	b.WriteString("[")
39	n := 0
40	for i := len(sales) - 1; i >= 0 && n < limit; i-- {
41		s := sales[i]
42		if n > 0 {
43			b.WriteString(",")
44		}
45		b.WriteString(ufmt.Sprintf(
46			`{"collID":%s,"tokenID":%s,"from":%s,"to":%s,"price":%d,"kind":%s,"height":%d}`,
47			js(s.collID), js(s.tokenID), js(s.from.String()), js(s.to.String()), s.price, js(s.kind), s.height))
48		n++
49	}
50	b.WriteString("]")
51	return b.String()
52}