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

index.gno

1.69 Kb · 79 lines
 1package lastcall
 2
 3// Open positions and pending orders live in ordered trees, globally and per
 4// owner, so list reads walk only what is open and the keeper can page them.
 5
 6import (
 7	"gno.land/p/nt/avl/v0"
 8	"gno.land/p/nt/seqid/v0"
 9)
10
11const maxPage = 200
12
13var (
14	perps       = avl.NewTree()
15	ownerPerps  = avl.NewTree()
16	orders      = avl.NewTree()
17	ownerOrders = avl.NewTree()
18)
19
20func idKey(id uint64) string { return seqid.ID(id).String() }
21
22func ownerTree(index *avl.Tree, owner address) *avl.Tree {
23	if v := index.Get(owner.String()); v != nil {
24		return v.(*avl.Tree)
25	}
26	t := avl.NewTree()
27	index.Set(owner.String(), t)
28	return t
29}
30
31func putItem(all, index *avl.Tree, owner address, id uint64, item any) {
32	all.Set(idKey(id), item)
33	ownerTree(index, owner).Set(idKey(id), item)
34}
35
36func dropItem(all, index *avl.Tree, owner address, id uint64) {
37	all.Remove(idKey(id))
38	t := ownerTree(index, owner)
39	t.Remove(idKey(id))
40	if t.Size() == 0 {
41		index.Remove(owner.String())
42	}
43}
44
45func eachOwned(index *avl.Tree, owner address, cb func(item any)) {
46	v := index.Get(owner.String())
47	if v == nil {
48		return
49	}
50	v.(*avl.Tree).Iterate("", "", func(_ string, item any) bool {
51		cb(item)
52		return false
53	})
54}
55
56func eachPage(all *avl.Tree, offset, count int, cb func(item any)) {
57	if offset < 0 || count <= 0 {
58		return
59	}
60	if count > maxPage {
61		count = maxPage
62	}
63	all.IterateByOffset(offset, count, func(_ string, item any) bool {
64		cb(item)
65		return false
66	})
67}
68
69func joinRow(out, row string) string {
70	if out == "" {
71		return row
72	}
73	return out + ";" + row
74}
75
76// OpenPerpCount and OrderCount tell the keeper how many pages to read.
77func OpenPerpCount() int { return perps.Size() }
78
79func OrderCount() int { return orders.Size() }