Skip to content

Commit

Permalink
fix middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jan 14, 2025
1 parent 9a530f4 commit 5e79e74
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 28 deletions.
5 changes: 2 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,6 @@ func New(
appCodec,
keys[ibcexported.StoreKey],
app.GetSubspace(ibcexported.ModuleName),
app.StakingKeeper,
app.UpgradeKeeper,
scopedIBCKeeper,
authAddr,
Expand All @@ -643,9 +642,9 @@ func New(
app.IBCFeeKeeper, // ISC4 Wrapper: fee IBC middleware
app.IBCKeeper.ChannelKeeper, app.IBCKeeper.PortKeeper,
app.AccountKeeper, scopedICAHostKeeper, app.MsgServiceRouter(),
app.GRPCQueryRouter(),
authtypes.NewModuleAddress(govtypes.ModuleName).String(),
)
app.ICAHostKeeper.WithQueryRouter(app.GRPCQueryRouter())
icaModule := ica.NewAppModule(&app.ICAControllerKeeper, &app.ICAHostKeeper)

// Create Transfer Keepers
Expand Down Expand Up @@ -747,7 +746,7 @@ func New(
))

var icaControllerStack porttypes.IBCModule
icaControllerStack = icacontroller.NewIBCMiddleware(nil, app.ICAControllerKeeper)
icaControllerStack = icacontroller.NewIBCMiddleware(app.ICAControllerKeeper)
icaControllerStack = ibcfee.NewIBCMiddleware(icaControllerStack, app.IBCFeeKeeper)
// Since the callbacks middleware itself is an ics4wrapper, it needs to be passed to the ica controller keeper
ics4Wrapper := icaControllerStack.(porttypes.Middleware)
Expand Down
3 changes: 3 additions & 0 deletions x/cronos/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
ibccallbacktypes "github.com/cosmos/ibc-go/modules/apps/callbacks/types"
ibcfeetypes "github.com/cosmos/ibc-go/v9/modules/apps/29-fee/types"
clienttypes "github.com/cosmos/ibc-go/v9/modules/core/02-client/types"
channeltypes "github.com/cosmos/ibc-go/v9/modules/core/04-channel/types"
Expand Down Expand Up @@ -48,6 +49,8 @@ type (
}
)

var _ ibccallbacktypes.ContractKeeper = Keeper{}

func NewKeeper(
cdc codec.Codec,
storeKey,
Expand Down
83 changes: 58 additions & 25 deletions x/cronos/middleware/conversion_middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,21 @@ func (im IBCConversionModule) OnRecvPacket(
return channeltypes.NewErrorAcknowledgement(errors.Wrap(sdkerrors.ErrUnknownRequest,
"cannot unmarshal ICS-20 transfer packet data in middleware"))
}
denom := im.getIbcDenomFromPacketAndData(packet, data)
// Check if it can be converted
if im.canBeConverted(ctx, denom) {
err = im.convertVouchers(ctx, data, denom, false)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err)
for _, token := range data.Tokens {
denom := im.getIbcDenomFromPacketAndData(packet, token)
// Check if it can be converted
if im.canBeConverted(ctx, denom) {
err = im.convertVouchers(
ctx,
token.Amount,
data.Sender,
data.Receiver,
denom,
false,
)
if err != nil {
return channeltypes.NewErrorAcknowledgement(err)
}
}
}
}
Expand Down Expand Up @@ -143,9 +152,18 @@ func (im IBCConversionModule) OnAcknowledgementPacket(
if err != nil {
return err
}
denom := im.getIbcDenomFromDataForRefund(data)
if im.canBeConverted(ctx, denom) {
return im.convertVouchers(ctx, data, denom, true)
for _, token := range data.Tokens {
denom := im.getIbcDenomFromDataForRefund(token)
if im.canBeConverted(ctx, denom) {
return im.convertVouchers(
ctx,
token.Amount,
data.Sender,
data.Receiver,
denom,
true,
)
}
}
}
}
Expand All @@ -167,9 +185,18 @@ func (im IBCConversionModule) OnTimeoutPacket(
if err != nil {
return err
}
denom := im.getIbcDenomFromDataForRefund(data)
if im.canBeConverted(ctx, denom) {
return im.convertVouchers(ctx, data, denom, true)
for _, token := range data.Tokens {
denom := im.getIbcDenomFromDataForRefund(token)
if im.canBeConverted(ctx, denom) {
return im.convertVouchers(
ctx,
token.Amount,
data.Sender,
data.Receiver,
denom,
true,
)
}
}
}
return err
Expand All @@ -184,18 +211,25 @@ func (im IBCConversionModule) getFungibleTokenPacketData(packet channeltypes.Pac
return data, nil
}

func (im IBCConversionModule) convertVouchers(ctx sdk.Context, data transferTypes.FungibleTokenPacketDataV2, denom string, isSender bool) error {
func (im IBCConversionModule) convertVouchers(
ctx sdk.Context,
amount string,
sender string,
receiver string,
denom string,
isSender bool,
) error {
// parse the transfer amount
transferAmount, ok := sdkmath.NewIntFromString(data.Tokens[0].Amount)
transferAmount, ok := sdkmath.NewIntFromString(amount)
if !ok {
return errors.Wrapf(transferTypes.ErrInvalidAmount,
"unable to parse transfer amount (%s) into sdk.Int in middleware", data.Tokens[0].Amount)
"unable to parse transfer amount (%s) into sdk.Int in middleware", amount)
}
token := sdk.NewCoin(denom, transferAmount)
if isSender {
im.cronoskeeper.OnRecvVouchers(ctx, sdk.NewCoins(token), data.Sender)
im.cronoskeeper.OnRecvVouchers(ctx, sdk.NewCoins(token), sender)
} else {
im.cronoskeeper.OnRecvVouchers(ctx, sdk.NewCoins(token), data.Receiver)
im.cronoskeeper.OnRecvVouchers(ctx, sdk.NewCoins(token), receiver)
}
return nil
}
Expand All @@ -209,24 +243,23 @@ func (im IBCConversionModule) canBeConverted(ctx sdk.Context, denom string) bool
return found
}

func (im IBCConversionModule) getIbcDenomFromDataForRefund(data transferTypes.FungibleTokenPacketDataV2) string {
return data.Tokens[0].Denom.Base
func (im IBCConversionModule) getIbcDenomFromDataForRefund(token transferTypes.Token) string {
return token.Denom.Base
}

func (im IBCConversionModule) getIbcDenomFromPacketAndData(
packet channeltypes.Packet, data transferTypes.FungibleTokenPacketDataV2,
packet channeltypes.Packet, token transferTypes.Token,
) string {
denom := data.Tokens[0].Denom
denom := token.Denom
if denom.HasPrefix(packet.GetSourcePort(), packet.GetSourceChannel()) {
denom.Trace = denom.Trace[1:]
return denom.IBCDenom()
}

// since SendPacket did not prefix the denomination, we must prefix denomination here
sourcePrefix := transferTypes.GetDenomPrefix(packet.GetDestPort(), packet.GetDestChannel())
prefixedDenom := sourcePrefix + data.Denom
denomTrace := transferTypes.ParseDenomTrace(prefixedDenom)
return denomTrace.IBCDenom()
trace := []transferTypes.Hop{transferTypes.NewHop(packet.DestinationPort, packet.DestinationChannel)}
denom.Trace = append(trace, denom.Trace...)
return denom.IBCDenom()
}

// OnChanUpgradeInit implements the IBCModule interface
Expand Down

0 comments on commit 5e79e74

Please sign in to comment.