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

store.gno

8.40 Kb · 269 lines
  1package dca
  2
  3import (
  4	"errors"
  5
  6	pdca "gno.land/p/gnoswap/order/dca/v0"
  7	"gno.land/p/gnoswap/store/v1"
  8	bptree "gno.land/p/nt/bptree/v0"
  9	ufmt "gno.land/p/nt/ufmt/v0"
 10)
 11
 12// NewBPTreeN allocates an empty B+tree in the order/dca realm.
 13// Implementations create the store trees through it,
 14// so the trees are owned by the realm that stores them.
 15func NewBPTreeN(fanout int) *bptree.BPTree {
 16	return bptree.NewBPTreeN(fanout)
 17}
 18
 19// StoreKey is a key of the DCA KV store.
 20type StoreKey string
 21
 22func (s StoreKey) String() string {
 23	return string(s)
 24}
 25
 26// Store layout. Tree keys are built by the pdca key helpers.
 27//
 28//	orders               OrderKey -> *Order
 29//	ordersByOwner        OwnerOrderKey -> orderID
 30//	groups               groupID -> *Group
 31//	scheduledGroups      ScheduleKey -> groupID, groups with orderAmount > 0 only
 32//	nextOrderID          uint64
 33//	config               *Config
 34//	executing            true while ExecuteGroup is inside its pool swap
 35//	ordersByGroup        GroupOrderKey -> orderID, active orders only
 36//	pendingExecutionFees  tokenPath -> int64, fees not yet sent to protocol_fee
 37//	executionLogs        ExecutionLogKey -> lastRound uint64, unreleased logs of active orders
 38//	orderAmountsByGroup  GroupOrderAmountKey -> endRound, orders by executionFeeRate, then amountPerExecution
 39//	tokenMinAmountsPerExecution  tokenPath -> int64, per-token minimum input per execution
 40//	pairPriceLimits      PairKey -> *PairPriceLimit, minimum pool output per input of a pair direction
 41const (
 42	StoreKeyOrders               StoreKey = "orders"
 43	StoreKeyOrdersByOwner        StoreKey = "ordersByOwner"
 44	StoreKeyGroups               StoreKey = "groups"
 45	StoreKeyScheduledGroups      StoreKey = "scheduledGroups"
 46	StoreKeyNextOrderID          StoreKey = "nextOrderID"
 47	StoreKeyConfig               StoreKey = "config"
 48	StoreKeyExecuting            StoreKey = "executing"
 49	StoreKeyOrdersByGroup        StoreKey = "ordersByGroup"
 50	StoreKeyPendingExecutionFees StoreKey = "pendingExecutionFees"
 51	StoreKeyExecutionLogs        StoreKey = "executionLogs"
 52	StoreKeyOrderAmountsByGroup  StoreKey = "orderAmountsByGroup"
 53
 54	StoreKeyTokenMinAmountsPerExecution StoreKey = "tokenMinAmountsPerExecution"
 55	StoreKeyPairPriceLimits             StoreKey = "pairPriceLimits"
 56)
 57
 58type dcaOrderStore struct {
 59	kvStore store.KVStore
 60}
 61
 62// NewDCAOrderStore wraps the domain KV store.
 63func NewDCAOrderStore(kvStore store.KVStore) pdca.IDCAOrderStore {
 64	return &dcaOrderStore{kvStore: kvStore}
 65}
 66
 67func (s *dcaOrderStore) getTree(key StoreKey) *bptree.BPTree {
 68	tree, err := s.kvStore.GetBPTree(key.String())
 69	if err != nil {
 70		panic(err.Error())
 71	}
 72
 73	if tree == nil {
 74		panic(ufmt.Sprintf(errNilValue, key.String()))
 75	}
 76
 77	return tree
 78}
 79
 80func (s *dcaOrderStore) setTree(_ int, rlm realm, key StoreKey, tree *bptree.BPTree) error {
 81	if !rlm.IsCurrent() {
 82		return errors.New(ErrSpoofedRealm)
 83	}
 84
 85	if tree == nil {
 86		return ufmt.Errorf(errNilValue, key.String())
 87	}
 88
 89	return s.kvStore.Set(0, rlm, key.String(), tree)
 90}
 91
 92func (s *dcaOrderStore) HasOrders() bool { return s.kvStore.Has(StoreKeyOrders.String()) }
 93
 94func (s *dcaOrderStore) GetOrders() *bptree.BPTree { return s.getTree(StoreKeyOrders) }
 95
 96func (s *dcaOrderStore) SetOrders(_ int, rlm realm, orders *bptree.BPTree) error {
 97	return s.setTree(0, rlm, StoreKeyOrders, orders)
 98}
 99
100func (s *dcaOrderStore) HasOrdersByOwner() bool {
101	return s.kvStore.Has(StoreKeyOrdersByOwner.String())
102}
103
104func (s *dcaOrderStore) GetOrdersByOwner() *bptree.BPTree { return s.getTree(StoreKeyOrdersByOwner) }
105
106func (s *dcaOrderStore) SetOrdersByOwner(_ int, rlm realm, ordersByOwner *bptree.BPTree) error {
107	return s.setTree(0, rlm, StoreKeyOrdersByOwner, ordersByOwner)
108}
109
110func (s *dcaOrderStore) HasOrdersByGroup() bool {
111	return s.kvStore.Has(StoreKeyOrdersByGroup.String())
112}
113
114func (s *dcaOrderStore) GetOrdersByGroup() *bptree.BPTree { return s.getTree(StoreKeyOrdersByGroup) }
115
116func (s *dcaOrderStore) SetOrdersByGroup(_ int, rlm realm, ordersByGroup *bptree.BPTree) error {
117	return s.setTree(0, rlm, StoreKeyOrdersByGroup, ordersByGroup)
118}
119
120func (s *dcaOrderStore) HasGroups() bool { return s.kvStore.Has(StoreKeyGroups.String()) }
121
122func (s *dcaOrderStore) GetGroups() *bptree.BPTree { return s.getTree(StoreKeyGroups) }
123
124func (s *dcaOrderStore) SetGroups(_ int, rlm realm, groups *bptree.BPTree) error {
125	return s.setTree(0, rlm, StoreKeyGroups, groups)
126}
127
128func (s *dcaOrderStore) HasScheduledGroups() bool {
129	return s.kvStore.Has(StoreKeyScheduledGroups.String())
130}
131
132func (s *dcaOrderStore) GetScheduledGroups() *bptree.BPTree {
133	return s.getTree(StoreKeyScheduledGroups)
134}
135
136func (s *dcaOrderStore) SetScheduledGroups(_ int, rlm realm, groups *bptree.BPTree) error {
137	return s.setTree(0, rlm, StoreKeyScheduledGroups, groups)
138}
139
140func (s *dcaOrderStore) HasNextOrderID() bool { return s.kvStore.Has(StoreKeyNextOrderID.String()) }
141
142func (s *dcaOrderStore) GetNextOrderID() uint64 {
143	id, err := s.kvStore.GetUint64(StoreKeyNextOrderID.String())
144	if err != nil {
145		panic(err.Error())
146	}
147
148	return id
149}
150
151func (s *dcaOrderStore) SetNextOrderID(_ int, rlm realm, id uint64) error {
152	if !rlm.IsCurrent() {
153		return errors.New(ErrSpoofedRealm)
154	}
155
156	return s.kvStore.Set(0, rlm, StoreKeyNextOrderID.String(), id)
157}
158
159func (s *dcaOrderStore) HasConfig() bool { return s.kvStore.Has(StoreKeyConfig.String()) }
160
161func (s *dcaOrderStore) GetConfig() *pdca.Config {
162	result, err := s.kvStore.Get(StoreKeyConfig.String())
163	if err != nil {
164		panic(err.Error())
165	}
166
167	config, ok := result.(*pdca.Config)
168	if !ok || config == nil {
169		panic(ufmt.Sprintf(errInvalidConfigType, result))
170	}
171
172	return config
173}
174
175func (s *dcaOrderStore) SetConfig(_ int, rlm realm, config *pdca.Config) error {
176	if !rlm.IsCurrent() {
177		return errors.New(ErrSpoofedRealm)
178	}
179
180	if config == nil {
181		return errors.New(errNilConfig)
182	}
183
184	return s.kvStore.Set(0, rlm, StoreKeyConfig.String(), config)
185}
186
187// IsExecuting reports whether a group execution is inside its swap.
188// The flag is in the proxy store,
189// so every version and UpgradeImpl share the same re-entrancy guard.
190func (s *dcaOrderStore) IsExecuting() bool {
191	if !s.kvStore.Has(StoreKeyExecuting.String()) {
192		return false
193	}
194
195	executing, err := s.kvStore.GetBool(StoreKeyExecuting.String())
196	if err != nil {
197		panic(err.Error())
198	}
199
200	return executing
201}
202
203func (s *dcaOrderStore) SetExecuting(_ int, rlm realm, executing bool) error {
204	if !rlm.IsCurrent() {
205		return errors.New(ErrSpoofedRealm)
206	}
207
208	return s.kvStore.Set(0, rlm, StoreKeyExecuting.String(), executing)
209}
210
211func (s *dcaOrderStore) HasPendingExecutionFees() bool {
212	return s.kvStore.Has(StoreKeyPendingExecutionFees.String())
213}
214
215func (s *dcaOrderStore) GetPendingExecutionFees() *bptree.BPTree {
216	return s.getTree(StoreKeyPendingExecutionFees)
217}
218
219func (s *dcaOrderStore) SetPendingExecutionFees(_ int, rlm realm, pendingExecutionFees *bptree.BPTree) error {
220	return s.setTree(0, rlm, StoreKeyPendingExecutionFees, pendingExecutionFees)
221}
222
223func (s *dcaOrderStore) HasExecutionLogs() bool {
224	return s.kvStore.Has(StoreKeyExecutionLogs.String())
225}
226
227func (s *dcaOrderStore) GetExecutionLogs() *bptree.BPTree {
228	return s.getTree(StoreKeyExecutionLogs)
229}
230
231func (s *dcaOrderStore) SetExecutionLogs(_ int, rlm realm, executionLogs *bptree.BPTree) error {
232	return s.setTree(0, rlm, StoreKeyExecutionLogs, executionLogs)
233}
234
235func (s *dcaOrderStore) HasOrderAmountsByGroup() bool {
236	return s.kvStore.Has(StoreKeyOrderAmountsByGroup.String())
237}
238
239func (s *dcaOrderStore) GetOrderAmountsByGroup() *bptree.BPTree {
240	return s.getTree(StoreKeyOrderAmountsByGroup)
241}
242
243func (s *dcaOrderStore) SetOrderAmountsByGroup(_ int, rlm realm, orderAmountsByGroup *bptree.BPTree) error {
244	return s.setTree(0, rlm, StoreKeyOrderAmountsByGroup, orderAmountsByGroup)
245}
246
247func (s *dcaOrderStore) HasTokenMinAmountsPerExecution() bool {
248	return s.kvStore.Has(StoreKeyTokenMinAmountsPerExecution.String())
249}
250
251func (s *dcaOrderStore) GetTokenMinAmountsPerExecution() *bptree.BPTree {
252	return s.getTree(StoreKeyTokenMinAmountsPerExecution)
253}
254
255func (s *dcaOrderStore) SetTokenMinAmountsPerExecution(_ int, rlm realm, tokenMinAmounts *bptree.BPTree) error {
256	return s.setTree(0, rlm, StoreKeyTokenMinAmountsPerExecution, tokenMinAmounts)
257}
258
259func (s *dcaOrderStore) HasPairPriceLimits() bool {
260	return s.kvStore.Has(StoreKeyPairPriceLimits.String())
261}
262
263func (s *dcaOrderStore) GetPairPriceLimits() *bptree.BPTree {
264	return s.getTree(StoreKeyPairPriceLimits)
265}
266
267func (s *dcaOrderStore) SetPairPriceLimits(_ int, rlm realm, pairPriceLimits *bptree.BPTree) error {
268	return s.setTree(0, rlm, StoreKeyPairPriceLimits, pairPriceLimits)
269}