store.gno
5.68 Kb · 195 lines
1package launchpad
2
3import (
4 "errors"
5 "strconv"
6
7 "gno.land/p/gnoswap/store"
8 bptree "gno.land/p/nt/bptree/v0"
9 ufmt "gno.land/p/nt/ufmt/v0"
10)
11
12// NewBPTreeN allocates a BP-tree under /r/gnoswap/launchpad's realm context
13// (the realm that declares Project/Deposit/RewardManager). The tree's PkgID is
14// therefore /r/gnoswap/launchpad, matching the domain values it stores, so
15// tree.Set leaf-slot writes clear the readonly-taint gate regardless of which
16// realm (launchpad/v1, mock, tests) calls Set. Implementations, mocks, and
17// tests must allocate launchpad trees through here rather than calling
18// bptree.NewBPTreeN directly in their own realm.
19func NewBPTreeN(fanout int) *bptree.BPTree {
20 return bptree.NewBPTreeN(fanout)
21}
22
23type StoreKey string
24
25func (s StoreKey) String() string {
26 return string(s)
27}
28
29const (
30 StoreKeyProjects StoreKey = "projects" // Projects tree
31 StoreKeyProjectTierRewardManagers StoreKey = "projectTierRewardManagers" // Project tier reward managers tree
32 StoreKeyDepositCounter StoreKey = "depositCounter" // Deposit counter
33 StoreKeyDeposits StoreKey = "deposits" // Deposits tree
34 StoreKeyTotalGNSStakedAmount StoreKey = "totalGNSStakedAmount" // Total active launchpad GNS stake
35)
36
37type launchpadStore struct {
38 kvStore store.KVStore
39}
40
41// HasProjectsKey checks if the projects key exists in the store.
42func (s *launchpadStore) HasProjectsKey() bool {
43 return s.kvStore.Has(StoreKeyProjects.String())
44}
45
46// GetProjects retrieves the projects tree.
47func (s *launchpadStore) GetProjects() *bptree.BPTree {
48 result, err := s.kvStore.Get(StoreKeyProjects.String())
49 if err != nil {
50 panic(err)
51 }
52
53 projects, ok := result.(*bptree.BPTree)
54 if !ok {
55 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
56 }
57
58 return projects
59}
60
61// SetProjects stores the projects tree.
62func (s *launchpadStore) SetProjects(_ int, rlm realm, projects *bptree.BPTree) error {
63 if !rlm.IsCurrent() {
64 return errors.New(ErrSpoofedRealm)
65 }
66
67 return s.kvStore.Set(0, rlm, StoreKeyProjects.String(), projects)
68}
69
70// HasProjectTierRewardManagersKey checks if the project tier reward managers key exists in the store.
71func (s *launchpadStore) HasProjectTierRewardManagersKey() bool {
72 return s.kvStore.Has(StoreKeyProjectTierRewardManagers.String())
73}
74
75// GetProjectTierRewardManagers retrieves the project tier reward managers tree.
76func (s *launchpadStore) GetProjectTierRewardManagers() *bptree.BPTree {
77 result, err := s.kvStore.Get(StoreKeyProjectTierRewardManagers.String())
78 if err != nil {
79 panic(err)
80 }
81
82 managers, ok := result.(*bptree.BPTree)
83 if !ok {
84 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
85 }
86
87 return managers
88}
89
90// SetProjectTierRewardManagers stores the project tier reward managers tree.
91func (s *launchpadStore) SetProjectTierRewardManagers(_ int, rlm realm, managers *bptree.BPTree) error {
92 if !rlm.IsCurrent() {
93 return errors.New(ErrSpoofedRealm)
94 }
95
96 return s.kvStore.Set(0, rlm, StoreKeyProjectTierRewardManagers.String(), managers)
97}
98
99// HasDepositCounterStoreKey checks if the deposit counter key exists in the store.
100func (s *launchpadStore) HasDepositCounterStoreKey() bool {
101 return s.kvStore.Has(StoreKeyDepositCounter.String())
102}
103
104// GetDepositCounter retrieves the deposit counter.
105func (s *launchpadStore) GetDepositCounter() *Counter {
106 result, err := s.kvStore.Get(StoreKeyDepositCounter.String())
107 if err != nil {
108 panic(err)
109 }
110
111 counter, ok := result.(*Counter)
112 if !ok {
113 panic(ufmt.Sprintf("failed to cast result to Counter: %T", result))
114 }
115
116 return counter
117}
118
119func (s *launchpadStore) SetDepositCounter(_ int, rlm realm, counter *Counter) error {
120 if !rlm.IsCurrent() {
121 return errors.New(ErrSpoofedRealm)
122 }
123
124 return s.kvStore.Set(0, rlm, StoreKeyDepositCounter.String(), counter)
125}
126
127func (s *launchpadStore) NextDepositID() string {
128 counter := s.GetDepositCounter()
129
130 return strconv.FormatInt(counter.Next(), 10)
131}
132
133// SetDepositCounter stores the deposit counter.
134// HasDepositsKey checks if the deposits key exists in the store.
135func (s *launchpadStore) HasDepositsKey() bool {
136 return s.kvStore.Has(StoreKeyDeposits.String())
137}
138
139// GetDeposits retrieves the deposits tree.
140func (s *launchpadStore) GetDeposits() *bptree.BPTree {
141 result, err := s.kvStore.Get(StoreKeyDeposits.String())
142 if err != nil {
143 panic(err)
144 }
145
146 deposits, ok := result.(*bptree.BPTree)
147 if !ok {
148 panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
149 }
150
151 return deposits
152}
153
154// SetDeposits stores the deposits tree.
155func (s *launchpadStore) SetDeposits(_ int, rlm realm, deposits *bptree.BPTree) error {
156 if !rlm.IsCurrent() {
157 return errors.New(ErrSpoofedRealm)
158 }
159
160 return s.kvStore.Set(0, rlm, StoreKeyDeposits.String(), deposits)
161}
162
163func (s *launchpadStore) HasTotalGNSStakedAmountKey() bool {
164 return s.kvStore.Has(StoreKeyTotalGNSStakedAmount.String())
165}
166
167func (s *launchpadStore) GetTotalGNSStakedAmount() int64 {
168 result, err := s.kvStore.Get(StoreKeyTotalGNSStakedAmount.String())
169 if err != nil {
170 panic(err)
171 }
172
173 amount, ok := result.(int64)
174 if !ok {
175 panic(ufmt.Sprintf("failed to cast result to int64: %T", result))
176 }
177
178 return amount
179}
180
181func (s *launchpadStore) SetTotalGNSStakedAmount(_ int, rlm realm, amount int64) error {
182 if !rlm.IsCurrent() {
183 return errors.New(ErrSpoofedRealm)
184 }
185
186 return s.kvStore.Set(0, rlm, StoreKeyTotalGNSStakedAmount.String(), amount)
187}
188
189// NewLaunchpadStore creates a new launchpad store instance with the provided KV store.
190// This function is used by the upgrade system to create storage instances for each implementation.
191func NewLaunchpadStore(kvStore store.KVStore) ILaunchpadStore {
192 return &launchpadStore{
193 kvStore: kvStore,
194 }
195}