Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

1.15 Kb ยท 61 lines
 1package router
 2
 3import (
 4	"chain/runtime"
 5	"errors"
 6
 7	"gno.land/p/gnoswap/store"
 8	"gno.land/p/gnoswap/version_manager"
 9)
10
11var (
12	currentAddress = runtime.CurrentRealm().Address()
13	domainPath     = runtime.CurrentRealm().PkgPath()
14
15	kvStore store.KVStore
16
17	versionManager version_manager.VersionManager
18	implementation IRouter
19)
20
21func init() {
22	// Create a new KV store instance for this domain
23	kvStore = store.NewKVStore(currentAddress)
24
25	// Initialize the initializers map to store implementation registration functions
26	versionManager = version_manager.NewVersionManager(
27		domainPath,
28		kvStore,
29		initializeDomainStore,
30	)
31
32	implementation = nil
33}
34
35func initializeDomainStore(kvStore store.KVStore) any {
36	return NewRouterStore(kvStore)
37}
38
39func getImplementation() IRouter {
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.(IRouter)
54	if !ok {
55		return errors.New("impl is not an IRouter")
56	}
57
58	implementation = impl
59
60	return nil
61}