package staker import ( "gno.land/r/gnoswap/access/v1" ) // RegisterInitializer registers a new staker implementation version. // This function is called by each version (v1, v2, etc.) during initialization // to register their implementation with the proxy system. // // The initializer function creates a new instance of the implementation // using the provided stakerStore interface. // // Parameters: // - cur: Current realm context; callers use cross(cur) when crossing into this realm. // - initializer: Factory callback that receives the internal discriminator, current staker realm, and accessor interfaces, then returns the implementation instance. // // Security: Only contracts within the domain path can register initializers. // Each package path can only register once to prevent duplicate registrations. func RegisterInitializer(cur realm, initializer func(_ int, rlm realm, stakerStore IStakerStore, poolAccessor PoolAccessor, emissionAccessor EmissionAccessor, nftAccessor NFTAccessor) IStaker) { initializerFunc := func(_ int, rlm realm, domainStore any) any { access.AssertIsRlmCurrent(0, rlm) currentStakerStore, ok := domainStore.(IStakerStore) if !ok { panic("domainStore is not an IStakerStore") } return initializer(0, rlm, currentStakerStore, newPoolAccessor(), newEmissionAccessor(), newNFTAccessor()) } err := versionManager.RegisterInitializer(0, cur, initializerFunc) if err != nil { panic(err) } err = updateImplementation() if err != nil { panic(err) } } // UpgradeImpl switches the active staker implementation to a different version. // It changes the implementation pointer without transforming persisted state; the // selected implementation must understand the existing storage schema. // // Parameters: // - cur: Current realm context; callers use cross(cur) when crossing into this realm. // - packagePath: Registered package path of the implementation to activate. // // Security: Only admin or governance can perform upgrades. // The new implementation must have been previously registered via RegisterInitializer. func UpgradeImpl(cur realm, packagePath string) { // Ensure only admin or governance can perform upgrades caller := cur.Previous().Address() access.AssertIsAdminOrGovernance(caller) err := versionManager.ChangeImplementation(0, cur, packagePath) if err != nil { panic(err) } err = updateImplementation() if err != nil { panic(err) } } // GetImplementationPackagePath returns the package path of the currently active implementation. // // Returns: // - packagePath: package path of the active implementation func GetImplementationPackagePath() string { return versionManager.GetCurrentPackagePath() }