-
Notifications
You must be signed in to change notification settings - Fork 168
/
migrator.go
62 lines (52 loc) · 1.85 KB
/
migrator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package keeper
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/CosmosContracts/juno/v21/x/tokenfactory/exported"
v2 "github.com/CosmosContracts/juno/v21/x/tokenfactory/migrations/v2"
)
// Migrator is a struct for handling in-place state migrations.
type Migrator struct {
keeper Keeper
legacySubspace exported.Subspace
}
func NewMigrator(k Keeper, ss exported.Subspace) Migrator {
return Migrator{
keeper: k,
legacySubspace: ss,
}
}
// Migrate1to2 migrates the x/tokenfactory module state from the consensus version 1 to
// version 2. Specifically, it takes the parameters that are currently stored
// and managed by the x/params modules and stores them directly into the x/tokenfactory
// module state.
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
// Fixes hard forking genesis being invalid.
// https://github.com/sei-protocol/sei-chain/pull/861
iter := m.keeper.GetAllDenomsIterator(ctx)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
denom := string(iter.Value())
denomMetadata, err := m.keeper.bankKeeper.GetDenomMetaData(ctx, denom)
if err {
panic(fmt.Errorf("denom %s does not exist", denom))
}
fmt.Printf("Migrating denom: %s\n", denom)
m.SetMetadata(&denomMetadata)
m.keeper.bankKeeper.SetDenomMetaData(ctx, denomMetadata)
}
return v2.Migrate(ctx, ctx.KVStore(m.keeper.storeKey), m.legacySubspace, m.keeper.cdc)
}
func (m Migrator) SetMetadata(denomMetadata *banktypes.Metadata) {
if len(denomMetadata.Base) == 0 {
panic(fmt.Errorf("no base exists for denom %v", denomMetadata))
}
if len(denomMetadata.Display) == 0 {
denomMetadata.Display = denomMetadata.Base
denomMetadata.Name = denomMetadata.Base
denomMetadata.Symbol = denomMetadata.Base
} else {
fmt.Printf("Denom %s already has denom set", denomMetadata.Base)
}
}