upgrade.gno
1.58 Kb · 61 lines
1package limit
2
3import (
4 plimit "gno.land/p/gnoswap/order/limit/v0"
5
6 "gno.land/r/gnoswap/access/v1"
7)
8
9// RegisterInitializer registers an implementation version.
10// Each version calls it from init with a factory that receives the shared store.
11// Only packages under this realm's path may register,
12// and the first registration becomes active.
13// A nil initializer is rejected before its version path is registered.
14func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, orderStore plimit.ILimitOrderStore) plimit.ILimitOrder) {
15 if initializer == nil {
16 panic(ErrNilInitializer)
17 }
18
19 initializerFunc := func(_ int, rlm realm, domainStore any) any {
20 access.AssertIsRlmCurrent(0, rlm)
21
22 orderStore, ok := domainStore.(plimit.ILimitOrderStore)
23 if !ok {
24 panic(errInvalidDomainStore)
25 }
26
27 return initializer(0, rlm, orderStore)
28 }
29
30 err := versionManager.RegisterInitializer(0, cur, initializerFunc)
31 if err != nil {
32 panic(err.Error())
33 }
34
35 err = updateImplementation()
36 if err != nil {
37 panic(err.Error())
38 }
39}
40
41// UpgradeImpl activates a previously registered version by package path.
42// Admin or governance only.
43func UpgradeImpl(cur realm, packagePath string) {
44 caller := cur.Previous().Address()
45 access.AssertIsAdminOrGovernance(caller)
46
47 err := versionManager.ChangeImplementation(0, cur, packagePath)
48 if err != nil {
49 panic(err.Error())
50 }
51
52 err = updateImplementation()
53 if err != nil {
54 panic(err.Error())
55 }
56}
57
58// GetImplementationPackagePath returns the active implementation path.
59func GetImplementationPackagePath() string {
60 return versionManager.GetCurrentPackagePath()
61}