Skip to content

Commit

Permalink
fix(bridging_fee): adding cacheCtx for charging fees (#1563)
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin authored Nov 28, 2024
1 parent 2c31dd3 commit 65cd290
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 40 deletions.
1 change: 0 additions & 1 deletion app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,7 +545,6 @@ func (a *AppKeepers) InitTransferStack() {
a.DelayedAckKeeper,
a.TransferKeeper,
*a.TxFeesKeeper,
a.AccountKeeper.GetModuleAddress(txfeestypes.ModuleName),
)
a.TransferStack = packetforwardmiddleware.NewIBCMiddleware(
a.TransferStack,
Expand Down
68 changes: 29 additions & 39 deletions x/bridgingfee/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/cosmos/ibc-go/v7/modules/core/exported"
"github.com/dymensionxyz/sdk-utils/utils/uevent"
"github.com/dymensionxyz/sdk-utils/utils/uibc"
"github.com/osmosis-labs/osmosis/v15/osmoutils"
txfeeskeeper "github.com/osmosis-labs/osmosis/v15/x/txfees/keeper"

commontypes "github.com/dymensionxyz/dymension/v3/x/common/types"
Expand All @@ -32,7 +33,6 @@ type IBCModule struct {
delayedAckKeeper delayedackkeeper.Keeper
transferKeeper transferkeeper.Keeper
txFeesKeeper txfeeskeeper.Keeper
feeModuleAddr sdk.AccAddress
}

func NewIBCModule(
Expand All @@ -41,15 +41,13 @@ func NewIBCModule(
delayedAckKeeper delayedackkeeper.Keeper,
transferKeeper transferkeeper.Keeper,
txFeesKeeper txfeeskeeper.Keeper,
feeModuleAddr sdk.AccAddress,
) *IBCModule {
return &IBCModule{
IBCModule: next,
rollappKeeper: rollappKeeper,
delayedAckKeeper: delayedAckKeeper,
transferKeeper: transferKeeper,
txFeesKeeper: txFeesKeeper,
feeModuleAddr: feeModuleAddr,
}
}

Expand Down Expand Up @@ -85,44 +83,36 @@ func (w *IBCModule) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re
return w.IBCModule.OnRecvPacket(ctx, packet, relayer)
}

// Use the packet as a basis for a fee transfer
feeData := transfer
fee := w.delayedAckKeeper.BridgingFeeFromAmt(ctx, transfer.MustAmountInt())
feeData.Amount = fee.String()
feeData.Receiver = w.feeModuleAddr.String()
// handle the packet transfer
ack := w.IBCModule.OnRecvPacket(ctx, packet, relayer)
if !ack.Success() {
return ack
}
receiver := sdk.MustAccAddressFromBech32(transfer.Receiver)

feeAmt := w.delayedAckKeeper.BridgingFeeFromAmt(ctx, transfer.MustAmountInt())
denomTrace := uibc.GetForeignDenomTrace(packet.GetDestChannel(), transfer.Denom)
feeCoin := sdk.NewCoin(denomTrace.IBCDenom(), feeAmt)

// No event emitted, as we called the transfer keeper directly (vs the transfer middleware)
err = w.transferKeeper.OnRecvPacket(ctx, packet, feeData.FungibleTokenPacketData)
// charge the bridging fee in cache context
err = osmoutils.ApplyFuncIfNoError(ctx, func(ctx sdk.Context) error {
return w.txFeesKeeper.ChargeFeesFromPayer(ctx, receiver, feeCoin, nil)
})
if err != nil {
l.Error("Charge bridging fee.", "err", err)
// We continue as we don't want the fee charge to fail the transfer in any case
fee = sdk.ZeroInt()
} else {
// Charge the fee from the txfees module account: construct the IBC denom and use it for the fee coin.
denomTrace := uibc.GetForeignDenomTrace(packet.GetDestChannel(), feeData.Denom)
feeCoin := sdk.NewCoin(denomTrace.IBCDenom(), fee)

err = w.txFeesKeeper.ChargeFees(ctx, feeCoin, nil, transfer.Receiver)
if err != nil {
// We continue as we don't want the fee charge to fail the transfer in any case.
// Also, the fee was already successfully sent to x/txfees and charging will be retried at the epoch end.
w.logger(ctx, packet, "OnRecvPacket").Error("Charge bridging fee from x/txfees account.", "err", err)
}

ctx.EventManager().EmitEvent(
sdk.NewEvent(
EventTypeBridgingFee,
sdk.NewAttribute(AttributeKeyFee, fee.String()),
sdk.NewAttribute(sdk.AttributeKeySender, transfer.Sender),
sdk.NewAttribute(transfertypes.AttributeKeyReceiver, transfer.Receiver),
sdk.NewAttribute(transfertypes.AttributeKeyDenom, transfer.Denom),
sdk.NewAttribute(transfertypes.AttributeKeyAmount, transfer.Amount),
),
)
// We continue as we don't want the fee charge to fail the transfer in any case.
l.Error("Charge bridging fee from payer.", "receiver", receiver, "err", err)
}

// transfer the rest to the original recipient
transfer.Amount = transfer.MustAmountInt().Sub(fee).String()
packet.Data = transfer.GetBytes()
return w.IBCModule.OnRecvPacket(ctx, packet, relayer)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
EventTypeBridgingFee,
sdk.NewAttribute(AttributeKeyFee, feeAmt.String()),
sdk.NewAttribute(sdk.AttributeKeySender, transfer.Sender),
sdk.NewAttribute(transfertypes.AttributeKeyReceiver, transfer.Receiver),
sdk.NewAttribute(transfertypes.AttributeKeyDenom, transfer.Denom),
sdk.NewAttribute(transfertypes.AttributeKeyAmount, transfer.Amount),
),
)

return ack
}

0 comments on commit 65cd290

Please sign in to comment.