Skip to content
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
101 changes: 89 additions & 12 deletions api/opinit/opchild/v1/types.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions proto/opinit/opchild/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ message MigrationInfo {

// ibc_port_id is the IBC port ID, which is allocated for l1 chain, in l2 chain state.
string ibc_port_id = 3;

// base_ibc_denom_path is the full IBC denom path of the stored base denom.
// It is required when the stored base denom is an ibc/{hash} denom.
string base_ibc_denom_path = 4;
}

// OraclePriceData defines a single oracle price without proof (used in batched updates).
Expand Down
2 changes: 1 addition & 1 deletion scripts/protocgen-pulsar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ echo "Generating API module"

# clone dependency proto files
IBC_URL=github.com/cosmos/ibc-go
IBC_V=v8
IBC_V=v10
ICS23_URL=github.com/cosmos/ics23

IBC_VERSION=$(cat ./go.mod | grep "$IBC_URL/$IBC_V v" | sed -nE 's/.* (v[0-9][^[:space:]]*).*/\1/p')
Expand Down
9 changes: 5 additions & 4 deletions x/opchild/keeper/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (

sdk "github.com/cosmos/cosmos-sdk/types"

transfertypes "github.com/cosmos/ibc-go/v10/modules/apps/transfer/types"

"github.com/initia-labs/OPinit/x/opchild/types"
)

Expand Down Expand Up @@ -108,8 +106,11 @@ func (k Keeper) InitGenesis(ctx context.Context, data *types.GenesisState) (res
panic(err)
}

ibcDenom := transfertypes.ExtractDenomFromPath(fmt.Sprintf("%s/%s/%s", migrationInfo.IbcPortId, migrationInfo.IbcChannelId, baseDenom)).IBCDenom()
if err := k.SetIBCToL2DenomMap(ctx, ibcDenom, migrationInfo.Denom); err != nil {
migrationIBCDenom, err := ibcDenom(migrationInfo, baseDenom)
if err != nil {
panic(err)
}
if err := k.SetIBCToL2DenomMap(ctx, migrationIBCDenom, migrationInfo.Denom); err != nil {
Comment thread
beer-1 marked this conversation as resolved.
panic(err)
}
}
Expand Down
51 changes: 48 additions & 3 deletions x/opchild/keeper/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"

"cosmossdk.io/collections"
errorsmod "cosmossdk.io/errors"
Expand Down Expand Up @@ -49,6 +50,11 @@ func (k Keeper) SetIBCToL2DenomMap(ctx context.Context, ibcDenom, l2Denom string
return k.IBCToL2DenomMap.Set(ctx, ibcDenom, l2Denom)
}

// RemoveIBCToL2DenomMap removes the ibc to l2 denom map
func (k Keeper) RemoveIBCToL2DenomMap(ctx context.Context, ibcDenom string) error {
return k.IBCToL2DenomMap.Remove(ctx, ibcDenom)
}

// GetIBCToL2DenomMap gets the ibc to l2 denom map
func (k Keeper) GetIBCToL2DenomMap(ctx context.Context, ibcDenom string) (string, error) {
return k.IBCToL2DenomMap.Get(ctx, ibcDenom)
Expand Down Expand Up @@ -87,7 +93,11 @@ func (k Keeper) MigrateToken(ctx context.Context, migrationInfo types.MigrationI
if err != nil {
return sdk.Coin{}, err
}
ibcCoin := sdk.NewCoin(ibcDenom(migrationInfo, baseDenom), amount.Amount)
migrationIBCDenom, err := ibcDenom(migrationInfo, baseDenom)
if err != nil {
return sdk.Coin{}, err
}
ibcCoin := sdk.NewCoin(migrationIBCDenom, amount.Amount)
err = k.bankKeeper.MintCoins(ctx, types.ModuleName, sdk.NewCoins(ibcCoin))
if err != nil {
return sdk.Coin{}, err
Expand Down Expand Up @@ -212,7 +222,42 @@ func (k Keeper) HandleMigratedTokenWithdrawal(ctx context.Context, msg *types.Ms
return true, nil
}

// ibcDenom computes the IBC denom from the migration info and base denom
func ibcDenom(migrationInfo types.MigrationInfo, baseDenom string) string {
// ibcDenom computes the IBC denom from the migration info and stored base denom.
func ibcDenom(migrationInfo types.MigrationInfo, baseDenom string) (string, error) {
if strings.HasPrefix(baseDenom, transfertypes.DenomPrefix+"/") {
if migrationInfo.BaseIbcDenomPath == "" {
return "", errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "base IBC denom path is required for IBC base denom")
}

baseDenomTrace := transfertypes.ExtractDenomFromPath(migrationInfo.BaseIbcDenomPath)
if err := baseDenomTrace.Validate(); err != nil {
return "", errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
if baseDenomTrace.IBCDenom() != baseDenom {
return "", errorsmod.Wrapf(
sdkerrors.ErrInvalidRequest,
"base IBC denom path does not match base denom; expected %s, got %s",
baseDenom,
baseDenomTrace.IBCDenom(),
)
}

return transfertypes.ExtractDenomFromPath(
fmt.Sprintf("%s/%s/%s", migrationInfo.IbcPortId, migrationInfo.IbcChannelId, migrationInfo.BaseIbcDenomPath),
).IBCDenom(), nil
}

if migrationInfo.BaseIbcDenomPath != "" {
return "", errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "base IBC denom path is only allowed for IBC base denom")
}

return transfertypes.ExtractDenomFromPath(fmt.Sprintf("%s/%s/%s", migrationInfo.IbcPortId, migrationInfo.IbcChannelId, baseDenom)).IBCDenom(), nil
}

func legacyIBCDenom(migrationInfo types.MigrationInfo, baseDenom string) string {
return transfertypes.ExtractDenomFromPath(fmt.Sprintf("%s/%s/%s", migrationInfo.IbcPortId, migrationInfo.IbcChannelId, baseDenom)).IBCDenom()
}

func canOverwriteMigrationInfo(migrationInfo types.MigrationInfo, baseDenom string) bool {
return strings.HasPrefix(baseDenom, transfertypes.DenomPrefix+"/") && migrationInfo.BaseIbcDenomPath == ""
Comment thread
traviolus marked this conversation as resolved.
}
Loading
Loading