Search Apps Documentation Source Content File Folder Download Copy Actions Download

upgrade.gno

1.97 Kb ยท 66 lines
 1package position
 2
 3import (
 4	"chain/runtime"
 5
 6	"gno.land/r/gnoswap/access"
 7)
 8
 9// RegisterInitializer registers a new position implementation version.
10// This function is called by each version (v1, v2, etc.) during initialization
11// to register their implementation with the proxy system.
12//
13// The initializer function creates a new instance of the implementation
14// using the provided positionStore interface.
15//
16// The stateInitializer function creates the initial state for this version.
17//
18// Security: Only contracts within the domain path can register initializers.
19// Each package path can only register once to prevent duplicate registrations.
20func RegisterInitializer(cur realm, initializer func(positionStore IPositionStore) IPosition) {
21	initializerFunc := func(domainStore any) any {
22		currentPositionStore, ok := domainStore.(IPositionStore)
23		if !ok {
24			panic("domainStore is not an IPositionStore")
25		}
26
27		return initializer(currentPositionStore)
28	}
29
30	err := versionManager.RegisterInitializer(initializerFunc)
31	if err != nil {
32		panic(err)
33	}
34
35	err = updateImplementation()
36	if err != nil {
37		panic(err)
38	}
39}
40
41// UpgradeImpl switches the active position implementation to a different version.
42// This function allows seamless upgrades from one version to another without
43// data migration or downtime.
44//
45// Security: Only admin or governance can perform upgrades.
46// The new implementation must have been previously registered via RegisterInitializer.
47func UpgradeImpl(cur realm, packagePath string) {
48	// Ensure only admin or governance can perform upgrades
49	caller := runtime.PreviousRealm().Address()
50	access.AssertIsAdminOrGovernance(caller)
51
52	err := versionManager.ChangeImplementation(packagePath)
53	if err != nil {
54		panic(err)
55	}
56
57	err = updateImplementation()
58	if err != nil {
59		panic(err)
60	}
61}
62
63// GetImplementationPackagePath returns the package path of the currently active implementation.
64func GetImplementationPackagePath() string {
65	return versionManager.GetCurrentPackagePath()
66}