Search Apps Documentation Source Content File Folder Download Copy Actions Download

store.gno

7.78 Kb ยท 253 lines
  1package pool
  2
  3import (
  4	"gno.land/p/gnoswap/store"
  5	"gno.land/p/nt/avl"
  6	"gno.land/p/nt/ufmt"
  7)
  8
  9// StoreKey defines the keys used for storing pool data in the KV store.
 10// These keys are prefixed with the domain address to ensure namespace isolation.
 11type StoreKey string
 12
 13func (s StoreKey) String() string {
 14	return string(s)
 15}
 16
 17const (
 18	// Pool data storage keys
 19	StoreKeyPools                StoreKey = "pools"                // Map containing all pools
 20	StoreKeyFeeAmountTickSpacing StoreKey = "feeAmountTickSpacing" // Fee tier to tick spacing mapping
 21	StoreKeySlot0FeeProtocol     StoreKey = "slot0FeeProtocol"     // Protocol fee percentage
 22
 23	// Protocol fee storage keys
 24	StoreKeyPoolCreationFee  StoreKey = "poolCreationFee"  // Pool creation fee amount
 25	StoreKeyWithdrawalFeeBPS StoreKey = "withdrawalFeeBPS" // Withdrawal fee in basis points
 26
 27	// Swap hook storage keys
 28	StoreKeySwapStartHook StoreKey = "swapStartHook" // Swap start hook function
 29	StoreKeySwapEndHook   StoreKey = "swapEndHook"   // Swap end hook function
 30	StoreKeyTickCrossHook StoreKey = "tickCrossHook" // Tick cross hook function
 31)
 32
 33// poolStore implements the IPoolStore interface for pool domain storage.
 34// It provides type-safe access to pool data stored in the underlying KV store.
 35type poolStore struct {
 36	kvStore store.KVStore
 37}
 38
 39func (s *poolStore) HasPools() bool {
 40	return s.kvStore.Has(StoreKeyPools.String())
 41}
 42
 43// GetPools retrieves the map containing all pool data.
 44// This is the main data structure that stores all pool instances.
 45func (s *poolStore) GetPools() *avl.Tree {
 46	pools, err := s.kvStore.GetTree(StoreKeyPools.String())
 47	if err != nil {
 48		panic(err)
 49	}
 50
 51	if pools == nil {
 52		panic("pools is nil")
 53	}
 54
 55	return pools
 56}
 57
 58// SetPools stores the map containing all pool data.
 59func (s *poolStore) SetPools(pools *avl.Tree) error {
 60	if pools == nil {
 61		panic("pools is nil")
 62	}
 63
 64	return s.kvStore.Set(StoreKeyPools.String(), pools)
 65}
 66
 67func (s *poolStore) HasFeeAmountTickSpacing() bool {
 68	return s.kvStore.Has(StoreKeyFeeAmountTickSpacing.String())
 69}
 70
 71// GetFeeAmountTickSpacing retrieves the mapping between fee amounts and tick spacing.
 72// This mapping determines the tick spacing for each supported fee tier.
 73func (s *poolStore) GetFeeAmountTickSpacing() map[uint32]int32 {
 74	result, err := s.kvStore.Get(StoreKeyFeeAmountTickSpacing.String())
 75	if err != nil {
 76		panic(err)
 77	}
 78
 79	feeAmountTickSpacing, ok := result.(map[uint32]int32)
 80	if !ok {
 81		panic(ufmt.Sprintf("failed to cast result to map[uint32]int32: %T", result))
 82	}
 83
 84	if feeAmountTickSpacing == nil {
 85		panic("feeAmountTickSpacing is nil")
 86	}
 87
 88	return feeAmountTickSpacing
 89}
 90
 91// SetFeeAmountTickSpacing stores the mapping between fee amounts and tick spacing.
 92func (s *poolStore) SetFeeAmountTickSpacing(feeAmountTickSpacing map[uint32]int32) error {
 93	if feeAmountTickSpacing == nil {
 94		panic("feeAmountTickSpacing is nil")
 95	}
 96
 97	return s.kvStore.Set(StoreKeyFeeAmountTickSpacing.String(), feeAmountTickSpacing)
 98}
 99
100func (s *poolStore) HasSlot0FeeProtocol() bool {
101	return s.kvStore.Has(StoreKeySlot0FeeProtocol.String())
102}
103
104// GetSlot0FeeProtocol retrieves the protocol fee percentage for slot0.
105func (s *poolStore) GetSlot0FeeProtocol() uint8 {
106	result, err := s.kvStore.Get(StoreKeySlot0FeeProtocol.String())
107	if err != nil {
108		panic(err)
109	}
110
111	slot0FeeProtocol, ok := result.(uint8)
112	if !ok {
113		panic(ufmt.Sprintf("failed to cast result to uint8: %T", result))
114	}
115
116	return slot0FeeProtocol
117}
118
119// SetSlot0FeeProtocol stores the protocol fee percentage for slot0.
120func (s *poolStore) SetSlot0FeeProtocol(slot0FeeProtocol uint8) error {
121	return s.kvStore.Set(StoreKeySlot0FeeProtocol.String(), slot0FeeProtocol)
122}
123
124func (s *poolStore) HasPoolCreationFee() bool {
125	return s.kvStore.Has(StoreKeyPoolCreationFee.String())
126}
127
128// GetPoolCreationFee retrieves the pool creation fee amount.
129func (s *poolStore) GetPoolCreationFee() int64 {
130	result, err := s.kvStore.Get(StoreKeyPoolCreationFee.String())
131	if err != nil {
132		panic(err)
133	}
134
135	poolCreationFee, ok := result.(int64)
136	if !ok {
137		panic(ufmt.Sprintf("failed to cast result to int64: %T", result))
138	}
139
140	return poolCreationFee
141}
142
143// SetPoolCreationFee stores the pool creation fee amount.
144func (s *poolStore) SetPoolCreationFee(poolCreationFee int64) error {
145	return s.kvStore.Set(StoreKeyPoolCreationFee.String(), poolCreationFee)
146}
147
148func (s *poolStore) HasWithdrawalFeeBPS() bool {
149	return s.kvStore.Has(StoreKeyWithdrawalFeeBPS.String())
150}
151
152// GetWithdrawalFeeBPS retrieves the withdrawal fee in basis points.
153func (s *poolStore) GetWithdrawalFeeBPS() uint64 {
154	result, err := s.kvStore.Get(StoreKeyWithdrawalFeeBPS.String())
155	if err != nil {
156		panic(err)
157	}
158
159	withdrawalFeeBPS, ok := result.(uint64)
160	if !ok {
161		panic(ufmt.Sprintf("failed to cast result to uint64: %T", result))
162	}
163
164	return withdrawalFeeBPS
165}
166
167// SetWithdrawalFeeBPS stores the withdrawal fee in basis points.
168func (s *poolStore) SetWithdrawalFeeBPS(withdrawalFeeBPS uint64) error {
169	return s.kvStore.Set(StoreKeyWithdrawalFeeBPS.String(), withdrawalFeeBPS)
170}
171
172// HasSwapStartHook checks if the swap start hook is set.
173func (s *poolStore) HasSwapStartHook() bool {
174	return s.kvStore.Has(StoreKeySwapStartHook.String())
175}
176
177// GetSwapStartHook retrieves the swap start hook function.
178func (s *poolStore) GetSwapStartHook() func(cur realm, poolPath string, timestamp int64) {
179	result, err := s.kvStore.Get(StoreKeySwapStartHook.String())
180	if err != nil {
181		panic(err)
182	}
183
184	swapStartHook, ok := result.(func(cur realm, poolPath string, timestamp int64))
185	if !ok {
186		panic(ufmt.Sprintf("failed to cast result to func(poolPath string, timestamp int64): %T", result))
187	}
188
189	return swapStartHook
190}
191
192// SetSwapStartHook stores the swap start hook function.
193func (s *poolStore) SetSwapStartHook(swapStartHook func(cur realm, poolPath string, timestamp int64)) error {
194	return s.kvStore.Set(StoreKeySwapStartHook.String(), swapStartHook)
195}
196
197// HasSwapEndHook checks if the swap end hook is set.
198func (s *poolStore) HasSwapEndHook() bool {
199	return s.kvStore.Has(StoreKeySwapEndHook.String())
200}
201
202// GetSwapEndHook retrieves the swap end hook function.
203func (s *poolStore) GetSwapEndHook() func(cur realm, poolPath string) error {
204	result, err := s.kvStore.Get(StoreKeySwapEndHook.String())
205	if err != nil {
206		panic(err)
207	}
208
209	swapEndHook, ok := result.(func(cur realm, poolPath string) error)
210	if !ok {
211		panic(ufmt.Sprintf("failed to cast result to func(poolPath string): %T", result))
212	}
213
214	return swapEndHook
215}
216
217// SetSwapEndHook stores the swap end hook function.
218func (s *poolStore) SetSwapEndHook(swapEndHook func(cur realm, poolPath string) error) error {
219	return s.kvStore.Set(StoreKeySwapEndHook.String(), swapEndHook)
220}
221
222// HasTickCrossHook checks if the tick cross hook is set.
223func (s *poolStore) HasTickCrossHook() bool {
224	return s.kvStore.Has(StoreKeyTickCrossHook.String())
225}
226
227// GetTickCrossHook retrieves the tick cross hook function.
228func (s *poolStore) GetTickCrossHook() func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64) {
229	result, err := s.kvStore.Get(StoreKeyTickCrossHook.String())
230	if err != nil {
231		panic(err)
232	}
233
234	tickCrossHook, ok := result.(func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64))
235	if !ok {
236		panic(ufmt.Sprintf("failed to cast result to func(poolPath string, tickId int32, zeroForOne bool, timestamp int64): %T", result))
237	}
238
239	return tickCrossHook
240}
241
242// SetTickCrossHook stores the tick cross hook function.
243func (s *poolStore) SetTickCrossHook(tickCrossHook func(cur realm, poolPath string, tickId int32, zeroForOne bool, timestamp int64)) error {
244	return s.kvStore.Set(StoreKeyTickCrossHook.String(), tickCrossHook)
245}
246
247// NewPoolStore creates a new pool store instance with the provided KV store.
248// This function is used by the upgrade system to create storage instances for each implementation.
249func NewPoolStore(kvStore store.KVStore) IPoolStore {
250	return &poolStore{
251		kvStore: kvStore,
252	}
253}