From a08a11be4ce1a855f99986c2788fdf99d16f9572 Mon Sep 17 00:00:00 2001 From: nulnut <151493716+nulnut@users.noreply.github.com> Date: Fri, 25 Oct 2024 16:09:26 +0800 Subject: [PATCH] chore: migrate erc20 token pair --- x/erc20/migrations/v8/keys.go | 33 +++++++++++++++++++++++++++++++- x/erc20/migrations/v8/legacy.go | 20 ------------------- x/erc20/migrations/v8/migrate.go | 29 ++++++++++++++-------------- 3 files changed, 46 insertions(+), 36 deletions(-) diff --git a/x/erc20/migrations/v8/keys.go b/x/erc20/migrations/v8/keys.go index c72cfea5..66cee898 100644 --- a/x/erc20/migrations/v8/keys.go +++ b/x/erc20/migrations/v8/keys.go @@ -1,10 +1,14 @@ package v8 import ( + "strings" + sdkmath "cosmossdk.io/math" storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + fxtypes "github.com/functionx/fx-core/v8/types" crosschaintypes "github.com/functionx/fx-core/v8/x/crosschain/types" "github.com/functionx/fx-core/v8/x/erc20/types" ) @@ -14,7 +18,9 @@ func (m Migrator) migrateKeys(ctx sdk.Context) error { if err := m.migrateParams(ctx, store); err != nil { return err } - + if err := m.migrateTokenPair(ctx, store); err != nil { + return err + } return m.migrateRelationToCache(ctx, store) } @@ -57,6 +63,31 @@ func (m Migrator) migrateParams(ctx sdk.Context, store storetypes.KVStore) error return m.keeper.Params.Set(ctx, types.Params{EnableErc20: legacyParams.EnableErc20}) } +func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) error { + iterator := storetypes.KVStorePrefixIterator(store, KeyPrefixTokenPair) + defer iterator.Close() + for ; iterator.Valid(); iterator.Next() { + var tokenPair types.ERC20Token + m.cdc.MustUnmarshal(iterator.Value(), &tokenPair) + md, found := m.bankKeeper.GetDenomMetaData(ctx, tokenPair.GetDenom()) + if !found { + return sdkerrors.ErrKeyNotFound.Wrapf("metadata not found: %s", tokenPair.GetDenom()) + } + if md.Base == fxtypes.DefaultDenom || md.Base == strings.ToLower(md.Symbol) { + if err := m.keeper.ERC20Token.Set(ctx, md.Base, tokenPair); err != nil { + return err + } + continue + } + tokenPair.Denom = strings.ToLower(md.Symbol) + if err := m.keeper.ERC20Token.Set(ctx, tokenPair.Denom, tokenPair); err != nil { + return err + } + store.Delete(iterator.Key()) + } + return nil +} + func OutgoingTransferKeyToOriginTokenKey(key []byte) string { // key = prefix + moduleName(string-) + sdk.Uint64ToBigEndian(id)(len = 8) // 1. remove prefix diff --git a/x/erc20/migrations/v8/legacy.go b/x/erc20/migrations/v8/legacy.go index 5b3cd4af..2410e5be 100644 --- a/x/erc20/migrations/v8/legacy.go +++ b/x/erc20/migrations/v8/legacy.go @@ -1,13 +1,5 @@ package v8 -import ( - storetypes "cosmossdk.io/store/types" - "github.com/cosmos/cosmos-sdk/codec" - sdk "github.com/cosmos/cosmos-sdk/types" - - "github.com/functionx/fx-core/v8/x/erc20/types" -) - var ( KeyPrefixTokenPair = []byte{0x01} KeyPrefixTokenPairByERC20 = []byte{0x02} @@ -23,15 +15,3 @@ func GetRemovedStoreKeys() [][]byte { KeyPrefixTokenPair, KeyPrefixTokenPairByERC20, KeyPrefixTokenPairByDenom, KeyPrefixAliasDenom, } } - -func LegacyIsNativeERC20(ctx sdk.Context, storeKey storetypes.StoreKey, cdc codec.BinaryCodec, denom string) bool { - store := ctx.KVStore(storeKey) - id := store.Get(append(KeyPrefixTokenPairByDenom, []byte(denom)...)) - bz := store.Get(append(KeyPrefixTokenPair, id...)) - if len(bz) == 0 { - return false - } - var tokenPair types.ERC20Token - cdc.MustUnmarshal(bz, &tokenPair) - return tokenPair.IsNativeERC20() -} diff --git a/x/erc20/migrations/v8/migrate.go b/x/erc20/migrations/v8/migrate.go index 17d0b898..bf06a414 100644 --- a/x/erc20/migrations/v8/migrate.go +++ b/x/erc20/migrations/v8/migrate.go @@ -21,32 +21,29 @@ func (m Migrator) MigrateToken(ctx sdk.Context) error { mds := m.bankKeeper.GetAllDenomMetaData(ctx) for _, md := range mds { - baseDenom := strings.ToLower(md.Symbol) + newBaseDenom := strings.ToLower(md.Symbol) // exclude FX and alias empty, except PUNDIX if md.Base == fxtypes.DefaultDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != "PUNDIX" { continue } // add other bridge/ibc token for _, alias := range md.DenomUnits[0].Aliases { - if err := m.addToken(ctx, baseDenom, alias); err != nil { + if err := m.addToken(ctx, newBaseDenom, alias); err != nil { return err } } // only add pundix/purse token - if md.Base == baseDenom || strings.Contains(md.Base, baseDenom) { + if md.Base == newBaseDenom || strings.Contains(md.Base, newBaseDenom) { continue } - if err := m.addToken(ctx, baseDenom, md.Base); err != nil { + if err := m.addToken(ctx, newBaseDenom, md.Base); err != nil { return err } } return nil } -func (m Migrator) addToken( - ctx sdk.Context, - base, alias string, -) error { +func (m Migrator) addToken(ctx sdk.Context, base, alias string) error { if strings.HasPrefix(alias, ibctransfertypes.DenomPrefix+"/") { return m.addIBCToken(ctx, base, alias) } @@ -59,23 +56,22 @@ func (m Migrator) addIBCToken(ctx sdk.Context, base, alias string) error { return sdkerrors.ErrInvalidCoins.Wrapf("ibc denom hash not found: %s %s", base, alias) } ctx.Logger().Info("add ibc token", "base-denom", base, "alias", alias, "channel", channel) - return m.keeper.AddIBCToken(ctx, base, channel, alias) + return m.keeper.AddIBCToken(ctx, channel, base, alias) } -func (m Migrator) addBridgeToken( - ctx sdk.Context, - base, alias string, -) error { +func (m Migrator) addBridgeToken(ctx sdk.Context, base, alias string) error { if getExcludeBridgeToken(ctx, alias) { return nil } for _, ck := range m.crosschainKeepers { canAddFxBridgeToken := base == fxtypes.DefaultDenom && ck.ModuleName() == ethtypes.ModuleName + canAddBridgeToken := strings.HasPrefix(alias, ck.ModuleName()) excludeModule := ck.ModuleName() != arbitrumtypes.ModuleName && ck.ModuleName() != optimismtypes.ModuleName if ctx.ChainID() == fxtypes.MainnetChainId { canAddBridgeToken = canAddBridgeToken && excludeModule } + if !canAddFxBridgeToken && !canAddBridgeToken { continue } @@ -87,9 +83,12 @@ func (m Migrator) addBridgeToken( if !found { return sdkerrors.ErrKeyNotFound.Wrapf("module %s bridge token: %s", ck.ModuleName(), alias) } + erc20Token, err := m.keeper.GetERC20Token(ctx, base) + if err != nil { + return err + } ctx.Logger().Info("add bridge token", "base-denom", base, "alias", alias, "module", ck.ModuleName(), "contract", legacyBridgeToken.Token) - isNativeErc20 := LegacyIsNativeERC20(ctx, m.storeKey, m.cdc, base) - if err := m.keeper.AddBridgeToken(ctx, base, ck.ModuleName(), legacyBridgeToken.Token, isNativeErc20); err != nil { + if err := m.keeper.AddBridgeToken(ctx, base, ck.ModuleName(), legacyBridgeToken.Token, erc20Token.IsNativeERC20()); err != nil { return err } break