init.gno
0.90 Kb · 46 lines
1package position
2
3import (
4 "errors"
5 "gno.land/r/gnoswap/position"
6)
7
8func init(cur realm) {
9 registerPositionV1(cur)
10}
11
12func registerPositionV1(cur realm) {
13 position.RegisterInitializer(
14 cross(cur),
15 func(_ int, rlm realm, positionStore position.IPositionStore) position.IPosition {
16 if !rlm.IsCurrent() {
17 panic(errors.New(errSpoofedRealm))
18 }
19
20 err := initStoreData(0, rlm, positionStore)
21 if err != nil {
22 panic(err)
23 }
24
25 return NewPositionV1(positionStore, newGNFTAccessor())
26 },
27 )
28}
29
30func initStoreData(_ int, rlm realm, positionStore position.IPositionStore) error {
31 if !positionStore.HasPositionNextIDStoreKey() {
32 err := positionStore.SetPositionNextID(0, rlm, uint64(1))
33 if err != nil {
34 return err
35 }
36 }
37
38 if !positionStore.HasPositionsStoreKey() {
39 err := positionStore.SetPositions(0, rlm, position.NewPositionsTree())
40 if err != nil {
41 return err
42 }
43 }
44
45 return nil
46}