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: migrate layer2 crosschain token #122

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions app/upgrades/v6/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ import (
layer2types "github.com/functionx/fx-core/v6/x/layer2/types"
)

var Layer2GenesisTokenAddress = map[string]string{
"FX": "",
"PUNDIX": "",
"USDT": "",
"WETH": "",
"LINK": "",
"WBTC": "",
"USDC": "",
}

var Upgrade = upgrades.Upgrade{
UpgradeName: "v6.0.x",
CreateUpgradeHandler: CreateUpgradeHandler,
Expand Down
30 changes: 30 additions & 0 deletions app/upgrades/v6/upgrade.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package v6

import (
"fmt"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"

"github.com/functionx/fx-core/v6/app/keepers"
crosschainkeeper "github.com/functionx/fx-core/v6/x/crosschain/keeper"
govtypes "github.com/functionx/fx-core/v6/x/gov/types"
layer2types "github.com/functionx/fx-core/v6/x/layer2/types"
)

func CreateUpgradeHandler(
Expand All @@ -21,6 +27,9 @@
return nil, err
}

MigrateMetadata(cacheCtx, app.BankKeeper)
MigrateLayer2Module(cacheCtx, app.Layer2Keeper)

ctx.Logger().Info("start to run v6 migrations...", "module", "upgrade")
toVM, err := mm.RunMigrations(cacheCtx, configurator, fromVM)
if err != nil {
Expand Down Expand Up @@ -78,3 +87,24 @@
})
return nil
}

func MigrateMetadata(ctx sdk.Context, bankKeeper bankkeeper.Keeper) {
bankKeeper.IterateAllDenomMetaData(ctx, func(metadata banktypes.Metadata) bool {
address, ok := Layer2GenesisTokenAddress[metadata.Symbol]
if !ok {
return false
}
if len(metadata.DenomUnits) > 0 {
metadata.DenomUnits[0].Aliases = append(metadata.DenomUnits[0].Aliases,
fmt.Sprintf("%s%s", layer2types.ModuleName, address))
}
zakir-code marked this conversation as resolved.
Show resolved Hide resolved
return false
})
}

func MigrateLayer2Module(ctx sdk.Context, layer2CrossChainKeeper crosschainkeeper.Keeper) {
for _, address := range Layer2GenesisTokenAddress {
fxTokenDenom := fmt.Sprintf("%s%s", layer2types.ModuleName, address)
layer2CrossChainKeeper.AddBridgeToken(ctx, address, fxTokenDenom)
}
Dismissed Show dismissed Hide dismissed
Dismissed Show dismissed Hide dismissed
}
28 changes: 28 additions & 0 deletions app/upgrades/v6/upgrade_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package v6_test

import (
"fmt"
"strings"
"testing"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmrand "github.com/tendermint/tendermint/libs/rand"
Expand All @@ -13,6 +16,7 @@ import (
v6 "github.com/functionx/fx-core/v6/app/upgrades/v6"
"github.com/functionx/fx-core/v6/testutil/helpers"
fxtypes "github.com/functionx/fx-core/v6/types"
layer2types "github.com/functionx/fx-core/v6/x/layer2/types"
)

type UpgradeTestSuite struct {
Expand All @@ -35,6 +39,10 @@ func (s *UpgradeTestSuite) SetupTest() {
Height: s.app.LastBlockHeight() + 1,
ProposerAddress: valSet.Proposer.Address.Bytes(),
})

for symbol := range v6.Layer2GenesisTokenAddress {
v6.Layer2GenesisTokenAddress[symbol] = common.BytesToAddress(helpers.NewEthPrivKey().PubKey().Address().Bytes()).String()
}
}

func (s *UpgradeTestSuite) CommitBlock(block int64) {
Expand All @@ -60,3 +68,23 @@ func (s *UpgradeTestSuite) TestUpdateParams() {
s.NoError(v6.UpdateParams(s.ctx, s.app.AppKeepers))
s.CommitBlock(10)
}

func (s *UpgradeTestSuite) TestMigrateMetadata() {
v6.MigrateMetadata(s.ctx, s.app.BankKeeper)
for symbol, address := range v6.Layer2GenesisTokenAddress {
metadata, found := s.app.BankKeeper.GetDenomMetaData(s.ctx, strings.ToLower(symbol))
zakir-code marked this conversation as resolved.
Show resolved Hide resolved
if found {
s.True(len(metadata.DenomUnits) > 0)
s.Subset(metadata.DenomUnits[0].Aliases, []string{fmt.Sprintf("%s%s", layer2types.ModuleName, address)})
}
}
}

func (s *UpgradeTestSuite) TestMigrateLayer2Module() {
v6.MigrateLayer2Module(s.ctx, s.app.Layer2Keeper)
for _, address := range v6.Layer2GenesisTokenAddress {
bridgeToken := s.app.Layer2Keeper.GetBridgeTokenDenom(s.ctx, address)
s.Equal(bridgeToken.Token, address)
s.Equal(bridgeToken.Denom, fmt.Sprintf("%s%s", layer2types.ModuleName, address))
}
}
Loading