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