-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(app)!: migrate pre-initialized module accounts
- Loading branch information
Showing
2 changed files
with
71 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package app | ||
|
||
import ( | ||
"fmt" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper" | ||
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types" | ||
) | ||
|
||
// MigratePreInitializedModuleAccounts migrates module accounts that were pre-initialized as BaseAccounts to ModuleAccounts. | ||
func MigratePreInitializedModuleAccounts( | ||
ctx sdk.Context, | ||
ak authkeeper.AccountKeeper, | ||
moduleAccountsToInitialize []string, | ||
) error { | ||
for _, module := range moduleAccountsToInitialize { | ||
addr, perms := ak.GetModuleAddressAndPermissions(module) | ||
if addr == nil { | ||
return fmt.Errorf( | ||
"failed to get module address and permissions for module %s", | ||
module, | ||
) | ||
} | ||
|
||
acc := ak.GetAccount(ctx, addr) | ||
if acc == nil { | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"account for module %s has not been initialized yet, skipping", | ||
module, | ||
)) | ||
continue | ||
} | ||
|
||
_, isModuleAccount := acc.(authtypes.ModuleAccountI) | ||
if isModuleAccount { | ||
ctx.Logger().Info(fmt.Sprintf( | ||
"account for module %s was correctly initialized, skipping", | ||
module, | ||
)) | ||
continue | ||
} | ||
|
||
// Migrate from base account to module account | ||
baseAccount, ok := acc.(*authtypes.BaseAccount) | ||
if !ok { | ||
panic(fmt.Sprintf("account %s must be a base account", acc.GetAddress())) | ||
} | ||
|
||
newModuleAccount := authtypes.NewModuleAccount( | ||
baseAccount, | ||
module, | ||
perms..., | ||
) | ||
ak.SetModuleAccount(ctx, newModuleAccount) | ||
|
||
ctx.Logger().Info(fmt.Sprintf( | ||
"Successfully migrated from base account %+v to module account %+v for module %s", | ||
baseAccount, | ||
newModuleAccount, | ||
module, | ||
)) | ||
} | ||
|
||
return nil | ||
} |