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

4.36 Kb · 160 lines
  1package limit
  2
  3import (
  4	"errors"
  5
  6	plimit "gno.land/p/gnoswap/order/limit/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/limit 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 limit order 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 plimit key helpers.
 27//
 28//	orders         OrderKey -> *Order, pending orders only
 29//	ordersByOwner  OwnerOrderKey -> orderID, pending orders only
 30//	pendingExecutionFees tokenPath -> int64, held until the protocol fee collector accepts them
 31//	nextOrderID    uint64
 32//	config         *Config
 33const (
 34	StoreKeyOrders               StoreKey = "orders"
 35	StoreKeyOrdersByOwner        StoreKey = "ordersByOwner"
 36	StoreKeyPendingExecutionFees StoreKey = "pendingExecutionFees"
 37	StoreKeyNextOrderID          StoreKey = "nextOrderID"
 38	StoreKeyConfig               StoreKey = "config"
 39)
 40
 41type limitOrderStore struct {
 42	kvStore store.KVStore
 43}
 44
 45// NewLimitOrderStore wraps the domain KV store.
 46func NewLimitOrderStore(kvStore store.KVStore) plimit.ILimitOrderStore {
 47	return &limitOrderStore{kvStore: kvStore}
 48}
 49
 50// set writes key after checking that rlm is the current realm.
 51// The KV store additionally enforces its write ACL for code realms.
 52func (s *limitOrderStore) set(_ int, rlm realm, key string, value any) error {
 53	if !rlm.IsCurrent() {
 54		return errors.New(ErrSpoofedRealm)
 55	}
 56
 57	return s.kvStore.Set(0, rlm, key, value)
 58}
 59
 60func (s *limitOrderStore) getTree(key StoreKey) *bptree.BPTree {
 61	tree, err := s.kvStore.GetBPTree(key.String())
 62	if err != nil {
 63		panic(err.Error())
 64	}
 65
 66	if tree == nil {
 67		panic(ufmt.Sprintf(errNilValue, key.String()))
 68	}
 69
 70	return tree
 71}
 72
 73func (s *limitOrderStore) setTree(_ int, rlm realm, key StoreKey, tree *bptree.BPTree) error {
 74	if !rlm.IsCurrent() {
 75		return errors.New(ErrSpoofedRealm)
 76	}
 77
 78	if tree == nil {
 79		return ufmt.Errorf(errNilValue, key.String())
 80	}
 81
 82	return s.kvStore.Set(0, rlm, key.String(), tree)
 83}
 84
 85func (s *limitOrderStore) HasOrders() bool { return s.kvStore.Has(StoreKeyOrders.String()) }
 86
 87func (s *limitOrderStore) GetOrders() *bptree.BPTree { return s.getTree(StoreKeyOrders) }
 88
 89func (s *limitOrderStore) SetOrders(_ int, rlm realm, orders *bptree.BPTree) error {
 90	return s.setTree(0, rlm, StoreKeyOrders, orders)
 91}
 92
 93func (s *limitOrderStore) HasOrdersByOwner() bool {
 94	return s.kvStore.Has(StoreKeyOrdersByOwner.String())
 95}
 96
 97func (s *limitOrderStore) GetOrdersByOwner() *bptree.BPTree {
 98	return s.getTree(StoreKeyOrdersByOwner)
 99}
100
101func (s *limitOrderStore) SetOrdersByOwner(_ int, rlm realm, ordersByOwner *bptree.BPTree) error {
102	return s.setTree(0, rlm, StoreKeyOrdersByOwner, ordersByOwner)
103}
104
105func (s *limitOrderStore) HasPendingExecutionFees() bool {
106	return s.kvStore.Has(StoreKeyPendingExecutionFees.String())
107}
108
109func (s *limitOrderStore) GetPendingExecutionFees() *bptree.BPTree {
110	return s.getTree(StoreKeyPendingExecutionFees)
111}
112
113func (s *limitOrderStore) SetPendingExecutionFees(_ int, rlm realm, pendingExecutionFees *bptree.BPTree) error {
114	return s.setTree(0, rlm, StoreKeyPendingExecutionFees, pendingExecutionFees)
115}
116
117func (s *limitOrderStore) HasNextOrderID() bool {
118	return s.kvStore.Has(StoreKeyNextOrderID.String())
119}
120
121func (s *limitOrderStore) GetNextOrderID() uint64 {
122	id, err := s.kvStore.GetUint64(StoreKeyNextOrderID.String())
123	if err != nil {
124		panic(err.Error())
125	}
126
127	return id
128}
129
130func (s *limitOrderStore) SetNextOrderID(_ int, rlm realm, id uint64) error {
131	return s.set(0, rlm, StoreKeyNextOrderID.String(), id)
132}
133
134func (s *limitOrderStore) HasConfig() bool { return s.kvStore.Has(StoreKeyConfig.String()) }
135
136func (s *limitOrderStore) GetConfig() *plimit.Config {
137	result, err := s.kvStore.Get(StoreKeyConfig.String())
138	if err != nil {
139		panic(err.Error())
140	}
141
142	config, ok := result.(*plimit.Config)
143	if !ok || config == nil {
144		panic(ufmt.Sprintf(errInvalidConfigType, result))
145	}
146
147	return config
148}
149
150func (s *limitOrderStore) SetConfig(_ int, rlm realm, config *plimit.Config) error {
151	if !rlm.IsCurrent() {
152		return errors.New(ErrSpoofedRealm)
153	}
154
155	if config == nil {
156		return errors.New(errNilConfig)
157	}
158
159	return s.kvStore.Set(0, rlm, StoreKeyConfig.String(), config)
160}