store.gno
2.88 Kb ยท 127 lines
1package position
2
3import (
4 "errors"
5 "strconv"
6
7 "gno.land/p/gnoswap/store"
8 "gno.land/p/nt/avl"
9 "gno.land/p/nt/ufmt"
10)
11
12type StoreKey string
13
14func (s StoreKey) String() string {
15 return string(s)
16}
17
18const (
19 StoreKeyPositions StoreKey = "positions" // Positions
20 StoreKeyPositionNextID StoreKey = "positionNextID" // Position next ID
21)
22
23type positionStore struct {
24 kvStore store.KVStore
25}
26
27func (s *positionStore) HasPositionsStoreKey() bool {
28 return s.kvStore.Has(StoreKeyPositions.String())
29}
30
31func (s *positionStore) GetPositions() *avl.Tree {
32 result, err := s.kvStore.Get(StoreKeyPositions.String())
33 if err != nil {
34 panic(err)
35 }
36
37 positions, ok := result.(*avl.Tree)
38 if !ok {
39 panic(ufmt.Sprintf("failed to cast result to *avl.Tree: %T", result))
40 }
41
42 return positions
43}
44
45func (s *positionStore) SetPositions(positions *avl.Tree) error {
46 return s.kvStore.Set(StoreKeyPositions.String(), positions)
47}
48
49func (s *positionStore) HasPositionNextIDStoreKey() bool {
50 return s.kvStore.Has(StoreKeyPositionNextID.String())
51}
52
53func (s *positionStore) GetPositionNextID() uint64 {
54 result, err := s.kvStore.Get(StoreKeyPositionNextID.String())
55 if err != nil {
56 panic(err)
57 }
58
59 nextID, ok := result.(uint64)
60 if !ok {
61 panic(ufmt.Sprintf("failed to cast result to uint64: %T", result))
62 }
63
64 return nextID
65}
66
67func (s *positionStore) SetPositionNextID(nextID uint64) error {
68 return s.kvStore.Set(StoreKeyPositionNextID.String(), nextID)
69}
70
71func (s *positionStore) HasPosition(positionId uint64) bool {
72 positions := s.GetPositions()
73
74 return positions.Has(uint64ToString(positionId))
75}
76
77func (s *positionStore) GetPosition(positionId uint64) (Position, bool) {
78 positions := s.GetPositions()
79
80 result, ok := positions.Get(uint64ToString(positionId))
81 if !ok {
82 return Position{}, false
83 }
84
85 position, ok := result.(Position)
86 if !ok {
87 panic(ufmt.Sprintf("failed to cast result to Position: %T", result))
88 }
89
90 return position, true
91}
92
93func (s *positionStore) SetPosition(positionId uint64, position Position) error {
94 if !s.HasPositionsStoreKey() {
95 return errors.New("positions store key not found")
96 }
97
98 positions := s.GetPositions()
99
100 positions.Set(uint64ToString(positionId), position)
101
102 return s.kvStore.Set(StoreKeyPositions.String(), positions)
103}
104
105func (s *positionStore) RemovePosition(positionId uint64) error {
106 if !s.HasPositionsStoreKey() {
107 return errors.New("positions store key not found")
108 }
109
110 positions := s.GetPositions()
111
112 positions.Remove(uint64ToString(positionId))
113
114 return s.kvStore.Set(StoreKeyPositions.String(), positions)
115}
116
117// NewPositionStore creates a new protocol fee store instance with the provided KV store.
118// This function is used by the upgrade system to create storage instances for each implementation.
119func NewPositionStore(kvStore store.KVStore) IPositionStore {
120 return &positionStore{
121 kvStore: kvStore,
122 }
123}
124
125func uint64ToString(id uint64) string {
126 return strconv.FormatUint(id, 10)
127}