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.16 Kb · 64 lines
 1package common
 2
 3import (
 4	"errors"
 5
 6	_ "gno.land/r/gnoswap/rbac/v1" // initialize readable contract role(s)
 7
 8	"gno.land/p/gnoswap/store/v1"
 9	"gno.land/p/gnoswap/version_manager/v1"
10)
11
12var (
13	currentAddress address
14	domainPath     string
15
16	kvStore store.KVStore
17
18	versionManager version_manager.VersionManager
19	implementation ICommon
20)
21
22func init(cur realm) {
23	currentAddress = cur.Address()
24	domainPath = cur.PkgPath()
25
26	kvStore = store.NewKVStore(currentAddress)
27
28	versionManager = version_manager.NewVersionManager(
29		domainPath,
30		kvStore,
31		initializeDomainStore,
32	)
33
34	implementation = nil
35}
36
37// common keeps no domain state; implementations only resolve tokens.
38func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
39	return nil
40}
41
42func getImplementation() ICommon {
43	if implementation == nil {
44		panic("implementation is not initialized")
45	}
46
47	return implementation
48}
49
50func updateImplementation() error {
51	result := versionManager.GetCurrentImplementation()
52	if result == nil {
53		return errors.New("implementation is not initialized")
54	}
55
56	impl, ok := result.(ICommon)
57	if !ok {
58		return errors.New("impl is not an ICommon")
59	}
60
61	implementation = impl
62
63	return nil
64}