Skip to content

Commit

Permalink
refactor: batch mint/transfer tokens when bridge call (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
zakir-code authored Feb 27, 2024
1 parent a2a14a3 commit 11e36d6
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions x/crosschain/keeper/relay_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 11e36d6

Please sign in to comment.