Skip to content

Commit

Permalink
Allow 1 denom in place of list of denoms
Browse files Browse the repository at this point in the history
  • Loading branch information
RaulBernal committed Mar 22, 2024
1 parent a3a54eb commit 5368c1d
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 73 deletions.
4 changes: 2 additions & 2 deletions proto/bcna/burn/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ service Msg {
rpc BurnCoinsAction (MsgBurnCoinsAction) returns (MsgBurnCoinsActionResponse);
}
message MsgBurnCoinsAction {
string creator = 1;
repeated cosmos.base.v1beta1.Coin coins = 2 [(gogoproto.nullable) = false];
string creator = 1;
cosmos.base.v1beta1.Coin amount = 2 [(gogoproto.nullable) = false]; // Now is only one coin not a list of coins
}

message MsgBurnCoinsActionResponse {}
Expand Down
9 changes: 5 additions & 4 deletions x/burn/client/cli/tx_burn_coins_action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cli

import (
"fmt"
"strconv"

"github.com/BitCannaGlobal/bcna/x/burn/types"
Expand All @@ -19,22 +20,22 @@ func CmdBurnCoinsAction() *cobra.Command {
Short: "Broadcast message BurnCoinsAction",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argCoins, err := sdk.ParseCoinsNormalized(args[0])
argCoins, err := sdk.ParseCoinNormalized(args[0])
if err != nil {
return err
return fmt.Errorf("failed to parse coin: %w", err)
}

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
return fmt.Errorf("failed to get client context: %w", err)
}

msg := types.NewMsgBurnCoinsAction(
clientCtx.GetFromAddress().String(),
argCoins,
)
if err := msg.ValidateBasic(); err != nil {
return err
return fmt.Errorf("message validation failed: %w", err)
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
Expand Down
34 changes: 16 additions & 18 deletions x/burn/keeper/msg_server_burn_coins_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,32 @@ func (k msgServer) BurnCoinsAction(goCtx context.Context, msg *types.MsgBurnCoin
if err != nil {
return nil, fmt.Errorf("the address is not valid")
}

// Check nulls and valid amounts
if msg.Coins == nil || len(msg.Coins) == 0 {
return nil, fmt.Errorf("no coins specified or the amount is not valid")
// 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)
}
for _, coin := range msg.Coins {
if coin.Amount.LTE(sdk.ZeroInt()) { // Comprueba si la cantidad es menor o igual a cero.
return nil, fmt.Errorf("invalid amount for coin %s, amount must be positive", coin.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())
}

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

if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, creatorAddr, types.ModuleName, msg.Coins); err != nil {
return nil, err
// 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)
}

if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, msg.Coins); err != nil {
return nil, 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)
}
// Log the successful burn operation
k.Logger(ctx).Info("Burning coins!! ", "signer", msg.Creator, "amount", msg.Coins)
k.Logger(ctx).Info("Burning coins!! ", "signer", msg.Creator, "amount", msg.Amount)

return &types.MsgBurnCoinsActionResponse{}, nil
}
9 changes: 6 additions & 3 deletions x/burn/types/message_burn_coins_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ const TypeMsgBurnCoinsAction = "burn_coins_action"

var _ sdk.Msg = &MsgBurnCoinsAction{}

func NewMsgBurnCoinsAction(creator string, coins sdk.Coins) *MsgBurnCoinsAction {
func NewMsgBurnCoinsAction(creator string, amount sdk.Coin) *MsgBurnCoinsAction {
return &MsgBurnCoinsAction{
Creator: creator,
Coins: coins,
Amount: amount,
}
}

Expand All @@ -39,10 +39,13 @@ func (msg *MsgBurnCoinsAction) GetSignBytes() []byte {
return sdk.MustSortJSON(bz)
}

func (msg *MsgBurnCoinsAction) ValidateBasic() error {
func (msg *MsgBurnCoinsAction) ValidateBasic() error { // TODO call IsValid and IsAllPositive
_, err := sdk.AccAddressFromBech32(msg.Creator)
if err != nil {
return fmt.Errorf("invalid creator address: %v: %w", err, errors.New("invalid address"))
}
if msg.Amount.IsNegative() || msg.Amount.IsZero() {
return fmt.Errorf("amount must be positive")
}
return nil
}
4 changes: 2 additions & 2 deletions x/burn/types/message_burn_coins_action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ func TestMsgBurnCoinsAction_ValidateBasic(t *testing.T) {
name: "invalid address",
msg: MsgBurnCoinsAction{
Creator: "invalid_address",
Coins: sdk.Coins{sdk.NewInt64Coin("testcoin", 1000)},
Amount: sdk.NewInt64Coin("testcoin", 1000),
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgBurnCoinsAction{
Creator: sample.AccAddress(),
Coins: sdk.Coins{sdk.NewInt64Coin("testcoin", 1000)},
Amount: sdk.NewInt64Coin("testcoin", 1000),
},
},
}
Expand Down
79 changes: 35 additions & 44 deletions x/burn/types/tx.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5368c1d

Please sign in to comment.