holders.gno
1.22 Kb · 58 lines
1package kourtv3
2const topHoldersMax = 50
3func HolderCount(courtSlug string) int {
4 c := mustCourt(courtSlug)
5 esc := c.escrow
6 n := 0
7 c.coin.WalkAccounts(func(who address, bal int64) bool {
8 if bal > 0 && who != esc {
9 n++
10 }
11 return false
12 })
13 return n
14}
15func TopHolders(courtSlug string, limit int) string {
16 if limit <= 0 || limit > topHoldersMax {
17 limit = topHoldersMax
18 }
19 c := mustCourt(courtSlug)
20 esc := c.escrow
21 who := make([]address, 0, limit)
22 bal := make([]int64, 0, limit)
23 c.coin.WalkAccounts(func(a address, b int64) bool {
24 if b <= 0 || a == esc {
25 return false
26 }
27 pos := len(who)
28 for pos > 0 && b > bal[pos-1] {
29 pos--
30 }
31 if pos >= limit {
32 return false
33 }
34 if len(who) < limit {
35 who = append(who, address(""))
36 bal = append(bal, 0)
37 }
38 for i := len(who) - 1; i > pos; i-- {
39 who[i], bal[i] = who[i-1], bal[i-1]
40 }
41 who[pos], bal[pos] = a, b
42 return false
43 })
44 out := ""
45 for i := range who {
46 if bal[i] <= 0 {
47 continue
48 }
49 out += ";" + string(who[i]) + ":" + itoa(bal[i])
50 }
51 if out == "" {
52 return ""
53 }
54 return out[1:]
55}
56func TopHoldersNote() string {
57 return "Ranked by coin held. Coin staked on a claim sits in the court's custody until the claim resolves, so it is not counted here."
58}