Skip to content

Commit

Permalink
fix: hal audit issue, mint zeta error handling (#697)
Browse files Browse the repository at this point in the history
  • Loading branch information
aldapp7 authored Jun 12, 2023
1 parent ce68376 commit 268b9b9
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions x/fungible/keeper/zeta.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
)

// Mint ZETA (gas token) to the given address
// TODO balanceCoinAfter != expCoin , be replicated in an unit test
func (k *Keeper) MintZetaToEVMAccount(ctx sdk.Context, to sdk.AccAddress, amount *big.Int) error {
balanceCoin := k.bankKeeper.GetBalance(ctx, to, config.BaseDenom)
coins := sdk.NewCoins(sdk.NewCoin(config.BaseDenom, sdk.NewIntFromBigInt(amount)))
Expand All @@ -20,20 +19,28 @@ func (k *Keeper) MintZetaToEVMAccount(ctx sdk.Context, to sdk.AccAddress, amount
}

// Send minted coins to the receiver
if err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, coins); err != nil {
return err
}
err := k.bankKeeper.SendCoinsFromModuleToAccount(ctx, types.ModuleName, to, coins)

// Check expected receiver balance after transfer
balanceCoinAfter := k.bankKeeper.GetBalance(ctx, to, config.BaseDenom)
expCoin := balanceCoin.Add(coins[0])
if err == nil {
// Check expected receiver balance after transfer
balanceCoinAfter := k.bankKeeper.GetBalance(ctx, to, config.BaseDenom)
expCoin := balanceCoin.Add(coins[0])

if ok := balanceCoinAfter.IsEqual(expCoin); !ok {
return sdkerrors.Wrapf(
types.ErrBalanceInvariance,
"invalid coin balance - expected: %v, actual: %v",
expCoin, balanceCoinAfter,
)
if ok := balanceCoinAfter.IsEqual(expCoin); !ok {
err = sdkerrors.Wrapf(
types.ErrBalanceInvariance,
"invalid coin balance - expected: %v, actual: %v",
expCoin, balanceCoinAfter,
)
}
}

if err != nil {
// Revert minting if an error is found.
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, coins); err != nil {
return err
}
return err
}

return nil
Expand Down

0 comments on commit 268b9b9

Please sign in to comment.