Skip to content

Commit

Permalink
Merge pull request #340 from BitCannaGlobal/v047-burn-module-add-params
Browse files Browse the repository at this point in the history
V047 burn module add params
  • Loading branch information
RaulBernal authored Mar 25, 2024
2 parents 9f86f1d + 4227bdd commit 5f3076d
Show file tree
Hide file tree
Showing 12 changed files with 194 additions and 94 deletions.
5 changes: 2 additions & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ import (
govclient "github.com/cosmos/cosmos-sdk/x/gov/client"
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govv1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1"
govv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/group"
groupkeeper "github.com/cosmos/cosmos-sdk/x/group/keeper"
Expand Down Expand Up @@ -192,7 +191,7 @@ var (
stakingtypes.NotBondedPoolName: {authtypes.Burner, authtypes.Staking},
govtypes.ModuleName: {authtypes.Burner},
ibctransfertypes.ModuleName: {authtypes.Minter, authtypes.Burner},
burnmoduletypes.ModuleName: {authtypes.Minter, authtypes.Burner},
burnmoduletypes.ModuleName: {authtypes.Burner},
// nft.ModuleName: nil,
}
)
Expand Down Expand Up @@ -956,7 +955,7 @@ func initParamsKeeper(appCodec codec.BinaryCodec, legacyAmino *codec.LegacyAmino
paramsKeeper.Subspace(minttypes.ModuleName)
paramsKeeper.Subspace(distrtypes.ModuleName)
paramsKeeper.Subspace(slashingtypes.ModuleName)
paramsKeeper.Subspace(govtypes.ModuleName).WithKeyTable(govv1.ParamKeyTable()) //nolint:staticcheck
paramsKeeper.Subspace(govtypes.ModuleName) //.WithKeyTable(govv1.ParamKeyTable()) //nolint: staticcheck // SA1019 //https://github.com/cosmos/cosmos-sdk/discussions/19832
paramsKeeper.Subspace(crisistypes.ModuleName)
paramsKeeper.Subspace(ibctransfertypes.ModuleName)
paramsKeeper.Subspace(ibcexported.ModuleName)
Expand Down
12 changes: 12 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ paths:
params:
description: params holds all the parameters of this module.
type: object
properties:
burn_denom:
type: string
title: denom allowed to be burned
description: >-
QueryParamsResponse is response type for the Query/Params RPC
method.
Expand Down Expand Up @@ -46923,13 +46927,21 @@ definitions:
type: object
bcna.burn.Params:
type: object
properties:
burn_denom:
type: string
title: denom allowed to be burned
description: Params defines the parameters for the module.
bcna.burn.QueryParamsResponse:
type: object
properties:
params:
description: params holds all the parameters of this module.
type: object
properties:
burn_denom:
type: string
title: denom allowed to be burned
description: QueryParamsResponse is response type for the Query/Params RPC method.
cosmos.base.v1beta1.Coin:
type: object
Expand Down
1 change: 1 addition & 0 deletions proto/bcna/burn/params.proto
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ option go_package = "github.com/BitCannaGlobal/bcna/x/burn/types";
// Params defines the parameters for the module.
message Params {
option (gogoproto.goproto_stringer) = false;
string burn_denom = 1; // denom allowed to be burned

}
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
44 changes: 25 additions & 19 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,36 +15,41 @@ 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)
}

// 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")
// Validate the coins
coins := sdk.NewCoins(msg.Amount)
if !coins.IsValid() {
return nil, errorsmod.Wrap(sdkerrors.ErrInvalidCoins, coins.String())
}
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)
}

// Get the module's params to verify the allowed denom
params := k.GetParams(ctx)
if msg.Amount.Denom != params.BurnDenom {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("denomination mismatch: expected %s, got %s", params.BurnDenom, msg.Amount.Denom)
}
// 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.
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, sdkerrors.ErrInvalidAddress.Wrapf("insufficient balance for %s", creatorAddr)
}

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, sdkerrors.ErrInvalidAddress.Wrapf("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, 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.Coins)
k.Logger(ctx).Info("Burning coins!! ", "signer", msg.Creator, "amount", msg.Amount)

return &types.MsgBurnCoinsActionResponse{}, nil
}
5 changes: 3 additions & 2 deletions x/burn/keeper/params.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

// GetParams get all parameters as types.Params
func (k Keeper) GetParams(ctx sdk.Context) types.Params {
return types.NewParams()
func (k Keeper) GetParams(ctx sdk.Context) (params types.Params) {
k.paramstore.GetParamSet(ctx, &params)
return params
}

// SetParams set the params
Expand Down
7 changes: 5 additions & 2 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 Down Expand Up @@ -44,5 +44,8 @@ func (msg *MsgBurnCoinsAction) ValidateBasic() error {
if err != nil {
return fmt.Errorf("invalid creator address: %v: %w", err, errors.New("invalid address"))
}
if msg.Amount.IsNegative() || msg.Amount.IsZero() || !msg.Amount.IsValid() {
return fmt.Errorf("amount must be positive or valid")
}
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
49 changes: 41 additions & 8 deletions x/burn/types/params.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,67 @@
package types

import (
"fmt"

paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"gopkg.in/yaml.v2"
)

var _ paramtypes.ParamSet = (*Params)(nil)

// ParamKeyTable the param key table for launch module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}
// Keys for parameter access
var (
KeyBurnDenom = []byte("BurnDenom")
)

// // Params defines the parameters for the module.
// type Params struct {
// BurnDenom string `json:"burn_denom" yaml:"burn_denom"`
// }

// NewParams creates a new Params instance
func NewParams() Params {
return Params{}
func NewParams(burnDenom string) Params {
return Params{
BurnDenom: burnDenom,
}
}

// DefaultParams returns a default set of parameters
func DefaultParams() Params {
return NewParams()
return Params{
BurnDenom: "ubcna",
}
}

// ParamKeyTable the param key table for the module
func ParamKeyTable() paramtypes.KeyTable {
return paramtypes.NewKeyTable().RegisterParamSet(&Params{})
}

// ParamSetPairs get the params.ParamSet
func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs {
return paramtypes.ParamSetPairs{}
return paramtypes.ParamSetPairs{
paramtypes.NewParamSetPair(KeyBurnDenom, &p.BurnDenom, validateBurnDenom),
}
}

// Validate validates the set of params
func (p Params) Validate() error {
if err := validateBurnDenom(p.BurnDenom); err != nil {
return err
}
return nil
}

// ValidateBurnDenom validates the BurnDenom parameter
func validateBurnDenom(i interface{}) error {
v, ok := i.(string)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v == "" {
return fmt.Errorf("burn denom cannot be empty")
}
return nil
}

Expand Down
Loading

0 comments on commit 5f3076d

Please sign in to comment.