state.gno
1.41 Kb ยท 69 lines
1package governance
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 IGovernance
24)
25
26func init() {
27 kvStore = store.NewKVStore(currentAddress)
28 initRegisterReadableContract()
29 versionManager = version_manager.NewVersionManager(domainPath, kvStore, initializeDomainStore)
30
31 implementation = nil
32}
33
34func initializeDomainStore(kvStore store.KVStore) any {
35 return NewGovernanceStore(kvStore)
36}
37
38func initRegisterReadableContract() {
39 govStakerAddress := access.MustGetAddress(prbac.ROLE_GOV_STAKER.String())
40
41 err := kvStore.AddAuthorizedCaller(govStakerAddress, store.ReadOnly)
42 if err != nil {
43 panic(err)
44 }
45}
46
47func getImplementation() IGovernance {
48 if implementation == nil {
49 panic("implementation is not initialized")
50 }
51
52 return implementation
53}
54
55func updateImplementation() error {
56 result := versionManager.GetCurrentImplementation()
57 if result == nil {
58 return errors.New("implementation is not initialized")
59 }
60
61 impl, ok := result.(IGovernance)
62 if !ok {
63 return errors.New("impl is not an IGovernance")
64 }
65
66 implementation = impl
67
68 return nil
69}