Skip to content

Commit

Permalink
fix(migration): add migration for correcting gamm pools denom metadata (
Browse files Browse the repository at this point in the history
  • Loading branch information
zale144 authored Dec 19, 2024
1 parent 3395842 commit f9d073a
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
34 changes: 34 additions & 0 deletions app/upgrades/v4/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v4

import (
"fmt"
"slices"
"strings"

Expand All @@ -11,6 +12,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
crisistypes "github.com/cosmos/cosmos-sdk/x/crisis/types"
distrtypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
Expand Down Expand Up @@ -107,6 +109,10 @@ func CreateUpgradeHandler(
return nil, err
}

if err := migrateGAMMPoolDenomMetadata(ctx, keepers.BankKeeper); err != nil {
return nil, err
}

// Start running the module migrations
logger.Debug("running module migrations ...")
return migrations, nil
Expand Down Expand Up @@ -313,6 +319,34 @@ func migrateDelayedAckPacketIndex(ctx sdk.Context, dk delayedackkeeper.Keeper) e
return nil
}

func migrateGAMMPoolDenomMetadata(ctx sdk.Context, rk bankkeeper.Keeper) error {
const lastOldDenomIndex = 13

for i := 1; i <= lastOldDenomIndex; i++ {
denom := fmt.Sprintf("gamm/pool/%d", i)
dm, ok := rk.GetDenomMetaData(ctx, denom)
if !ok {
return errorsmod.Wrapf(banktypes.ErrDenomMetadataNotFound, "denom metadata not found for denom %s", denom)
}

if dm.Name == "" {
dm.Name = denom
}

if dm.Display == "" {
dm.Display = fmt.Sprintf("GAMM-%d", i)
}

if dm.Symbol == "" {
dm.Symbol = dm.Display
}

rk.SetDenomMetaData(ctx, dm)
}

return nil
}

func ConvertOldRollappToNew(oldRollapp rollapptypes.Rollapp) rollapptypes.Rollapp {
genesisInfo := rollapptypes.GenesisInfo{
Bech32Prefix: oldRollapp.RollappId[:5], // placeholder data
Expand Down
54 changes: 54 additions & 0 deletions app/upgrades/v4/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/keys/ed25519"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/cosmos/cosmos-sdk/x/bank/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -82,6 +83,8 @@ func (s *UpgradeTestSuite) TestUpgrade() {

s.seedRollappFinalizationQueue()

s.seedOldGAMMDenomMetadata()

return nil
},
upgrade: func() {
Expand Down Expand Up @@ -153,6 +156,8 @@ func (s *UpgradeTestSuite) TestUpgrade() {

s.validateModulePermissions()

s.validateGAMMDenomMetadata()

return
},
expPass: true,
Expand Down Expand Up @@ -406,6 +411,18 @@ func (s *UpgradeTestSuite) validateNonFinalizedStateInfos() {
}
}

func (s *UpgradeTestSuite) validateGAMMDenomMetadata() {
for _, dm := range generateOldGAMMDenomMetadata() {
// name and symbol are expected to be set
dm.Name = dm.Base
dm.Symbol = dm.Display

got, ok := s.App.BankKeeper.GetDenomMetaData(s.Ctx, dm.Base)
s.Require().True(ok)
s.Require().Equal(got, dm)
}
}

func (s *UpgradeTestSuite) seedAndStoreRollapps(numRollapps int) {
for _, rollapp := range s.seedRollapps(numRollapps) {
s.App.RollappKeeper.SetRollapp(s.Ctx, rollapp)
Expand Down Expand Up @@ -522,6 +539,43 @@ func (s *UpgradeTestSuite) seedRollappFinalizationQueue() {
}
}

func generateOldGAMMDenomMetadata() (dms []types.Metadata) {
const lastOldDenomIndex = 13
for i := 1; i <= lastOldDenomIndex; i++ {
denom := fmt.Sprintf("gamm/pool/%d", i)
display := fmt.Sprintf("GAMM-%d", i)

dm := types.Metadata{
Description: fmt.Sprintf("The share token of the gamm pool %d", i),
DenomUnits: []*types.DenomUnit{
{
Denom: denom,
Exponent: 0,
Aliases: []string{"attopoolshare"},
}, {
Denom: display,
Exponent: 18,
Aliases: nil,
},
},
Base: denom,
Display: display,
Name: "",
Symbol: "",
URI: "",
URIHash: "",
}
dms = append(dms, dm)
}
return
}

func (s *UpgradeTestSuite) seedOldGAMMDenomMetadata() {
for _, dm := range generateOldGAMMDenomMetadata() {
s.App.BankKeeper.SetDenomMetaData(s.Ctx, dm)
}
}

func generateStateInfo(rollappIdx, stateIdx int) rollapptypes.StateInfo {
return rollapptypes.StateInfo{
StateInfoIndex: rollapptypes.StateInfoIndex{
Expand Down

0 comments on commit f9d073a

Please sign in to comment.