Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

state.gno

1.20 Kb · 61 lines
 1package position
 2
 3import (
 4	"errors"
 5
 6	_ "gno.land/r/gnoswap/rbac/v1" // initialize readable contract role(s)
 7
 8	"gno.land/p/gnoswap/store/v1"
 9	"gno.land/p/gnoswap/version_manager/v1"
10)
11
12var (
13	domainPath = "gno.land/r/gnoswap/position"
14
15	kvStore store.KVStore
16
17	versionManager version_manager.VersionManager
18	implementation IPosition
19)
20
21func init(cur realm) {
22	// Create a new KV store instance for this domain
23	kvStore = store.NewKVStore(cur.Address())
24
25	// Initialize the initializers map to store implementation registration functions
26	versionManager = version_manager.NewVersionManager(
27		cur.PkgPath(),
28		kvStore,
29		initializeDomainStore,
30	)
31
32	implementation = nil
33}
34
35func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
36	return NewPositionStore(kvStore)
37}
38
39func getImplementation() IPosition {
40	if implementation == nil {
41		panic("implementation is not initialized")
42	}
43
44	return implementation
45}
46
47func updateImplementation() error {
48	result := versionManager.GetCurrentImplementation()
49	if result == nil {
50		return errors.New("implementation is not initialized")
51	}
52
53	impl, ok := result.(IPosition)
54	if !ok {
55		return errors.New("impl is not an IPosition")
56	}
57
58	implementation = impl
59
60	return nil
61}