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

supplyseries.gno

1.64 Kb · 58 lines
 1package kourt
 2import (
 3	"strconv"
 4	"strings"
 5)
 6const (
 7	burnSeriesKey  = "burn"
 8	priceSeriesKey = "price"
 9)
10func recordPrice(c *Court) {
11	setIfChanged(ensureCSArch(c), &c.priceH, priceSeriesKey,
12		uint32(heightNow()/epochBlocks)+1, c.crv.Price(c.minted))
13}
14func recordBurn(c *Court) {
15	setIfChanged(ensureCSArch(c), &c.burnH, burnSeriesKey,
16		uint32(heightNow()/epochBlocks)+1, c.burnedGNOT)
17}
18func formatSeries(width int64, now uint32, pts []histPoint, more bool) string {
19	rows := make([]string, 0, len(pts))
20	for _, p := range pts {
21		rows = append(rows, strconv.FormatUint(uint64(p.e), 10)+":"+
22			strconv.FormatInt(p.v, 10))
23	}
24	m := "0"
25	if more {
26		m = "1"
27	}
28	return strconv.FormatInt(width, 10) + "," +
29		strconv.FormatUint(uint64(now), 10) + "," + m + ";" +
30		strings.Join(rows, ",")
31}
32func SupplySeries(courtSlug string) string {
33	c := mustCourt(courtSlug)
34	pts := []histPoint{}
35	more := false
36	c.coin.WalkSupply(func(e uint32, v int64) bool {
37		if len(pts) >= seriesRowCap {
38			more = true
39			return true
40		}
41		pts = append(pts, histPoint{e, v})
42		return false
43	})
44	for i, j := 0, len(pts)-1; i < j; i, j = i+1, j-1 {
45		pts[i], pts[j] = pts[j], pts[i]
46	}
47	return formatSeries(c.coin.EpochBlocks(), c.coin.Epoch(), pts, more)
48}
49func BurnSeries(courtSlug string) string {
50	c := mustCourt(courtSlug)
51	pts, more := collectSeriesKeyed(c.csArch, &c.burnH, burnSeriesKey)
52	return formatSeries(epochBlocks, uint32(heightNow()/epochBlocks)+1, pts, more)
53}
54func PriceSeries(courtSlug string) string {
55	c := mustCourt(courtSlug)
56	pts, more := collectSeriesKeyed(c.csArch, &c.priceH, priceSeriesKey)
57	return formatSeries(epochBlocks, uint32(heightNow()/epochBlocks)+1, pts, more)
58}