Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

1.51 Kb · 73 lines
 1package staker
 2
 3import (
 4	"errors"
 5
 6	prbac "gno.land/p/gnoswap/rbac"
 7	_ "gno.land/r/gnoswap/rbac" // initialize readable contract role(s)
 8
 9	"gno.land/p/gnoswap/store"
10	"gno.land/p/gnoswap/version_manager"
11
12	"gno.land/r/gnoswap/access"
13)
14
15var (
16	ErrSpoofedRealm = errors.New("rlm does not match the current crossing frame")
17
18	kvStore store.KVStore
19
20	versionManager version_manager.VersionManager
21	implementation IStaker
22)
23
24func init(cur realm) {
25	// Create a new KV store instance for this domain
26	kvStore = store.NewKVStore(cur.Address())
27	initRegisterWritableContract(cur)
28
29	// Initialize the initializers map to store implementation registration functions
30	versionManager = version_manager.NewVersionManager(
31		cur.PkgPath(),
32		kvStore,
33		initializeDomainStore,
34	)
35
36	implementation = nil
37}
38
39func initRegisterWritableContract(cur realm) {
40	poolAddr := access.MustGetAddress(prbac.ROLE_POOL.String())
41
42	if err := kvStore.AddAuthorizedCaller(0, cur, poolAddr, store.Write); err != nil {
43		panic(err)
44	}
45}
46
47func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
48	return NewStakerStore(kvStore)
49}
50
51func getImplementation() IStaker {
52	if implementation == nil {
53		panic("implementation is not initialized")
54	}
55
56	return implementation
57}
58
59func updateImplementation() error {
60	result := versionManager.GetCurrentImplementation()
61	if result == nil {
62		return errors.New("implementation is not initialized")
63	}
64
65	impl, ok := result.(IStaker)
66	if !ok {
67		return errors.New("impl is not an IStaker")
68	}
69
70	implementation = impl
71
72	return nil
73}