Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

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