Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Halborn audit issue 14, revert mint if failed transfer of ZETA #697

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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