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

state.gno

1.25 Kb · 66 lines
 1package dca
 2
 3import (
 4	_ "gno.land/r/gnoswap/rbac/v1" // registers the roles read through access
 5
 6	"errors"
 7
 8	pdca "gno.land/p/gnoswap/order/dca/v0"
 9	"gno.land/p/gnoswap/store/v1"
10	"gno.land/p/gnoswap/version_manager/v1"
11)
12
13var (
14	currentAddress address
15	kvStore        store.KVStore
16
17	versionManager version_manager.VersionManager
18	implementation pdca.IDCAOrder
19)
20
21func init(cur realm) {
22	currentAddress = cur.Address()
23	kvStore = store.NewKVStore(currentAddress)
24
25	versionManager = version_manager.NewVersionManager(
26		cur.PkgPath(),
27		kvStore,
28		initializeDomainStore,
29	)
30
31	implementation = nil
32}
33
34func initializeDomainStore(_ int, rlm realm, kvStore store.KVStore) any {
35	return NewDCAOrderStore(kvStore)
36}
37
38// Address returns the realm address that escrows order funds.
39// Users approve it before CreateOrder.
40func Address() address {
41	return currentAddress
42}
43
44func getImplementation() pdca.IDCAOrder {
45	if implementation == nil {
46		panic(errImplNotInitialized)
47	}
48
49	return implementation
50}
51
52func updateImplementation() error {
53	result := versionManager.GetCurrentImplementation()
54	if result == nil {
55		return errors.New(errImplNotInitialized)
56	}
57
58	impl, ok := result.(pdca.IDCAOrder)
59	if !ok {
60		return errors.New(errInvalidImpl)
61	}
62
63	implementation = impl
64
65	return nil
66}