store.gno
9.05 Kb ยท 328 lines
1package protocol_fee
2
3import (
4 "errors"
5
6 "gno.land/p/gnoswap/store"
7 "gno.land/p/nt/avl"
8 "gno.land/p/nt/ufmt"
9)
10
11type StoreKey string
12
13func (s StoreKey) String() string {
14 return string(s)
15}
16
17const (
18 // By default, devOps will get 0% of the protocol fee (which means gov/staker will get 100% of the protocol fee)
19 // This percentage can be modified through governance.
20 StoreKeyDevOpsPct StoreKey = "devOpsPct"
21
22 // accumulated amount distributed to gov/staker by token path
23 StoreKeyAccuToGovStaker StoreKey = "accuToGovStaker" // tokenPath -> amount
24 StoreKeyAccuToDevOps StoreKey = "accuToDevOps" // tokenPath -> amount
25
26 // distributedToDevOpsHistory and distributedToGovStakerHistory are used to keep track of the distribution history
27 StoreKeyDistributedToGovStakerHistory StoreKey = "distributedToGovStakerHistory" // tokenPath -> amount
28 StoreKeyDistributedToDevOpsHistory StoreKey = "distributedToDevOpsHistory" // tokenPath -> amount
29
30 // tokenListWithAmounts is used to keep track of the token list with amount
31 StoreKeyTokenListWithAmounts StoreKey = "tokenListWithAmounts" // tokenPath -> amount
32)
33
34const (
35 defaultDevOpsPct = int64(0)
36)
37
38type protocolFeeStore struct {
39 kvStore store.KVStore
40}
41
42// handle devOpsPct store data
43func (s *protocolFeeStore) HasDevOpsPctStoreKey() bool {
44 return s.kvStore.Has(StoreKeyDevOpsPct.String())
45}
46
47func (s *protocolFeeStore) InitializeDevOpsPct() error {
48 return s.kvStore.Set(StoreKeyDevOpsPct.String(), defaultDevOpsPct)
49}
50
51func (s *protocolFeeStore) GetDevOpsPct() int64 {
52 devOpsPct, err := s.kvStore.GetInt64(StoreKeyDevOpsPct.String())
53 if err != nil {
54 panic(err)
55 }
56
57 return devOpsPct
58}
59
60func (s *protocolFeeStore) SetDevOpsPct(pct int64) error {
61 return s.kvStore.Set(StoreKeyDevOpsPct.String(), pct)
62}
63
64// handle accuToGovStaker store data
65func (s *protocolFeeStore) HasAccuToGovStakerStoreKey() bool {
66 return s.kvStore.Has(StoreKeyAccuToGovStaker.String())
67}
68
69func (s *protocolFeeStore) InitializeAccuToGovStaker() error {
70 return s.kvStore.Set(StoreKeyAccuToGovStaker.String(), avl.NewTree())
71}
72
73func (s *protocolFeeStore) GetAccuToGovStaker() *avl.Tree {
74 accuToGovStaker, err := s.kvStore.GetTree(StoreKeyAccuToGovStaker.String())
75 if err != nil {
76 panic(err)
77 }
78
79 return accuToGovStaker
80}
81
82func (s *protocolFeeStore) GetAccuToGovStakerItem(tokenPath string) (int64, bool) {
83 accuToGovStaker, err := s.kvStore.GetTree(StoreKeyAccuToGovStaker.String())
84 if err != nil {
85 panic(err)
86 }
87
88 result, ok := accuToGovStaker.Get(tokenPath)
89 if !ok {
90 return 0, false
91 }
92
93 amount, ok := result.(int64)
94 if !ok {
95 panic(ufmt.Errorf("failed to cast result to int64: %T", result))
96 }
97
98 return amount, true
99}
100
101func (s *protocolFeeStore) SetAccuToGovStakerItem(tokenPath string, amount int64) error {
102 accuToGovStaker, err := s.kvStore.GetTree(StoreKeyAccuToGovStaker.String())
103 if err != nil {
104 return err
105 }
106
107 accuToGovStaker.Set(tokenPath, amount)
108
109 return s.kvStore.Set(StoreKeyAccuToGovStaker.String(), accuToGovStaker)
110}
111
112// handle accuToDevOps store data
113func (s *protocolFeeStore) HasAccuToDevOpsStoreKey() bool {
114 return s.kvStore.Has(StoreKeyAccuToDevOps.String())
115}
116
117func (s *protocolFeeStore) InitializeAccuToDevOps() error {
118 return s.kvStore.Set(StoreKeyAccuToDevOps.String(), avl.NewTree())
119}
120
121func (s *protocolFeeStore) GetAccuToDevOps() *avl.Tree {
122 accuToDevOps, err := s.kvStore.GetTree(StoreKeyAccuToDevOps.String())
123 if err != nil {
124 panic(err)
125 }
126
127 return accuToDevOps
128}
129
130func (s *protocolFeeStore) GetAccuToDevOpsItem(tokenPath string) (int64, bool) {
131 accuToDevOps, err := s.kvStore.GetTree(StoreKeyAccuToDevOps.String())
132 if err != nil {
133 panic(err)
134 }
135
136 result, ok := accuToDevOps.Get(tokenPath)
137 if !ok {
138 return 0, false
139 }
140
141 amount, ok := result.(int64)
142 if !ok {
143 panic(ufmt.Errorf("failed to cast result to int64: %T", result))
144 }
145
146 return amount, true
147}
148
149func (s *protocolFeeStore) SetAccuToDevOpsItem(tokenPath string, amount int64) error {
150 accuToDevOps, err := s.kvStore.GetTree(StoreKeyAccuToDevOps.String())
151 if err != nil {
152 return err
153 }
154
155 accuToDevOps.Set(tokenPath, amount)
156
157 return s.kvStore.Set(StoreKeyAccuToDevOps.String(), accuToDevOps)
158}
159
160// handle distributedToGovStakerHistory store data
161func (s *protocolFeeStore) HasDistributedToGovStakerHistoryStoreKey() bool {
162 return s.kvStore.Has(StoreKeyDistributedToGovStakerHistory.String())
163}
164
165func (s *protocolFeeStore) InitializeDistributedToGovStakerHistory() error {
166 return s.kvStore.Set(StoreKeyDistributedToGovStakerHistory.String(), avl.NewTree())
167}
168
169func (s *protocolFeeStore) GetDistributedToGovStakerHistory() *avl.Tree {
170 distributedToGovStakerHistory, err := s.kvStore.GetTree(StoreKeyDistributedToGovStakerHistory.String())
171 if err != nil {
172 panic(err)
173 }
174
175 return distributedToGovStakerHistory
176}
177
178func (s *protocolFeeStore) GetDistributedToGovStakerHistoryItem(tokenPath string) (int64, bool) {
179 distributedToGovStakerHistory, err := s.kvStore.GetTree(StoreKeyDistributedToGovStakerHistory.String())
180 if err != nil {
181 panic(err)
182 }
183
184 result, ok := distributedToGovStakerHistory.Get(tokenPath)
185 if !ok {
186 return 0, false
187 }
188
189 amount, ok := result.(int64)
190 if !ok {
191 panic(ufmt.Errorf("failed to cast result to int64: %T", result))
192 }
193
194 return amount, true
195}
196
197func (s *protocolFeeStore) SetDistributedToGovStakerHistoryItem(tokenPath string, amount int64) error {
198 distributedToGovStakerHistory, err := s.kvStore.GetTree(StoreKeyDistributedToGovStakerHistory.String())
199 if err != nil {
200 return err
201 }
202
203 distributedToGovStakerHistory.Set(tokenPath, amount)
204
205 return s.kvStore.Set(StoreKeyDistributedToGovStakerHistory.String(), distributedToGovStakerHistory)
206}
207
208// handle distributedToDevOpsHistory store data
209func (s *protocolFeeStore) HasDistributedToDevOpsHistoryStoreKey() bool {
210 return s.kvStore.Has(StoreKeyDistributedToDevOpsHistory.String())
211}
212
213func (s *protocolFeeStore) InitializeDistributedToDevOpsHistory() error {
214 return s.kvStore.Set(StoreKeyDistributedToDevOpsHistory.String(), avl.NewTree())
215}
216
217func (s *protocolFeeStore) GetDistributedToDevOpsHistory() *avl.Tree {
218 distributedToDevOpsHistory, err := s.kvStore.GetTree(StoreKeyDistributedToDevOpsHistory.String())
219 if err != nil {
220 panic(err)
221 }
222
223 return distributedToDevOpsHistory
224}
225
226func (s *protocolFeeStore) GetDistributedToDevOpsHistoryItem(tokenPath string) (int64, bool) {
227 distributedToDevOpsHistory, err := s.kvStore.GetTree(StoreKeyDistributedToDevOpsHistory.String())
228 if err != nil {
229 panic(err)
230 }
231
232 result, ok := distributedToDevOpsHistory.Get(tokenPath)
233 if !ok {
234 return 0, false
235 }
236
237 amount, ok := result.(int64)
238 if !ok {
239 panic(ufmt.Errorf("failed to cast result to int64: %T", result))
240 }
241
242 return amount, true
243}
244
245func (s *protocolFeeStore) SetDistributedToDevOpsHistoryItem(tokenPath string, amount int64) error {
246 distributedToDevOpsHistory, err := s.kvStore.GetTree(StoreKeyDistributedToDevOpsHistory.String())
247 if err != nil {
248 return err
249 }
250
251 distributedToDevOpsHistory.Set(tokenPath, amount)
252
253 return s.kvStore.Set(StoreKeyDistributedToDevOpsHistory.String(), distributedToDevOpsHistory)
254}
255
256// handle tokenListWithAmounts store data
257func (s *protocolFeeStore) HasTokenListWithAmountsStoreKey() bool {
258 return s.kvStore.Has(StoreKeyTokenListWithAmounts.String())
259}
260
261func (s *protocolFeeStore) InitializeTokenListWithAmount() error {
262 return s.kvStore.Set(StoreKeyTokenListWithAmounts.String(), make(map[string]int64))
263}
264
265func (s *protocolFeeStore) GetTokenListWithAmounts() map[string]int64 {
266 result, err := s.kvStore.Get(StoreKeyTokenListWithAmounts.String())
267 if err != nil {
268 panic(err)
269 }
270
271 tokenListWithAmounts, ok := result.(map[string]int64)
272 if !ok {
273 panic(ufmt.Errorf("failed to cast result to map[string]int64: %T", result))
274 }
275
276 return tokenListWithAmounts
277}
278
279func (s *protocolFeeStore) SetTokenListWithAmounts(tokenListWithAmounts map[string]int64) error {
280 if tokenListWithAmounts == nil {
281 return errors.New("tokenListWithAmounts is nil")
282 }
283
284 return s.kvStore.Set(StoreKeyTokenListWithAmounts.String(), tokenListWithAmounts)
285}
286
287func (s *protocolFeeStore) GetTokenListWithAmountItem(tokenPath string) (int64, bool) {
288 result, err := s.kvStore.Get(StoreKeyTokenListWithAmounts.String())
289 if err != nil {
290 panic(err)
291 }
292
293 tokenListWithAmounts, ok := result.(map[string]int64)
294 if !ok {
295 panic(ufmt.Errorf("failed to cast result to map[string]int64: %T", result))
296 }
297
298 amount, ok := tokenListWithAmounts[tokenPath]
299 if !ok {
300 return 0, false
301 }
302
303 return amount, true
304}
305
306func (s *protocolFeeStore) SetTokenListWithAmountItem(tokenPath string, amount int64) error {
307 result, err := s.kvStore.Get(StoreKeyTokenListWithAmounts.String())
308 if err != nil {
309 return err
310 }
311
312 tokenListWithAmounts, ok := result.(map[string]int64)
313 if !ok {
314 return ufmt.Errorf("failed to cast result to map[string]int64: %T", result)
315 }
316
317 tokenListWithAmounts[tokenPath] = amount
318
319 return s.kvStore.Set(StoreKeyTokenListWithAmounts.String(), tokenListWithAmounts)
320}
321
322// NewprotocolFeeStore creates a new protocol fee store instance with the provided KV store.
323// This function is used by the upgrade system to create storage instances for each implementation.
324func NewProtocolFeeStore(kvStore store.KVStore) IProtocolFeeStore {
325 return &protocolFeeStore{
326 kvStore: kvStore,
327 }
328}