Skip to content

Commit

Permalink
Merge pull request #7 from oraidex/add-event-ibc-auto-forward
Browse files Browse the repository at this point in the history
Dang/Add event ibc auto forward
  • Loading branch information
GNaD13 authored Jan 8, 2025
2 parents a15a1f1 + cb1df1d commit 1272964
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 12 deletions.
7 changes: 6 additions & 1 deletion module/x/gravity/keeper/attestation_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,12 @@ func (a AttestationHandler) sendCoinToCosmosAccount(
}

err = a.addToIbcAutoForwardQueue(ctx, claim.CosmosReceiver, coin, sourceChannel, claim)

// emit chain id for bridge monitor
bridgeChainId := strconv.Itoa(int(a.keeper.GetBridgeChainID(ctx, claim.EvmChainPrefix)))
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeSendToCosmosIbcAutoForwardPending,
sdk.NewAttribute(types.AttributeKeyBridgeChainID, bridgeChainId),
))
if err != nil {
a.keeper.logger(ctx).Error(
"SendToCosmos IBC auto forwarding failed, sending to local gravity account instead with error: ", err.Error(),
Expand Down
23 changes: 21 additions & 2 deletions module/x/gravity/keeper/ibc_auto_forward.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package keeper

import (
"fmt"
"strconv"
"time"

"github.com/Gravity-Bridge/Gravity-Bridge/module/x/gravity/types"
Expand Down Expand Up @@ -205,12 +206,18 @@ func (k Keeper) ProcessNextPendingIbcAutoForward(ctx sdk.Context, evmChainPrefix

// Make the ibc-transfer attempt
wCtx := sdk.WrapSDKContext(ctx)
_, recoverableErr := k.ibcTransferKeeper.Transfer(wCtx, &msgTransfer)
msgResponse, recoverableErr := k.ibcTransferKeeper.Transfer(wCtx, &msgTransfer)
ctx = sdk.UnwrapSDKContext(wCtx)

// Log + emit event
if recoverableErr == nil {
k.logEmitIbcForwardSuccessEvent(ctx, *forward, msgTransfer)
k.logEmitIbcForwardSuccessEvent(
ctx,
strconv.Itoa(int(k.GetBridgeChainID(ctx, evmChainPrefix))), // we want to check that tx bridge from
*forward,
msgTransfer,
msgResponse,
)
} else {
// Funds have already been sent to the fallback user, emit a failure log
/*
Expand Down Expand Up @@ -266,8 +273,10 @@ func thirtyDaysInFuture(ctx sdk.Context) time.Time {
// EventSendToCosmosExecutedIbcAutoForward type event
func (k Keeper) logEmitIbcForwardSuccessEvent(
ctx sdk.Context,
bridgeChainId string,
forward types.PendingIbcAutoForward,
msgTransfer ibctransfertypes.MsgTransfer,
msgTransferResponse *ibctransfertypes.MsgTransferResponse,
) {
k.logger(ctx).Info("SendToCosmos IBC Auto-Forward", "ibcReceiver", forward.ForeignReceiver, "denom", forward.Token.Denom,
"amount", forward.Token.Amount.String(), "ibc-port", msgTransfer.SourcePort, "ibcChannel", forward.IbcChannel,
Expand All @@ -284,6 +293,16 @@ func (k Keeper) logEmitIbcForwardSuccessEvent(
TimeoutHeight: msgTransfer.TimeoutHeight.String(),
TimeoutTime: fmt.Sprint(msgTransfer.TimeoutTimestamp),
})
ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeSendToCosmosIbcAutoForward,
sdk.NewAttribute(types.AttributeKeyBridgeChainID, bridgeChainId),
sdk.NewAttribute(types.AttributeKeyBatchNonce, fmt.Sprint(forward.EventNonce)),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSequence, strconv.FormatUint(msgTransferResponse.Sequence, 10)),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSrcPort, msgTransfer.SourcePort),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSrcChannel, msgTransfer.SourceChannel),
),
})
}

// logEmitIbcForwardFailureEvent logs failed IBC Auto-Forwarding and emits a EventSendToCosmosLocal type event
Expand Down
16 changes: 16 additions & 0 deletions module/x/gravity/keeper/ibc_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper

import (
"fmt"
"strconv"
"strings"

"github.com/Gravity-Bridge/Gravity-Bridge/module/x/gravity/types"
Expand Down Expand Up @@ -175,6 +176,21 @@ func (k Keeper) OnRecvPacket(
return channeltypes.NewErrorAcknowledgement(err.Error())
}

ctx.EventManager().EmitEvents(sdk.Events{
sdk.NewEvent(
types.EventTypeIbcAutoForwardSendToEvmPending,
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSequence, strconv.Itoa(int(packet.Sequence))),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSrcChannel, packet.SourceChannel),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardSrcPort, packet.SourcePort),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardDstChannel, packet.DestinationChannel),
sdk.NewAttribute(types.AttributeKeyIbcAutoForwardDstPort, packet.DestinationPort),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.GetBridgeChainID(ctx, evmChainPrefix)))),
sdk.NewAttribute(types.AttributeKeyContract, k.GetBridgeContractAddress(ctx, evmChainPrefix).GetAddress().Hex()),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, strconv.Itoa(int(txID))),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(txID)),
),
})

ctx.EventManager().EmitTypedEvent(
&types.EventOutgoingTxId{
Message: "send_to_eth",
Expand Down
27 changes: 18 additions & 9 deletions module/x/gravity/types/events.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package types

const (
EventTypeObservation = "observation"
EventTypeOutgoingBatch = "outgoing_batch"
EventTypeMultisigUpdateRequest = "multisig_update_request"
EventTypeOutgoingBatchCanceled = "outgoing_batch_canceled"
EventTypeOutgoingLogicCallCanceled = "outgoing_logic_call_canceled"
EventTypeBridgeWithdrawalReceived = "withdrawal_received"
EventTypeBridgeDepositReceived = "deposit_received"
EventTypeBridgeWithdrawCanceled = "withdraw_canceled"
EventTypeInvalidSendToCosmosReceiver = "invalid_send_to_cosmos_receiver"
EventTypeObservation = "observation"
EventTypeOutgoingBatch = "outgoing_batch"
EventTypeMultisigUpdateRequest = "multisig_update_request"
EventTypeOutgoingBatchCanceled = "outgoing_batch_canceled"
EventTypeOutgoingLogicCallCanceled = "outgoing_logic_call_canceled"
EventTypeBridgeWithdrawalReceived = "withdrawal_received"
EventTypeBridgeDepositReceived = "deposit_received"
EventTypeBridgeWithdrawCanceled = "withdraw_canceled"
EventTypeInvalidSendToCosmosReceiver = "invalid_send_to_cosmos_receiver"
EventTypeSendToCosmosIbcAutoForward = "send_to_cosmos_ibc_auto_forward"
EventTypeSendToCosmosIbcAutoForwardPending = "send_to_cosmos_ibc_auto_forward_pending"
EventTypeIbcAutoForwardSendToEvmPending = "ibc_auto_forward_send_to_evm_pending"

AttributeKeyAttestationID = "attestation_id"
AttributeKeyBatchConfirmKey = "batch_confirm_key"
Expand All @@ -29,6 +32,12 @@ const (
AttributeKeyBadEthSignature = "bad_eth_signature"
AttributeKeyBadEthSignatureSubject = "bad_eth_signature_subject"

AttributeKeyIbcAutoForwardSequence = "packet_sequence"
AttributeKeyIbcAutoForwardSrcPort = "packet_src_port"
AttributeKeyIbcAutoForwardSrcChannel = "packet_src_channel"
AttributeKeyIbcAutoForwardDstPort = "packet_dst_port"
AttributeKeyIbcAutoForwardDstChannel = "packet_dst_channel"

AttributeKeySendToCosmosAmount = "msg_send_to_cosmsos_amount"
AttributeKeySendToCosmosNonce = "msg_send_to_cosmsos_nonce"
AttributeKeySendToCosmosToken = "msg_send_to_cosmsos_token"
Expand Down

0 comments on commit 1272964

Please sign in to comment.