Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): add regen denom metadata migration #1209

Merged
merged 5 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions app/denom_metadata_migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//go:build !experimental
// +build !experimental

package app

import (
"encoding/json"
"os"
"testing"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/x/bank/types"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
dbm "github.com/tendermint/tm-db"
)

func TestMigrateDenomMetadata(t *testing.T) {
encCfg := MakeEncodingConfig()
db := dbm.NewMemDB()

regenApp := NewRegenApp(log.NewTMLogger(log.NewSyncWriter(os.Stdout)), db, nil, true, map[int64]bool{},
DefaultNodeHome, 0, encCfg, simapp.EmptyAppOptions{}, nil)
bz := NewDefaultGenesisState(encCfg.Marshaler)
stateBytes, err := json.MarshalIndent(bz, "", " ")
require.NoError(t, err)

regenApp.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
AppStateBytes: stateBytes,
},
)

ctx := regenApp.BaseApp.NewContext(false, tmproto.Header{})
regenApp.BankKeeper.SetDenomMetaData(ctx, types.Metadata{
Description: "The native staking token of Regen Network",
Base: "uregen",
Display: "regen",
DenomUnits: []*types.DenomUnit{
{
Denom: "uregen",
Exponent: 0,
Aliases: []string{"microregen"},
},
{
Denom: "mregen",
Exponent: 3,
Aliases: []string{"milliregen"},
},
{
Denom: "regen",
Exponent: 6,
},
},
})

regenApp.BankKeeper.SetDenomMetaData(ctx, types.Metadata{
Description: "The native staking token of Cosmos Hub",
Base: "uatom",
Display: "atom",
DenomUnits: []*types.DenomUnit{
{
Denom: "uatom",
Exponent: 0,
Aliases: []string{"microatom"},
},
{
Denom: "matom",
Exponent: 3,
Aliases: []string{"milliatom"},
},
{
Denom: "atom",
Exponent: 6,
},
},
Name: "Cosmos Atom",
Symbol: "ATOM",
})

metadata, found := regenApp.BankKeeper.GetDenomMetaData(ctx, "uregen")
require.True(t, found)
require.Equal(t, metadata.Name, "")
require.Equal(t, metadata.Symbol, "")

err = migrateDenomMetadata(ctx, regenApp.BankKeeper)
require.NoError(t, err)

metadata, found = regenApp.BankKeeper.GetDenomMetaData(ctx, "uregen")
require.True(t, found)
require.Equal(t, metadata.Name, "Regen")
require.Equal(t, metadata.Symbol, "REGEN")

metadata, found = regenApp.BankKeeper.GetDenomMetaData(ctx, "uatom")
require.True(t, found)
require.Equal(t, metadata.Name, "Cosmos Atom")
require.Equal(t, metadata.Symbol, "ATOM")
}
18 changes: 18 additions & 0 deletions app/stable_appconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,27 @@ func (app *RegenApp) registerUpgradeHandlers() {
}
}

// add name and symbol to regen denom metadata
if err := migrateDenomMetadata(ctx, app.BankKeeper); err != nil {
return nil, err
}

return toVersion, nil
})
}

func migrateDenomMetadata(ctx sdk.Context, bk bankkeeper.Keeper) error {
denom := "uregen"
metadata, found := bk.GetDenomMetaData(ctx, denom)
if !found {
return fmt.Errorf("metadata is present for %s denom", denom)
aleem1314 marked this conversation as resolved.
Show resolved Hide resolved
}

metadata.Name = "Regen"
metadata.Symbol = "REGEN"
bk.SetDenomMetaData(ctx, metadata)

return nil
}

func recoverFunds(ctx sdk.Context, ak authkeeper.AccountKeeper, bk bankkeeper.Keeper) error {
Expand Down