Skip to content

Commit

Permalink
fix: delete duplicate events when transfer erc20 (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Feb 27, 2024
1 parent 11d3376 commit 18555fd
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions x/crosschain/keeper/relay_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,27 @@ func (k Keeper) RelayTransferHandler(ctx sdk.Context, eventNonce uint64, targetH
if fxTarget.GetTarget() == fxtypes.ERC20Target {
// transfer to evm
cacheCtx, commit := ctx.CacheContext()
if err := k.transferErc20Handler(cacheCtx, eventNonce, receiver, receiver, coin); err != nil {
receiverHex := common.BytesToAddress(receiver.Bytes())
if err := k.transferErc20Handler(cacheCtx, eventNonce, receiver, receiverHex, coin, true); err != nil {
return err
}
commit()
}
return nil
}

func (k Keeper) transferErc20Handler(ctx sdk.Context, eventNonce uint64, sender, receiver sdk.AccAddress, coin sdk.Coin) error {
receiverEthAddr := common.BytesToAddress(receiver.Bytes())
if err := k.erc20Keeper.TransferAfter(ctx, sender, receiverEthAddr.String(), coin, sdk.NewCoin(coin.Denom, sdkmath.ZeroInt()), false); err != nil {
func (k Keeper) transferErc20Handler(ctx sdk.Context, eventNonce uint64, sender sdk.AccAddress, receiver common.Address, coin sdk.Coin, isEmitEvent bool) error {
if err := k.erc20Keeper.TransferAfter(ctx, sender, receiver.String(), coin, sdk.NewCoin(coin.Denom, sdkmath.ZeroInt()), false); err != nil {
k.Logger(ctx).Error("transfer convert denom failed", "error", err.Error())
return err
}
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeEvmTransfer,
sdk.NewAttribute(sdk.AttributeKeyModule, k.moduleName),
sdk.NewAttribute(types.AttributeKeyEventNonce, fmt.Sprint(eventNonce)),
))
if isEmitEvent {
ctx.EventManager().EmitEvent(sdk.NewEvent(
types.EventTypeEvmTransfer,
sdk.NewAttribute(sdk.AttributeKeyModule, k.moduleName),
sdk.NewAttribute(types.AttributeKeyEventNonce, fmt.Sprint(eventNonce)),
))
}
return nil
}

Expand Down Expand Up @@ -127,14 +129,16 @@ func (k Keeper) bridgeCallERC20Handler(
switch dstChainID {
case types.FxcoreChainID:
// convert coin to erc20
isEmitEvent := true
for _, coin := range targetCoins {
// not convert FX
if coin.Denom == fxtypes.DefaultDenom {
continue
}
if err = k.transferErc20Handler(ctx, eventNonce, senderAddr.Bytes(), receiver, coin); err != nil {
if err = k.transferErc20Handler(ctx, eventNonce, senderAddr.Bytes(), common.BytesToAddress(receiver), coin, isEmitEvent); err != nil {
return err
}
isEmitEvent = false
}
var toAddrPtr *common.Address
if len(to) > 0 {
Expand Down

0 comments on commit 18555fd

Please sign in to comment.