Search Apps Documentation Source Content File Folder Download Copy Actions Download

state.gno

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