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

chore: migrate erc20 token pair #783

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 31 additions & 1 deletion x/erc20/migrations/v8/keys.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -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)
}

Expand Down Expand Up @@ -57,6 +63,30 @@ 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)
nulnut marked this conversation as resolved.
Show resolved Hide resolved
md, found := m.bankKeeper.GetDenomMetaData(ctx, tokenPair.GetDenom())
if !found {
return sdkerrors.ErrKeyNotFound.Wrapf("metadata not found: %s", tokenPair.GetDenom())
}
nulnut marked this conversation as resolved.
Show resolved Hide resolved
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
}
nulnut marked this conversation as resolved.
Show resolved Hide resolved
tokenPair.Denom = strings.ToLower(md.Symbol)
if err := m.keeper.ERC20Token.Set(ctx, tokenPair.Denom, tokenPair); err != nil {
return err
}
nulnut marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

func OutgoingTransferKeyToOriginTokenKey(key []byte) string {
// key = prefix + moduleName(string-) + sdk.Uint64ToBigEndian(id)(len = 8)
// 1. remove prefix
Expand Down
20 changes: 0 additions & 20 deletions x/erc20/migrations/v8/legacy.go
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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()
}
29 changes: 14 additions & 15 deletions x/erc20/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
}
Expand All @@ -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
Expand Down