Skip to content

Commit

Permalink
add OnRecvPacket hook
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie4x committed Sep 14, 2022
1 parent 3866865 commit 3105e35
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
6 changes: 6 additions & 0 deletions modules/apps/transfer/ibc_module.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ func (im IBCModule) OnRecvPacket(
if err != nil {
ack = types.NewErrorAcknowledgement(err)
}

im.keeper.AfterOnRecvPacket(ctx, data)
}

ctx.EventManager().EmitEvent(
Expand Down Expand Up @@ -222,6 +224,10 @@ func (im IBCModule) OnAcknowledgementPacket(
return err
}

// AfterTransferEnd
denom := types.ParseDenomTrace(data.Denom)
im.keeper.AfterTransferEnd(ctx, data, denom.BaseDenom)

ctx.EventManager().EmitEvent(
sdk.NewEvent(
types.EventTypePacket,
Expand Down
7 changes: 6 additions & 1 deletion modules/apps/transfer/keeper/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import (
"github.com/cosmos/ibc-go/v3/modules/apps/transfer/types"
)

// AfterEpochEnd executes the indicated hook after epochs ends
// AfterEpochEnd executes the indicated hook after Transfer ends
func (k Keeper) AfterTransferEnd(ctx sdk.Context, packet types.FungibleTokenPacketData, base_denom string) {
k.hooks.AfterTransferEnd(ctx, packet, base_denom)
}

// AfterOnRecvPacket executes the indicated hook after OnRecvPacket ends
func (k Keeper) AfterOnRecvPacket(ctx sdk.Context, packet types.FungibleTokenPacketData) {
k.hooks.AfterOnRecvPacket(ctx, packet)
}
15 changes: 9 additions & 6 deletions modules/apps/transfer/keeper/relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
// 4. A -> C : sender chain is sink zone. Denom upon receiving: 'C/B/denom'
// 5. C -> B : sender chain is sink zone. Denom upon receiving: 'B/denom'
// 6. B -> A : sender chain is sink zone. Denom upon receiving: 'denom'

// nova -
func (k Keeper) SendTransfer(
ctx sdk.Context,
sourcePort,
Expand Down Expand Up @@ -88,12 +90,15 @@ func (k Keeper) SendTransfer(
}

// NOTE: denomination and hex hash correctness checked during msg.ValidateBasic
// uatom
fullDenomPath := token.Denom

var err error

// deconstruct the token denomination into the denomination trace info
// to determine if the sender is the source chain
// uatom -> osmo -> nova
// ibc/hash
if strings.HasPrefix(token.Denom, "ibc/") {
fullDenomPath, err = k.DenomPathFromHash(ctx, token.Denom)
if err != nil {
Expand Down Expand Up @@ -122,7 +127,6 @@ func (k Keeper) SendTransfer(
); err != nil {
return err
}

} else {
labels = append(labels, telemetry.NewLabel(coretypes.LabelSource, "false"))

Expand Down Expand Up @@ -243,6 +247,7 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to receive funds", receiver)
}

// portID/channelID/basedenom
// unescrow tokens
escrowAddress := types.GetEscrowAddress(packet.GetDestPort(), packet.GetDestChannel())
if err := k.bankKeeper.SendCoins(ctx, escrowAddress, receiver, sdk.NewCoins(token)); err != nil {
Expand Down Expand Up @@ -270,7 +275,6 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
),
)
}()

return nil
}

Expand Down Expand Up @@ -313,6 +317,9 @@ func (k Keeper) OnRecvPacket(ctx sdk.Context, packet channeltypes.Packet, data t
return err
}

// hook 실행
// k.AfterOnRecvPacket(ctx, packet)

defer func() {
if transferAmount.IsInt64() {
telemetry.SetGaugeWithLabels(
Expand Down Expand Up @@ -345,10 +352,6 @@ func (k Keeper) OnAcknowledgementPacket(ctx sdk.Context, packet channeltypes.Pac
default:
// the acknowledgement succeeded on the receiving chain so nothing
// needs to be executed and no error needs to be returned

//TODO : add filter
denom := types.ParseDenomTrace(data.Denom)
k.AfterTransferEnd(ctx, data, denom.BaseDenom)
return nil
}
}
Expand Down
7 changes: 7 additions & 0 deletions modules/apps/transfer/types/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type TransferHooks interface {
AfterTransferEnd(ctx sdk.Context, data FungibleTokenPacketData, base_denom string)
AfterOnRecvPacket(ctx sdk.Context, data FungibleTokenPacketData)
}

var _ TransferHooks = MultiTransferHooks{}
Expand All @@ -21,3 +22,9 @@ func (h MultiTransferHooks) AfterTransferEnd(ctx sdk.Context, data FungibleToken
h[i].AfterTransferEnd(ctx, data, base_denom)
}
}

func (h MultiTransferHooks) AfterOnRecvPacket(ctx sdk.Context, data FungibleTokenPacketData) {
for i := range h {
h[i].AfterOnRecvPacket(ctx, data)
}
}

0 comments on commit 3105e35

Please sign in to comment.