Search Apps Documentation Source Content File Folder Download Copy Actions Download

store.gno

3.23 Kb · 143 lines
  1package position
  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
 12type StoreKey string
 13
 14func (s StoreKey) String() string {
 15	return string(s)
 16}
 17
 18const (
 19	StoreKeyPositions      StoreKey = "positions"      // Positions
 20	StoreKeyPositionNextID StoreKey = "positionNextID" // Position next ID
 21)
 22
 23type positionStore struct {
 24	kvStore store.KVStore
 25}
 26
 27func (s *positionStore) HasPositionsStoreKey() bool {
 28	return s.kvStore.Has(StoreKeyPositions.String())
 29}
 30
 31func (s *positionStore) GetPositions() *bptree.BPTree {
 32	result, err := s.kvStore.Get(StoreKeyPositions.String())
 33	if err != nil {
 34		panic(err)
 35	}
 36
 37	positions, ok := result.(*bptree.BPTree)
 38	if !ok {
 39		panic(ufmt.Sprintf("failed to cast result to *bptree.BPTree: %T", result))
 40	}
 41
 42	return positions
 43}
 44
 45func (s *positionStore) SetPositions(_ int, rlm realm, positions *bptree.BPTree) error {
 46	if !rlm.IsCurrent() {
 47		return ErrSpoofedRealm
 48	}
 49
 50	return s.kvStore.Set(0, rlm, StoreKeyPositions.String(), positions)
 51}
 52
 53func (s *positionStore) HasPositionNextIDStoreKey() bool {
 54	return s.kvStore.Has(StoreKeyPositionNextID.String())
 55}
 56
 57func (s *positionStore) GetPositionNextID() uint64 {
 58	result, err := s.kvStore.Get(StoreKeyPositionNextID.String())
 59	if err != nil {
 60		panic(err)
 61	}
 62
 63	nextID, ok := result.(uint64)
 64	if !ok {
 65		panic(ufmt.Sprintf("failed to cast result to uint64: %T", result))
 66	}
 67
 68	return nextID
 69}
 70
 71func (s *positionStore) SetPositionNextID(_ int, rlm realm, nextID uint64) error {
 72	if !rlm.IsCurrent() {
 73		return ErrSpoofedRealm
 74	}
 75
 76	return s.kvStore.Set(0, rlm, StoreKeyPositionNextID.String(), nextID)
 77}
 78
 79func (s *positionStore) HasPosition(positionId uint64) bool {
 80	positions := s.GetPositions()
 81
 82	return positions.Has(uint64ToString(positionId))
 83}
 84
 85func (s *positionStore) GetPosition(positionId uint64) (Position, bool) {
 86	positions := s.GetPositions()
 87
 88	result, ok := positions.Get(uint64ToString(positionId))
 89	if !ok {
 90		return Position{}, false
 91	}
 92
 93	position, ok := result.(Position)
 94	if !ok {
 95		panic(ufmt.Sprintf("failed to cast result to Position: %T", result))
 96	}
 97
 98	return position, true
 99}
100
101func (s *positionStore) SetPosition(_ int, rlm realm, positionId uint64, position Position) error {
102	if !rlm.IsCurrent() {
103		return ErrSpoofedRealm
104	}
105
106	if !s.HasPositionsStoreKey() {
107		return errors.New("positions store key not found")
108	}
109
110	positions := s.GetPositions()
111
112	positions.Set(uint64ToString(positionId), position)
113
114	return s.kvStore.Set(0, rlm, StoreKeyPositions.String(), positions)
115}
116
117func (s *positionStore) RemovePosition(_ int, rlm realm, positionId uint64) error {
118	if !rlm.IsCurrent() {
119		return ErrSpoofedRealm
120	}
121
122	if !s.HasPositionsStoreKey() {
123		return errors.New("positions store key not found")
124	}
125
126	positions := s.GetPositions()
127
128	positions.Remove(uint64ToString(positionId))
129
130	return s.kvStore.Set(0, rlm, StoreKeyPositions.String(), positions)
131}
132
133// NewPositionStore creates a new protocol fee store instance with the provided KV store.
134// This function is used by the upgrade system to create storage instances for each implementation.
135func NewPositionStore(kvStore store.KVStore) IPositionStore {
136	return &positionStore{
137		kvStore: kvStore,
138	}
139}
140
141func uint64ToString(id uint64) string {
142	return strconv.FormatUint(id, 10)
143}