diff --git a/cctp.go b/cctp.go index b624fac..6bd0fae 100644 --- a/cctp.go +++ b/cctp.go @@ -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" @@ -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, @@ -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, @@ -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()) } } diff --git a/middleware.go b/middleware.go index 1060a3d..63deef4 100644 --- a/middleware.go +++ b/middleware.go @@ -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) @@ -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)) }