Skip to content

Commit

Permalink
fix(token): migrate ERC20 token on testnet (#865)
Browse files Browse the repository at this point in the history
Co-authored-by: nulnut <[email protected]>
  • Loading branch information
zakir-code and nulnut authored Jan 3, 2025
1 parent 171f571 commit 1ef9e49
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
24 changes: 24 additions & 0 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func Test_UpgradeTestnet(t *testing.T) {

// 3. check the status after the upgrade
checkBridgeToken(t, ctx, myApp)
checkErc20Token(t, ctx, myApp)
}

func buildApp(t *testing.T) *app.App {
Expand Down Expand Up @@ -161,6 +162,7 @@ func checkAppUpgrade(t *testing.T, ctx sdk.Context, myApp *app.App, bdd BeforeUp
checkMigrateBalance(t, ctx, myApp, bdd)

checkBridgeToken(t, ctx, myApp)
checkErc20Token(t, ctx, myApp)
}

func checkErc20Keys(t *testing.T, ctx sdk.Context, myApp *app.App) {
Expand Down Expand Up @@ -303,6 +305,28 @@ func checkBridgeToken(t *testing.T, ctx sdk.Context, myApp *app.App) {
}
}

func checkErc20Token(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()

iter, err := myApp.Erc20Keeper.ERC20Token.Iterate(ctx, nil)
require.NoError(t, err)
defer iter.Close()

kvs, err := iter.KeyValues()
require.NoError(t, err)

erc20Tokens := make([]erc20types.ERC20Token, 0, len(kvs))
for _, kv := range kvs {
erc20Tokens = append(erc20Tokens, kv.Value)
}

for _, et := range erc20Tokens {
has, err := myApp.Erc20Keeper.DenomIndex.Has(ctx, et.Erc20Address)
require.NoError(t, err)
require.True(t, has)
}
}

func allBalances(ctx sdk.Context, myApp *app.App) (map[string]sdk.Coins, map[string]sdk.Coins) {
accountBalance := make(map[string]sdk.Coins)
moduleBalance := make(map[string]sdk.Coins)
Expand Down
27 changes: 27 additions & 0 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ func CreateUpgradeHandler(cdc codec.Codec, mm *module.Manager, configurator modu
if err := migrateTestnetBridgeToken(cacheCtx, app.Erc20Keeper); err != nil {
return fromVM, err
}
if err := migrateTestnetErc20Token(cacheCtx, app.Erc20Keeper); err != nil {
return fromVM, err
}
commit()
cacheCtx.Logger().Info("upgrade complete", "module", "upgrade")
return fromVM, nil
Expand Down Expand Up @@ -481,6 +484,7 @@ func migrateTestnetBridgeToken(ctx sdk.Context, erc20Keeper erc20keeper.Keeper)
if err != nil {
return err
}
defer iter.Close()
kvs, err := iter.KeyValues()
if err != nil {
return err
Expand All @@ -503,3 +507,26 @@ func migrateTestnetBridgeToken(ctx sdk.Context, erc20Keeper erc20keeper.Keeper)
}
return nil
}

func migrateTestnetErc20Token(ctx sdk.Context, erc20Keeper erc20keeper.Keeper) error {
iter, err := erc20Keeper.ERC20Token.Iterate(ctx, nil)
if err != nil {
return err
}
defer iter.Close()
kvs, err := iter.KeyValues()
if err != nil {
return err
}
erc20Tokens := make([]erc20types.ERC20Token, 0, len(kvs))
for _, kv := range kvs {
erc20Tokens = append(erc20Tokens, kv.Value)
}
// set erc20 token
for _, et := range erc20Tokens {
if err = erc20Keeper.DenomIndex.Set(ctx, et.Erc20Address, et.Denom); err != nil {
return err
}
}
return nil
}
6 changes: 6 additions & 0 deletions x/erc20/migrations/v8/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) er
if err := m.keeper.ERC20Token.Set(ctx, md.Base, tokenPair); err != nil {
return err
}
if err := m.keeper.DenomIndex.Set(ctx, tokenPair.Erc20Address, md.Base); err != nil {
return err
}
continue
}
tokenPair.Denom = md.Base
Expand All @@ -87,6 +90,9 @@ func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) er
if err := m.keeper.ERC20Token.Set(ctx, tokenPair.Denom, tokenPair); err != nil {
return err
}
if err := m.keeper.DenomIndex.Set(ctx, tokenPair.Erc20Address, tokenPair.Denom); err != nil {
return err
}
}
return nil
}
Expand Down

0 comments on commit 1ef9e49

Please sign in to comment.