Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

state.gno

1.38 Kb · 63 lines
 1package governance
 2
 3import (
 4	"errors"
 5
 6	_ "gno.land/r/gnoswap/rbac" // initialize readable contract role(s)
 7
 8	"gno.land/p/gnoswap/store"
 9	"gno.land/p/gnoswap/version_manager"
10)
11
12const ErrSpoofedRealm = "rlm does not match the current crossing frame"
13
14var (
15	// currentAddress and domainPath describe the governance realm itself.
16	// They are captured from the `cur realm` parameter in init(cur realm),
17	// which resolves to this governance domain realm.
18	currentAddress address
19	domainPath     string
20
21	kvStore store.KVStore
22
23	versionManager version_manager.VersionManager
24	implementation IGovernance
25)
26
27func init(cur realm) {
28	currentAddress = cur.Address()
29	domainPath = cur.PkgPath()
30
31	kvStore = store.NewKVStore(currentAddress)
32	versionManager = version_manager.NewVersionManager(domainPath, kvStore, initializeDomainStore)
33
34	implementation = nil
35}
36
37func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
38	return NewGovernanceStore(kvStore)
39}
40
41func getImplementation() IGovernance {
42	if implementation == nil {
43		panic("implementation is not initialized")
44	}
45
46	return implementation
47}
48
49func updateImplementation() error {
50	result := versionManager.GetCurrentImplementation()
51	if result == nil {
52		return errors.New("implementation is not initialized")
53	}
54
55	impl, ok := result.(IGovernance)
56	if !ok {
57		return errors.New("impl is not an IGovernance")
58	}
59
60	implementation = impl
61
62	return nil
63}