diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index f2e69e2a3..5420e2368 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -75350,7 +75350,11 @@ definitions: description: params holds all the parameters of this module. type: object description: QueryParamsResponse is response type for the Query/Params RPC method. +<<<<<<< HEAD pocket.supplier.MsgUnstakeSupplierResponse: +======= + pocket.supplier.MsgStakeSupplierResponse: +>>>>>>> main type: object pocket.supplier.Params: type: object diff --git a/proto/pocket/supplier/tx.proto b/proto/pocket/supplier/tx.proto index e7b7de825..70e81c614 100644 --- a/proto/pocket/supplier/tx.proto +++ b/proto/pocket/supplier/tx.proto @@ -6,11 +6,18 @@ option go_package = "pocket/x/supplier/types"; // Msg defines the Msg service. service Msg { + rpc StakeSupplier (MsgStakeSupplier) returns (MsgStakeSupplierResponse); rpc UnstakeSupplier (MsgUnstakeSupplier) returns (MsgUnstakeSupplierResponse); } + message MsgUnstakeSupplier { string address = 1; } +message MsgStakeSupplier { + string address = 1; +} + message MsgUnstakeSupplierResponse {} +message MsgStakeSupplierResponse {} \ No newline at end of file diff --git a/x/supplier/client/cli/tx.go b/x/supplier/client/cli/tx.go index d1a596dfc..ed3f85141 100644 --- a/x/supplier/client/cli/tx.go +++ b/x/supplier/client/cli/tx.go @@ -30,6 +30,7 @@ func GetTxCmd() *cobra.Command { RunE: client.ValidateCmd, } + cmd.AddCommand(CmdStakeSupplier()) cmd.AddCommand(CmdUnstakeSupplier()) // this line is used by starport scaffolding # 1 diff --git a/x/supplier/client/cli/tx_stake_supplier.go b/x/supplier/client/cli/tx_stake_supplier.go new file mode 100644 index 000000000..5969ff7be --- /dev/null +++ b/x/supplier/client/cli/tx_stake_supplier.go @@ -0,0 +1,40 @@ +package cli + +import ( + "strconv" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + "github.com/spf13/cobra" + "pocket/x/supplier/types" +) + +var _ = strconv.Itoa(0) + +func CmdStakeSupplier() *cobra.Command { + cmd := &cobra.Command{ + Use: "stake-supplier", + Short: "Broadcast message stake-supplier", + Args: cobra.ExactArgs(0), + RunE: func(cmd *cobra.Command, args []string) (err error) { + + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + + msg := types.NewMsgStakeSupplier( + clientCtx.GetFromAddress().String(), + ) + if err := msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/supplier/keeper/msg_server_stake_supplier.go b/x/supplier/keeper/msg_server_stake_supplier.go new file mode 100644 index 000000000..3e0efd0d2 --- /dev/null +++ b/x/supplier/keeper/msg_server_stake_supplier.go @@ -0,0 +1,17 @@ +package keeper + +import ( + "context" + + sdk "github.com/cosmos/cosmos-sdk/types" + "pocket/x/supplier/types" +) + +func (k msgServer) StakeSupplier(goCtx context.Context, msg *types.MsgStakeSupplier) (*types.MsgStakeSupplierResponse, error) { + ctx := sdk.UnwrapSDKContext(goCtx) + + // TODO: Handling the message + _ = ctx + + return &types.MsgStakeSupplierResponse{}, nil +} diff --git a/x/supplier/module_simulation.go b/x/supplier/module_simulation.go index e6082a64d..7449d6c5f 100644 --- a/x/supplier/module_simulation.go +++ b/x/supplier/module_simulation.go @@ -3,14 +3,15 @@ package supplier import ( "math/rand" + "pocket/testutil/sample" + suppliersimulation "pocket/x/supplier/simulation" + "pocket/x/supplier/types" + "github.com/cosmos/cosmos-sdk/baseapp" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/cosmos/cosmos-sdk/types/module" simtypes "github.com/cosmos/cosmos-sdk/types/simulation" "github.com/cosmos/cosmos-sdk/x/simulation" - "pocket/testutil/sample" - suppliersimulation "pocket/x/supplier/simulation" - "pocket/x/supplier/types" ) // avoid unused import issue @@ -23,6 +24,10 @@ var ( ) const ( + opWeightMsgStakeSupplier = "op_weight_msg_stake_supplier" + // TODO: Determine the simulation weight value + defaultWeightMsgStakeSupplier int = 100 + opWeightMsgUnstakeSupplier = "op_weight_msg_unstake_supplier" // TODO: Determine the simulation weight value defaultWeightMsgUnstakeSupplier int = 100 @@ -55,6 +60,17 @@ func (AppModule) ProposalContents(_ module.SimulationState) []simtypes.WeightedP func (am AppModule) WeightedOperations(simState module.SimulationState) []simtypes.WeightedOperation { operations := make([]simtypes.WeightedOperation, 0) + var weightMsgStakeSupplier int + simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgStakeSupplier, &weightMsgStakeSupplier, nil, + func(_ *rand.Rand) { + weightMsgStakeSupplier = defaultWeightMsgStakeSupplier + }, + ) + operations = append(operations, simulation.NewWeightedOperation( + weightMsgStakeSupplier, + suppliersimulation.SimulateMsgStakeSupplier(am.accountKeeper, am.bankKeeper, am.keeper), + )) + var weightMsgUnstakeSupplier int simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgUnstakeSupplier, &weightMsgUnstakeSupplier, nil, func(_ *rand.Rand) { @@ -75,6 +91,12 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.WeightedProposalMsg { return []simtypes.WeightedProposalMsg{ simulation.NewWeightedProposalMsg( + opWeightMsgStakeSupplier, + defaultWeightMsgStakeSupplier, + func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg { + suppliersimulation.SimulateMsgStakeSupplier(am.accountKeeper, am.bankKeeper, am.keeper) + }, + opWeightMsgUnstakeSupplier, defaultWeightMsgUnstakeSupplier, func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg { diff --git a/x/supplier/simulation/stake_supplier.go b/x/supplier/simulation/stake_supplier.go new file mode 100644 index 000000000..bacf4a172 --- /dev/null +++ b/x/supplier/simulation/stake_supplier.go @@ -0,0 +1,29 @@ +package simulation + +import ( + "math/rand" + + "github.com/cosmos/cosmos-sdk/baseapp" + sdk "github.com/cosmos/cosmos-sdk/types" + simtypes "github.com/cosmos/cosmos-sdk/types/simulation" + "pocket/x/supplier/keeper" + "pocket/x/supplier/types" +) + +func SimulateMsgStakeSupplier( + ak types.AccountKeeper, + bk types.BankKeeper, + k keeper.Keeper, +) simtypes.Operation { + return func(r *rand.Rand, app *baseapp.BaseApp, ctx sdk.Context, accs []simtypes.Account, chainID string, + ) (simtypes.OperationMsg, []simtypes.FutureOperation, error) { + simAccount, _ := simtypes.RandomAcc(r, accs) + msg := &types.MsgStakeSupplier{ + Address: simAccount.Address.String(), + } + + // TODO: Handling the StakeSupplier simulation + + return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "StakeSupplier simulation not implemented"), nil, nil + } +} diff --git a/x/supplier/types/codec.go b/x/supplier/types/codec.go index 44e0550d4..a892cc75d 100644 --- a/x/supplier/types/codec.go +++ b/x/supplier/types/codec.go @@ -8,12 +8,14 @@ import ( ) func RegisterCodec(cdc *codec.LegacyAmino) { + cdc.RegisterConcrete(&MsgStakeSupplier{}, "supplier/StakeSupplier", nil) cdc.RegisterConcrete(&MsgUnstakeSupplier{}, "supplier/UnstakeSupplier", nil) // this line is used by starport scaffolding # 2 } func RegisterInterfaces(registry cdctypes.InterfaceRegistry) { registry.RegisterImplementations((*sdk.Msg)(nil), + &MsgStakeSupplier{}, &MsgUnstakeSupplier{}, ) // this line is used by starport scaffolding # 3 diff --git a/x/supplier/types/message_stake_supplier.go b/x/supplier/types/message_stake_supplier.go new file mode 100644 index 000000000..d5df10303 --- /dev/null +++ b/x/supplier/types/message_stake_supplier.go @@ -0,0 +1,45 @@ +package types + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" +) + +const TypeMsgStakeSupplier = "stake_supplier" + +var _ sdk.Msg = &MsgStakeSupplier{} + +func NewMsgStakeSupplier(address string) *MsgStakeSupplier { + return &MsgStakeSupplier{ + Address: address, + } +} + +func (msg *MsgStakeSupplier) Route() string { + return RouterKey +} + +func (msg *MsgStakeSupplier) Type() string { + return TypeMsgStakeSupplier +} + +func (msg *MsgStakeSupplier) GetSigners() []sdk.AccAddress { + address, err := sdk.AccAddressFromBech32(msg.Address) + if err != nil { + panic(err) + } + return []sdk.AccAddress{address} +} + +func (msg *MsgStakeSupplier) GetSignBytes() []byte { + bz := ModuleCdc.MustMarshalJSON(msg) + return sdk.MustSortJSON(bz) +} + +func (msg *MsgStakeSupplier) ValidateBasic() error { + _, err := sdk.AccAddressFromBech32(msg.Address) + if err != nil { + return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address address (%s)", err) + } + return nil +} diff --git a/x/supplier/types/message_stake_supplier_test.go b/x/supplier/types/message_stake_supplier_test.go new file mode 100644 index 000000000..0f59696fa --- /dev/null +++ b/x/supplier/types/message_stake_supplier_test.go @@ -0,0 +1,40 @@ +package types + +import ( + "testing" + + sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" + "github.com/stretchr/testify/require" + "pocket/testutil/sample" +) + +func TestMsgStakeSupplier_ValidateBasic(t *testing.T) { + tests := []struct { + name string + msg MsgStakeSupplier + err error + }{ + { + name: "invalid address", + msg: MsgStakeSupplier{ + Address: "invalid_address", + }, + err: sdkerrors.ErrInvalidAddress, + }, { + name: "valid address", + msg: MsgStakeSupplier{ + Address: sample.AccAddress(), + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + err := tt.msg.ValidateBasic() + if tt.err != nil { + require.ErrorIs(t, err, tt.err) + return + } + require.NoError(t, err) + }) + } +}