Skip to content

Commit

Permalink
emitting events on exit from middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
spoo-bar committed Oct 29, 2024
1 parent 0eb7817 commit bf596b8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 2 deletions.
46 changes: 44 additions & 2 deletions cctp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package autocctp

import (
"strconv"

"autocctp.dev/types"
cctptypes "github.com/circlefin/noble-cctp/x/cctp/types"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -11,11 +13,17 @@ import (
func (m Middleware) forwardViaCCTP(ctx sdk.Context, forwardCoin sdk.Coin, autocctpMemo types.Memo, sender sdk.AccAddress) exported.Acknowledgement {
// forwarding amount to sender address to initiate deposit for burn
if err := m.bankKeeper.SendCoins(ctx, types.ModuleAddress, sender, sdk.NewCoins(forwardCoin)); err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to send coins from autocctp module to sender",
sdk.NewAttribute("from", types.ModuleAddress.String()),
sdk.NewAttribute("to", sender.String()),
sdk.NewAttribute("amount", forwardCoin.String()),
sdk.NewAttribute("error", err.Error()),
))
return channeltypes.NewErrorAcknowledgement(err)
}
destinationCaller := autocctpMemo.GetDestinationCaller()
if len(destinationCaller) == 0 {
// no destination caller, so DepositForBurn
m.logger.Debug("no destination caller specified, calling DepositForBurn", "memo", autocctpMemo)
msg := &cctptypes.MsgDepositForBurn{
From: sender.String(),
Amount: forwardCoin.Amount,
Expand All @@ -25,11 +33,27 @@ func (m Middleware) forwardViaCCTP(ctx sdk.Context, forwardCoin sdk.Coin, autocc
}
res, err := m.server.DepositForBurn(ctx, msg)
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to deposit for burn",
sdk.NewAttribute("from", sender.String()),
sdk.NewAttribute("amount", forwardCoin.Amount.String()),
sdk.NewAttribute("destination_domain", strconv.Itoa(int(autocctpMemo.GetDestinationDomain()))),
sdk.NewAttribute("mint_recipient", string(autocctpMemo.GetMintRecipient())),
sdk.NewAttribute("burn_token", forwardCoin.Denom),
sdk.NewAttribute("error", err.Error()),
))
return channeltypes.NewErrorAcknowledgement(err)
}
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: successfully deposited for burn",
sdk.NewAttribute("from", sender.String()),
sdk.NewAttribute("amount", forwardCoin.Amount.String()),
sdk.NewAttribute("destination_domain", strconv.Itoa(int(autocctpMemo.GetDestinationDomain()))),
sdk.NewAttribute("mint_recipient", string(autocctpMemo.GetMintRecipient())),
sdk.NewAttribute("burn_token", forwardCoin.Denom),
sdk.NewAttribute("nonce", strconv.FormatUint(res.Nonce, 10)),
))
return channeltypes.NewResultAcknowledgement(types.NewResponse(res.Nonce).Bytes())
} else {
// destination caller specified, so DepositForBurnWithCaller
m.logger.Debug("destination caller specified, calling DepositForBurnWithCaller", "memo", autocctpMemo)
msg := &cctptypes.MsgDepositForBurnWithCaller{
From: sender.String(),
Amount: forwardCoin.Amount,
Expand All @@ -40,8 +64,26 @@ func (m Middleware) forwardViaCCTP(ctx sdk.Context, forwardCoin sdk.Coin, autocc
}
res, err := m.server.DepositForBurnWithCaller(ctx, msg)
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to deposit for burn with caller",
sdk.NewAttribute("from", sender.String()),
sdk.NewAttribute("amount", forwardCoin.Amount.String()),
sdk.NewAttribute("destination_domain", strconv.Itoa(int(autocctpMemo.GetDestinationDomain()))),
sdk.NewAttribute("mint_recipient", string(autocctpMemo.GetMintRecipient())),
sdk.NewAttribute("burn_token", forwardCoin.Denom),
sdk.NewAttribute("destination_caller", string(destinationCaller)),
sdk.NewAttribute("error", err.Error()),
))
return channeltypes.NewErrorAcknowledgement(err)
}
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: successfully deposited for burn with caller",
sdk.NewAttribute("from", sender.String()),
sdk.NewAttribute("amount", forwardCoin.Amount.String()),
sdk.NewAttribute("destination_domain", strconv.Itoa(int(autocctpMemo.GetDestinationDomain()))),
sdk.NewAttribute("mint_recipient", string(autocctpMemo.GetMintRecipient())),
sdk.NewAttribute("burn_token", forwardCoin.Denom),
sdk.NewAttribute("destination_caller", string(destinationCaller)),
sdk.NewAttribute("nonce", strconv.FormatUint(res.Nonce, 10)),
))
return channeltypes.NewResultAcknowledgement(types.NewResponse(res.Nonce).Bytes())
}
}
18 changes: 18 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,15 @@ func (m Middleware) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re
if !shouldProcess {
return ack
}
m.logger.Debug("processing ICS20 packet by the autocctp middleware", "packet", packet)
autocctpMemo, err := types.ParseMemo(ics20packetData.GetMemo())
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to parse memo", sdk.NewAttribute("memo", ics20packetData.GetMemo()), sdk.NewAttribute("error", err.Error())))
return channeltypes.NewErrorAcknowledgement(err)
}
ibcTransferAmount, ok := math.NewIntFromString(ics20packetData.Amount)
if !ok {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to decode packet amount", sdk.NewAttribute("amount", ics20packetData.Amount)))
return channeltypes.NewErrorAcknowledgement(errors.Wrap(types.ErrInvalidICS20Packet, "failed to decode packet amount"))
}
denom := types.GetIBCDenom(packet, ics20packetData)
Expand All @@ -82,16 +85,31 @@ func (m Middleware) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, re
// send fees to fee recipient
feeAmount, err := getFeeAmount(autocctpMemo, ibcTransferAmount)
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to calculate fee amount", sdk.NewAttribute("error", err.Error())))
return channeltypes.NewErrorAcknowledgement(err)
}
m.logger.Debug("sending fees to fee recipient", "feeRecipient", *feeRecipient, "amount", feeAmount, "denom", denom, "memo", autocctpMemo)
if err = m.bankKeeper.SendCoins(ctx, types.ModuleAddress, sdk.MustAccAddressFromBech32(*feeRecipient), sdk.NewCoins(sdk.NewCoin(denom, feeAmount))); err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to send fees to fee recipient",
sdk.NewAttribute("from", types.ModuleAddress.String()),
sdk.NewAttribute("to", *feeRecipient),
sdk.NewAttribute("amount", feeAmount.String()),
sdk.NewAttribute("denom", denom),
sdk.NewAttribute("error", err.Error()),
))
return channeltypes.NewErrorAcknowledgement(errors.Wrap(err, "failed to send fees to fee recipient"))
}
ibcTransferAmount, err = ibcTransferAmount.SafeSub(feeAmount)
if err != nil {
ctx.EventManager().EmitEvent(sdk.NewEvent("autocctp: failed to calculate remaining amount after fee deduction",
sdk.NewAttribute("ibcTransferAmount", ibcTransferAmount.String()),
sdk.NewAttribute("feeAmount", feeAmount.String()),
sdk.NewAttribute("error", err.Error()),
))
return channeltypes.NewErrorAcknowledgement(errors.Wrap(err, "failed to calculate remaining amount after fee deduction"))
}
}
m.logger.Debug("forwarding the rest of the amount to the cctp module", "amount", ibcTransferAmount, "memo", autocctpMemo)
return m.forwardViaCCTP(ctx, sdk.NewCoin(denom, ibcTransferAmount), autocctpMemo, types.GenerateAddress(packet.GetDestChannel(), ics20packetData.Sender))
}

Expand Down

0 comments on commit bf596b8

Please sign in to comment.