Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

1.24 Kb ยท 63 lines
 1package launchpad
 2
 3import (
 4	"chain/runtime"
 5	"errors"
 6
 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
13var (
14	currentAddress = runtime.CurrentRealm().Address()
15	domainPath     = runtime.CurrentRealm().PkgPath()
16
17	kvStore store.KVStore
18
19	versionManager version_manager.VersionManager
20	implementation ILaunchpad
21)
22
23func init() {
24	// Create a new KV store instance for this domain
25	kvStore = store.NewKVStore(currentAddress)
26
27	// Initialize the initializers map to store implementation registration functions
28	versionManager = version_manager.NewVersionManager(
29		domainPath,
30		kvStore,
31		initializeDomainStore,
32	)
33
34	implementation = nil
35}
36
37func initializeDomainStore(kvStore store.KVStore) any {
38	return NewLaunchpadStore(kvStore)
39}
40
41func getImplementation() ILaunchpad {
42	if implementation == nil {
43		panic("implementation is not initialized")
44	}
45
46	return implementation
47}
48
49func updateImplementation() error {
50	result := versionManager.GetCurrentImplementation()
51	if result == nil {
52		return errors.New("implementation is not initialized")
53	}
54
55	impl, ok := result.(ILaunchpad)
56	if !ok {
57		return errors.New("impl is not an ILaunchpad")
58	}
59
60	implementation = impl
61
62	return nil
63}