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