state.gno
1.27 Kb · 66 lines
1package limit
2
3import (
4 _ "gno.land/r/gnoswap/rbac/v1" // registers the roles read through access
5
6 "errors"
7
8 plimit "gno.land/p/gnoswap/order/limit/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 plimit.ILimitOrder
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, _ realm, kvStore store.KVStore) any {
35 return NewLimitOrderStore(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() plimit.ILimitOrder {
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.(plimit.ILimitOrder)
59 if !ok {
60 return errors.New(errInvalidImpl)
61 }
62
63 implementation = impl
64
65 return nil
66}