Skip to content

Commit

Permalink
refactor: rename OriginalFXDenom to LegacyFXDenom (#926)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Jan 20, 2025
1 parent 293b4cf commit 3b8384b
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 45 deletions.
7 changes: 3 additions & 4 deletions app/upgrade_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ func checkPundiAIFXERC20Token(t *testing.T, ctx sdk.Context, myApp *app.App) {
has, err := myApp.Erc20Keeper.ERC20Token.Has(ctx, fxtypes.DefaultDenom)
require.NoError(t, err)
require.True(t, has)
has, err = myApp.Erc20Keeper.ERC20Token.Has(ctx, fxtypes.OriginalFXDenom())
has, err = myApp.Erc20Keeper.ERC20Token.Has(ctx, fxtypes.LegacyFXDenom)
require.NoError(t, err)
require.False(t, has)
}
Expand All @@ -668,10 +668,9 @@ func checkEvmParams(t *testing.T, ctx sdk.Context, myApp *app.App) {
func checkBankModule(t *testing.T, ctx sdk.Context, myApp *app.App) {
t.Helper()

fxDenom := fxtypes.OriginalFXDenom()
totalSupply := sdkmath.ZeroInt()
myApp.BankKeeper.IterateAllBalances(ctx, func(addr sdk.AccAddress, balance sdk.Coin) bool {
require.NotEqual(t, fxDenom, balance.Denom)
require.NotEqual(t, fxtypes.LegacyFXDenom, balance.Denom)
if balance.Denom == fxtypes.DefaultDenom {
totalSupply = totalSupply.Add(balance.Amount)
}
Expand All @@ -682,7 +681,7 @@ func checkBankModule(t *testing.T, ctx sdk.Context, myApp *app.App) {
require.Equal(t, totalSupply, supply.Amount)

myApp.BankKeeper.IterateSendEnabledEntries(ctx, func(denom string, sendEnabled bool) (stop bool) {
require.NotEqual(t, fxDenom, denom)
require.NotEqual(t, fxtypes.LegacyFXDenom, denom)
return false
})
}
Expand Down
29 changes: 12 additions & 17 deletions app/upgrades/v8/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func migrateTransferTokenInEscrow(ctx sdk.Context, transferKeeper ibctransferkee
for oldDenom, newDenom := range escrowDenoms {
totalEscrow := transferKeeper.GetTotalEscrowForDenom(ctx, oldDenom)
newAmount := totalEscrow.Amount
if oldDenom == fxtypes.IBCFXDenom {
if oldDenom == fxtypes.LegacyFXDenom {
newAmount = fxtypes.SwapAmount(newAmount)
}
// first remove old denom
Expand Down Expand Up @@ -286,10 +286,9 @@ func migrateCrosschainModuleAccount(ctx sdk.Context, ak authkeeper.AccountKeeper
}

func migrateBridgeBalance(ctx sdk.Context, bankKeeper bankkeeper.Keeper, accountKeeper authkeeper.AccountKeeper) error {
fxDenom := fxtypes.OriginalFXDenom()
mds := bankKeeper.GetAllDenomMetaData(ctx)
for _, md := range mds {
if md.Base == fxDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != pundixSymbol {
if md.Base == fxtypes.LegacyFXDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != pundixSymbol {
continue
}
dstBase := md.Base
Expand Down Expand Up @@ -362,12 +361,11 @@ func migrateERC20TokenToCrosschain(ctx sdk.Context, bankKeeper bankkeeper.Keeper
}

func updateMetadata(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error {
fxDenom := fxtypes.OriginalFXDenom()
mds := bankKeeper.GetAllDenomMetaData(ctx)

removeMetadata := make([]string, 0, 2)
for _, md := range mds {
if md.Base == fxDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != pundixSymbol {
if md.Base == fxtypes.LegacyFXDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != pundixSymbol {
continue
}
// remove alias
Expand Down Expand Up @@ -681,16 +679,15 @@ func migrateMetadataDisplay(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error
}

func migrateErc20FXToPundiAI(ctx sdk.Context, keeper erc20keeper.Keeper) error {
fxDenom := fxtypes.OriginalFXDenom()
erc20Token, err := keeper.GetERC20Token(ctx, fxDenom)
erc20Token, err := keeper.GetERC20Token(ctx, fxtypes.LegacyFXDenom)
if err != nil {
return err
}
erc20Token.Denom = fxtypes.DefaultDenom
if err = keeper.ERC20Token.Set(ctx, erc20Token.Denom, erc20Token); err != nil {
return err
}
return keeper.ERC20Token.Remove(ctx, fxDenom)
return keeper.ERC20Token.Remove(ctx, fxtypes.LegacyFXDenom)
}

func migrateMetadataFXToPundiAI(ctx sdk.Context, keeper bankkeeper.Keeper) error {
Expand All @@ -703,28 +700,26 @@ func migrateMetadataFXToPundiAI(ctx sdk.Context, keeper bankkeeper.Keeper) error
if !ok {
return errors.New("bank keeper not implement bank.BaseKeeper")
}
return bk.BaseViewKeeper.DenomMetadata.Remove(ctx, fxtypes.OriginalFXDenom())
return bk.BaseViewKeeper.DenomMetadata.Remove(ctx, fxtypes.LegacyFXDenom)
}

func migrateBankModule(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error {
fxDenom := fxtypes.OriginalFXDenom()

sendEnabledEntry, found := bankKeeper.GetSendEnabledEntry(ctx, fxDenom)
sendEnabledEntry, found := bankKeeper.GetSendEnabledEntry(ctx, fxtypes.LegacyFXDenom)
if found {
bankKeeper.DeleteSendEnabled(ctx, fxDenom)
bankKeeper.DeleteSendEnabled(ctx, fxtypes.LegacyFXDenom)
bankKeeper.SetSendEnabled(ctx, fxtypes.DefaultDenom, sendEnabledEntry.Enabled)
}

var err error
fxSupply := bankKeeper.GetSupply(ctx, fxDenom)
fxSupply := bankKeeper.GetSupply(ctx, fxtypes.LegacyFXDenom)
apundiaiSupply := sdkmath.ZeroInt()

bk, ok := bankKeeper.(bankkeeper.BaseKeeper)
if !ok {
return errors.New("bank keeper not implement bank.BaseKeeper")
}
bk.IterateAllBalances(ctx, func(address sdk.AccAddress, coin sdk.Coin) (stop bool) {
if coin.Denom != fxDenom {
if coin.Denom != fxtypes.LegacyFXDenom {
return false
}
if err = bk.Balances.Remove(ctx, collections.Join(address, coin.Denom)); err != nil {
Expand All @@ -746,15 +741,15 @@ func migrateBankModule(ctx sdk.Context, bankKeeper bankkeeper.Keeper) error {
}

ctx.Logger().Info("migrate fx to apundiai", "FX supply", fxSupply.Amount.String(), "apundiai supply", apundiaiSupply.String())
if err = bk.Supply.Remove(ctx, fxDenom); err != nil {
if err = bk.Supply.Remove(ctx, fxtypes.LegacyFXDenom); err != nil {
return err
}
return bk.Supply.Set(ctx, fxtypes.DefaultDenom, apundiaiSupply)
}

func GetMigrateEscrowDenoms(chainID string) map[string]string {
result := make(map[string]string, 2)
result[fxtypes.IBCFXDenom] = fxtypes.DefaultDenom
result[fxtypes.LegacyFXDenom] = fxtypes.DefaultDenom

pundixDenom := fxtypes.MainnetPundixUnWrapDenom
if chainID == fxtypes.TestnetChainId {
Expand Down
7 changes: 2 additions & 5 deletions types/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const (
DefaultSymbol = "PUNDIAI"
DenomUnit = 18

FXDenom = "fx"
LegacyFXDenom = "FX"
FXDenom = "fx"

AddrLen = 20

Expand Down Expand Up @@ -104,7 +105,3 @@ func SwapCoin(coin sdk.Coin) sdk.Coin {
coin.Amount = SwapAmount(coin.Amount)
return coin
}

func OriginalFXDenom() string {
return strings.ToUpper(FXDenom)
}
10 changes: 4 additions & 6 deletions types/denom_wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
const (
PundixWrapDenom = "pundix"
PundixChannel = "channel-0"

IBCFXDenom = "FX"
)

const (
Expand All @@ -27,8 +25,8 @@ const (
var (
MainnetOnRecvWrap = map[string]string{
OnRecvDenomWrapKey(PundixChannel, MainnetPundixUnWrapDenom): PundixWrapDenom,
OnRecvDenomWrapKey(MainnetOsmosisChannel, IBCFXDenom): DefaultDenom,
OnRecvDenomWrapKey(PundixChannel, IBCFXDenom): DefaultDenom,
OnRecvDenomWrapKey(MainnetOsmosisChannel, LegacyFXDenom): DefaultDenom,
OnRecvDenomWrapKey(PundixChannel, LegacyFXDenom): DefaultDenom,
}

MainnetSendPacketWrap = map[string]string{
Expand All @@ -39,8 +37,8 @@ var (
var (
TestnetOnRecvWrap = map[string]string{
OnRecvDenomWrapKey(PundixChannel, TestnetPundixUnWrapDenom): PundixWrapDenom,
OnRecvDenomWrapKey(TestnetOsmosisChannel, IBCFXDenom): DefaultDenom,
OnRecvDenomWrapKey(PundixChannel, IBCFXDenom): DefaultDenom,
OnRecvDenomWrapKey(TestnetOsmosisChannel, LegacyFXDenom): DefaultDenom,
OnRecvDenomWrapKey(PundixChannel, LegacyFXDenom): DefaultDenom,
}
TestnetSendPacketWrap = map[string]string{
SendPacketDenomWrapKey(PundixChannel, PundixWrapDenom): TestnetPundixUnWrapDenom,
Expand Down
8 changes: 4 additions & 4 deletions types/denom_wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ func Test_OnRecvDenomNeedWrap(t *testing.T) {
name: "mainnet osmosis FX to DefaultDenom",
chainId: MainnetChainId,
channel: MainnetOsmosisChannel,
denom: IBCFXDenom,
denom: LegacyFXDenom,
expected: true,
wrapDenom: DefaultDenom,
},
{
name: "mainnet pundix FX to DefaultDenom",
chainId: MainnetChainId,
channel: PundixChannel,
denom: IBCFXDenom,
denom: LegacyFXDenom,
expected: true,
wrapDenom: DefaultDenom,
},
Expand All @@ -53,15 +53,15 @@ func Test_OnRecvDenomNeedWrap(t *testing.T) {
name: "testnet osmosis FX to DefaultDenom",
chainId: TestnetChainId,
channel: TestnetOsmosisChannel,
denom: IBCFXDenom,
denom: LegacyFXDenom,
expected: true,
wrapDenom: DefaultDenom,
},
{
name: "testnet pundix FX to DefaultDenom",
chainId: TestnetChainId,
channel: PundixChannel,
denom: IBCFXDenom,
denom: LegacyFXDenom,
expected: true,
wrapDenom: DefaultDenom,
},
Expand Down
3 changes: 1 addition & 2 deletions x/erc20/migrations/v8/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ func (m Migrator) migrateParams(ctx sdk.Context, store storetypes.KVStore) error
}

func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) error {
fxDenom := fxtypes.OriginalFXDenom()
iterator := storetypes.KVStorePrefixIterator(store, KeyPrefixTokenPair)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
Expand All @@ -74,7 +73,7 @@ func (m Migrator) migrateTokenPair(ctx sdk.Context, store storetypes.KVStore) er
if !found {
return sdkerrors.ErrKeyNotFound.Wrapf("metadata not found: %s", tokenPair.GetDenom())
}
if md.Base == fxDenom || md.Base == strings.ToLower(md.Symbol) {
if md.Base == fxtypes.LegacyFXDenom || md.Base == strings.ToLower(md.Symbol) {
if err := m.keeper.ERC20Token.Set(ctx, md.Base, tokenPair); err != nil {
return err
}
Expand Down
8 changes: 3 additions & 5 deletions x/erc20/migrations/v8/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@ import (
)

func (m Migrator) MigrateToken(ctx sdk.Context) error {
fxDenom := fxtypes.OriginalFXDenom()
// add FX bridge token
if err := m.addToken(ctx, fxDenom, ""); err != nil {
if err := m.addToken(ctx, fxtypes.LegacyFXDenom, ""); err != nil {
return err
}

mds := m.bankKeeper.GetAllDenomMetaData(ctx)
for _, md := range mds {
// exclude FX and alias empty, except PUNDIX
if md.Base == fxDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != "PUNDIX" {
if md.Base == fxtypes.LegacyFXDenom || (len(md.DenomUnits) == 0 || len(md.DenomUnits[0].Aliases) == 0) && md.Symbol != "PUNDIX" {
continue
}

Expand Down Expand Up @@ -74,12 +73,11 @@ func (m Migrator) addIBCToken(ctx sdk.Context, base, alias string) error {
}

func (m Migrator) addBridgeToken(ctx sdk.Context, base, alias string) error {
fxDenom := fxtypes.OriginalFXDenom()
if getExcludeBridgeToken(ctx, alias) {
return nil
}
for _, ck := range m.crosschainKeepers {
canAddFxBridgeToken := base == fxDenom && ck.ModuleName() == ethtypes.ModuleName
canAddFxBridgeToken := base == fxtypes.LegacyFXDenom && ck.ModuleName() == ethtypes.ModuleName

canAddBridgeToken := strings.HasPrefix(alias, ck.ModuleName())
excludeModule := ck.ModuleName() != arbitrumtypes.ModuleName && ck.ModuleName() != optimismtypes.ModuleName
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/middleware/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func UnmarshalAckPacketData(packetData []byte) (transfertypes.FungibleTokenPacke
}
ibcPacketData := data.ToIBCPacketData()
totalAmount := amount.Add(fee)
if ibcPacketData.Denom == fxtypes.IBCFXDenom {
if ibcPacketData.Denom == fxtypes.LegacyFXDenom {
ibcPacketData.Denom = fxtypes.DefaultDenom
totalAmount = fxtypes.SwapAmount(totalAmount)
}
Expand Down
2 changes: 1 addition & 1 deletion x/ibc/middleware/keeper/relay_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func (suite *KeeperTestSuite) mintCoin(address sdk.AccAddress, coins ...sdk.Coin

func TestUnmarshalAckPacketData(t *testing.T) {
normalData := types.FungibleTokenPacketData{
Denom: fxtypes.IBCFXDenom,
Denom: fxtypes.LegacyFXDenom,
Amount: "1000",
}
normalExpected := transfertypes.FungibleTokenPacketData{
Expand Down

0 comments on commit 3b8384b

Please sign in to comment.