Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

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