From 5368c1dd5ca1a7fca7f5e4baadaebcbf5ff965f2 Mon Sep 17 00:00:00 2001 From: Raul Bernal Date: Fri, 22 Mar 2024 10:29:19 +0100 Subject: [PATCH] Allow 1 denom in place of list of denoms --- proto/bcna/burn/tx.proto | 4 +- x/burn/client/cli/tx_burn_coins_action.go | 9 ++- x/burn/keeper/msg_server_burn_coins_action.go | 34 ++++---- x/burn/types/message_burn_coins_action.go | 9 ++- .../types/message_burn_coins_action_test.go | 4 +- x/burn/types/tx.pb.go | 79 ++++++++----------- 6 files changed, 66 insertions(+), 73 deletions(-) diff --git a/proto/bcna/burn/tx.proto b/proto/bcna/burn/tx.proto index 9b962cd0..b521defb 100644 --- a/proto/bcna/burn/tx.proto +++ b/proto/bcna/burn/tx.proto @@ -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 {} diff --git a/x/burn/client/cli/tx_burn_coins_action.go b/x/burn/client/cli/tx_burn_coins_action.go index 33f8515d..d727c1c2 100644 --- a/x/burn/client/cli/tx_burn_coins_action.go +++ b/x/burn/client/cli/tx_burn_coins_action.go @@ -1,6 +1,7 @@ package cli import ( + "fmt" "strconv" "github.com/BitCannaGlobal/bcna/x/burn/types" @@ -19,14 +20,14 @@ 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( @@ -34,7 +35,7 @@ func CmdBurnCoinsAction() *cobra.Command { argCoins, ) if err := msg.ValidateBasic(); err != nil { - return err + return fmt.Errorf("message validation failed: %w", err) } return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) }, diff --git a/x/burn/keeper/msg_server_burn_coins_action.go b/x/burn/keeper/msg_server_burn_coins_action.go index 754752ed..d74205b4 100644 --- a/x/burn/keeper/msg_server_burn_coins_action.go +++ b/x/burn/keeper/msg_server_burn_coins_action.go @@ -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 } diff --git a/x/burn/types/message_burn_coins_action.go b/x/burn/types/message_burn_coins_action.go index f5c700e8..ac36fa6b 100644 --- a/x/burn/types/message_burn_coins_action.go +++ b/x/burn/types/message_burn_coins_action.go @@ -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, } } @@ -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 } diff --git a/x/burn/types/message_burn_coins_action_test.go b/x/burn/types/message_burn_coins_action_test.go index b1e0b534..12ecb21e 100644 --- a/x/burn/types/message_burn_coins_action_test.go +++ b/x/burn/types/message_burn_coins_action_test.go @@ -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), }, }, } diff --git a/x/burn/types/tx.pb.go b/x/burn/types/tx.pb.go index 389b01ad..16831910 100644 --- a/x/burn/types/tx.pb.go +++ b/x/burn/types/tx.pb.go @@ -30,8 +30,8 @@ var _ = math.Inf const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type MsgBurnCoinsAction struct { - Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` - Coins []types.Coin `protobuf:"bytes,2,rep,name=coins,proto3" json:"coins"` + Creator string `protobuf:"bytes,1,opt,name=creator,proto3" json:"creator,omitempty"` + Amount types.Coin `protobuf:"bytes,2,opt,name=amount,proto3" json:"amount"` } func (m *MsgBurnCoinsAction) Reset() { *m = MsgBurnCoinsAction{} } @@ -74,11 +74,11 @@ func (m *MsgBurnCoinsAction) GetCreator() string { return "" } -func (m *MsgBurnCoinsAction) GetCoins() []types.Coin { +func (m *MsgBurnCoinsAction) GetAmount() types.Coin { if m != nil { - return m.Coins + return m.Amount } - return nil + return types.Coin{} } type MsgBurnCoinsActionResponse struct { @@ -126,24 +126,24 @@ func init() { proto.RegisterFile("bcna/burn/tx.proto", fileDescriptor_7b8a0e462b var fileDescriptor_7b8a0e462b5fa7e6 = []byte{ // 281 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x90, 0x4f, 0x4b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0x33, 0xcc, 0xe8, 0x33, 0x39, 0xfb, 0x3f, 0xc8, 0x15, 0xff, 0x5b, 0x90, 0xef, - 0x02, 0xe2, 0x9b, 0xbd, 0xf6, 0x96, 0x9f, 0x3c, 0xae, 0x6a, 0x16, 0xae, 0x6b, 0x16, 0xfe, 0xd4, - 0x2c, 0xfc, 0x6c, 0x58, 0xb0, 0x6e, 0x58, 0xf0, 0xd5, 0xb0, 0xe0, 0xe5, 0x36, 0xcb, 0xdd, 0x5b, - 0xa5, 0xb8, 0x86, 0x85, 0x48, 0x72, 0x37, 0x95, 0xd6, 0xca, 0xa7, 0x39, 0x28, 0x39, 0x17, 0x6d, - 0xdb, 0xcb, 0x4d, 0xdf, 0x1f, 0x85, 0x41, 0x75, 0xdc, 0x56, 0x76, 0xf7, 0x1b, 0x00, 0x00, 0xff, - 0xff, 0x6b, 0x2b, 0xe4, 0xa4, 0x89, 0x01, 0x00, 0x00, + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x90, 0x41, 0x4b, 0xc3, 0x30, + 0x1c, 0xc5, 0x1b, 0x95, 0xc9, 0xe2, 0x41, 0x08, 0x1e, 0x66, 0xd1, 0x38, 0x06, 0xc2, 0x40, 0x48, + 0xd8, 0x3c, 0x78, 0xb6, 0x43, 0x3c, 0xed, 0xd2, 0x8b, 0xe0, 0x41, 0x48, 0x42, 0x88, 0x85, 0x35, + 0xff, 0xd2, 0xa4, 0x32, 0xbf, 0x85, 0x1f, 0x6b, 0xc7, 0x1d, 0x3d, 0x89, 0xb4, 0x5f, 0x44, 0xd2, + 0x76, 0x1e, 0x1c, 0x78, 0x4b, 0xde, 0x7b, 0xbc, 0x5f, 0xf2, 0x30, 0x91, 0xca, 0x0a, 0x2e, 0xab, + 0xd2, 0x72, 0xbf, 0x66, 0x45, 0x09, 0x1e, 0xc8, 0x30, 0x68, 0x2c, 0x68, 0xf1, 0x99, 0x01, 0x03, + 0xad, 0xca, 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00, } // Reference imports to suppress errors if they are not otherwise used. @@ -246,20 +246,16 @@ func (m *MsgBurnCoinsAction) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l - if len(m.Coins) > 0 { - for iNdEx := len(m.Coins) - 1; iNdEx >= 0; iNdEx-- { - { - size, err := m.Coins[iNdEx].MarshalToSizedBuffer(dAtA[:i]) - if err != nil { - return 0, err - } - i -= size - i = encodeVarintTx(dAtA, i, uint64(size)) - } - i-- - dAtA[i] = 0x12 + { + size, err := m.Amount.MarshalToSizedBuffer(dAtA[:i]) + if err != nil { + return 0, err } + i -= size + i = encodeVarintTx(dAtA, i, uint64(size)) } + i-- + dAtA[i] = 0x12 if len(m.Creator) > 0 { i -= len(m.Creator) copy(dAtA[i:], m.Creator) @@ -314,12 +310,8 @@ func (m *MsgBurnCoinsAction) Size() (n int) { if l > 0 { n += 1 + l + sovTx(uint64(l)) } - if len(m.Coins) > 0 { - for _, e := range m.Coins { - l = e.Size() - n += 1 + l + sovTx(uint64(l)) - } - } + l = m.Amount.Size() + n += 1 + l + sovTx(uint64(l)) return n } @@ -401,7 +393,7 @@ func (m *MsgBurnCoinsAction) Unmarshal(dAtA []byte) error { iNdEx = postIndex case 2: if wireType != 2 { - return fmt.Errorf("proto: wrong wireType = %d for field Coins", wireType) + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) } var msglen int for shift := uint(0); ; shift += 7 { @@ -428,8 +420,7 @@ func (m *MsgBurnCoinsAction) Unmarshal(dAtA []byte) error { if postIndex > l { return io.ErrUnexpectedEOF } - m.Coins = append(m.Coins, types.Coin{}) - if err := m.Coins[len(m.Coins)-1].Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + if err := m.Amount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { return err } iNdEx = postIndex