From 11e36d6777c9acdac7d55e4c86d14dd1860c0b15 Mon Sep 17 00:00:00 2001 From: zakir <80246097+zakir-code@users.noreply.github.com> Date: Tue, 27 Feb 2024 14:03:52 +0800 Subject: [PATCH] refactor: batch mint/transfer tokens when bridge call (#231) --- x/crosschain/keeper/relay_transfer.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/x/crosschain/keeper/relay_transfer.go b/x/crosschain/keeper/relay_transfer.go index 35d534156..05d9de3ad 100644 --- a/x/crosschain/keeper/relay_transfer.go +++ b/x/crosschain/keeper/relay_transfer.go @@ -154,25 +154,38 @@ func (k Keeper) bridgeCallERC20Handler( func (k Keeper) bridgeCallTargetCoinsHandler(ctx sdk.Context, receiver common.Address, tokens []common.Address, amounts []*big.Int) (sdk.Coins, error) { tokens, amounts = types.MergeDuplicationERC20(tokens, amounts) - targetCoins := sdk.NewCoins() + + mintCoins := sdk.NewCoins() + unlockCoins := sdk.NewCoins() for i := 0; i < len(tokens); i++ { bridgeToken := k.GetBridgeTokenDenom(ctx, tokens[i].String()) if bridgeToken == nil { return nil, errorsmod.Wrap(types.ErrInvalid, "bridge token is not exist") } - if amounts[i].Cmp(big.NewInt(0)) <= 0 { + amount := sdkmath.NewIntFromBigInt(amounts[i]) + if !amount.IsPositive() { continue } - coin := sdk.NewCoin(bridgeToken.Denom, sdkmath.NewIntFromBigInt(amounts[i])) + coin := sdk.NewCoin(bridgeToken.Denom, amount) isOriginOrConverted := k.erc20Keeper.IsOriginOrConvertedDenom(ctx, bridgeToken.Denom) if !isOriginOrConverted { - if err := k.bankKeeper.MintCoins(ctx, k.moduleName, sdk.NewCoins(coin)); err != nil { - return nil, errorsmod.Wrapf(err, "mint vouchers coins") - } + mintCoins = mintCoins.Add(coin) + } + unlockCoins = unlockCoins.Add(coin) + } + if mintCoins.IsAllPositive() { + if err := k.bankKeeper.MintCoins(ctx, k.moduleName, mintCoins); err != nil { + return nil, errorsmod.Wrapf(err, "mint vouchers coins") } - if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, k.moduleName, receiver.Bytes(), sdk.NewCoins(coin)); err != nil { + } + if unlockCoins.IsAllPositive() { + if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, k.moduleName, receiver.Bytes(), unlockCoins); err != nil { return nil, errorsmod.Wrap(err, "transfer vouchers") } + } + + targetCoins := sdk.NewCoins() + for _, coin := range unlockCoins { targetCoin, err := k.erc20Keeper.ConvertDenomToTarget(ctx, receiver.Bytes(), coin, fxtypes.ParseFxTarget(fxtypes.ERC20Target)) if err != nil { return nil, errorsmod.Wrap(err, "convert to target coin")