Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

1.43 Kb · 62 lines
 1package staker
 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
12var (
13	// Package-init reads use chain/runtime/unsafe.CurrentRealm because there
14	// is no `cur realm` in scope at package-initialization time. The values
15	// captured here describe the gov/staker realm itself, not any caller.
16	currentAddress address
17	domainPath     = "gno.land/r/gnoswap/gov/staker"
18
19	kvStore store.KVStore
20
21	versionManager version_manager.VersionManager
22	implementation IGovStaker
23)
24
25func init(cur realm) {
26	currentAddress = cur.Address()
27
28	// Initialize KVStore
29	kvStore = store.NewKVStore(currentAddress)
30
31	// Create VersionManager
32	versionManager = version_manager.NewVersionManager(domainPath, kvStore, initializeDomainStore)
33	implementation = nil
34}
35
36func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
37	return NewGovStakerStore(kvStore)
38}
39
40// getImplementation returns the current active implementation
41func getImplementation() IGovStaker {
42	if implementation == nil {
43		panic("implementation is not initialized")
44	}
45	return implementation
46}
47
48func updateImplementation() error {
49	result := versionManager.GetCurrentImplementation()
50	if result == nil {
51		return errors.New("implementation is not initialized")
52	}
53
54	impl, ok := result.(IGovStaker)
55	if !ok {
56		return errors.New("impl is not an IGovStaker")
57	}
58
59	implementation = impl
60
61	return nil
62}