state.gno
1.53 Kb ยท 75 lines
1package position
2
3import (
4 "chain/runtime"
5 "errors"
6
7 prbac "gno.land/p/gnoswap/rbac"
8 _ "gno.land/r/gnoswap/rbac" // initialize readable contract role(s)
9
10 "gno.land/p/gnoswap/store"
11 "gno.land/p/gnoswap/version_manager"
12
13 "gno.land/r/gnoswap/access"
14)
15
16var (
17 currentAddress = runtime.CurrentRealm().Address()
18 domainPath = runtime.CurrentRealm().PkgPath()
19
20 kvStore store.KVStore
21
22 versionManager version_manager.VersionManager
23 implementation IPosition
24)
25
26func init() {
27 // Create a new KV store instance for this domain
28 kvStore = store.NewKVStore(currentAddress)
29 initRegisterReadableContract()
30
31 // Initialize the initializers map to store implementation registration functions
32 versionManager = version_manager.NewVersionManager(
33 domainPath,
34 kvStore,
35 initializeDomainStore,
36 )
37
38 implementation = nil
39}
40
41func initRegisterReadableContract() {
42 stakerAddr := access.MustGetAddress(prbac.ROLE_STAKER.String())
43
44 if err := kvStore.AddAuthorizedCaller(stakerAddr, store.ReadOnly); err != nil {
45 panic(err)
46 }
47}
48
49func initializeDomainStore(kvStore store.KVStore) any {
50 return NewPositionStore(kvStore)
51}
52
53func getImplementation() IPosition {
54 if implementation == nil {
55 panic("implementation is not initialized")
56 }
57
58 return implementation
59}
60
61func updateImplementation() error {
62 result := versionManager.GetCurrentImplementation()
63 if result == nil {
64 return errors.New("implementation is not initialized")
65 }
66
67 impl, ok := result.(IPosition)
68 if !ok {
69 return errors.New("impl is not an IPosition")
70 }
71
72 implementation = impl
73
74 return nil
75}