Skip to content

Commit

Permalink
More checks in keeper and use sdkerrors
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulBernal committed Mar 22, 2024
1 parent 905d8f1 commit 4227bdd
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions x/burn/keeper/msg_server_burn_coins_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package keeper

import (
"context"
"fmt"

errorsmod "cosmossdk.io/errors"
"github.com/BitCannaGlobal/bcna/x/burn/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

// Move coins from sender to Bank account module and then the module burns the coins.
Expand All @@ -14,31 +15,38 @@ func (k msgServer) BurnCoinsAction(goCtx context.Context, msg *types.MsgBurnCoin

creatorAddr, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return nil, fmt.Errorf("the address is not valid")
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid creator address: %s", err)
}

// Validate the coins
coins := sdk.NewCoins(msg.Amount)
if !coins.IsValid() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coins.String())
}

// Get the module's params to verify the allowed denom
params := k.GetParams(ctx)
if msg.Amount.Denom != params.BurnDenom {
return nil, fmt.Errorf("denomination mismatch: expected %s, got %s", params.BurnDenom, msg.Amount.Denom)
return nil, sdkerrors.ErrInvalidAddress.Wrapf("denomination mismatch: expected %s, got %s", params.BurnDenom, msg.Amount.Denom)
}
// Check if it is a valid amount
if msg.Amount.IsZero() || msg.Amount.IsNegative() {
return nil, fmt.Errorf("invalid amount: %s", msg.Amount.String())
// Ensure coins are positive
if !coins.IsAllPositive() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coins.String())
}

// Gets the balance of the sender to check if are there enough coins.
balance := k.bankKeeper.GetBalance(ctx, creatorAddr, msg.Amount.Denom)
if balance.Amount.LT(msg.Amount.Amount) {
return nil, fmt.Errorf("insufficient balance for %s", creatorAddr)
return nil, sdkerrors.ErrInvalidAddress.Wrapf("insufficient balance for %s", creatorAddr)
}

// Send the coins from the creator to the module and later it burns
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, creatorAddr, types.ModuleName, sdk.NewCoins(msg.Amount)); err != nil {
return nil, fmt.Errorf("failed to send coins from account to module: %v", err)
return nil, sdkerrors.ErrInvalidAddress.Wrapf("failed to send coins from account to module: %v", err)
}

if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, sdk.NewCoins(msg.Amount)); err != nil {
return nil, fmt.Errorf("failed to burn coins: %v", err)
return nil, sdkerrors.ErrInvalidAddress.Wrapf("failed to burn coins: %v", err)
}
// Log the successful burn operation
k.Logger(ctx).Info("Burning coins!! ", "signer", msg.Creator, "amount", msg.Amount)
Expand Down

0 comments on commit 4227bdd

Please sign in to comment.