Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Supplier] Scaffolded the UnstakeSupplier message and nothing else #50

Merged
merged 7 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75521,7 +75521,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
Expand Down
9 changes: 8 additions & 1 deletion proto/pocket/supplier/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,17 @@ 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 MsgStakeSupplierResponse {}
message MsgUnstakeSupplierResponse {}

message MsgStakeSupplierResponse {}
1 change: 1 addition & 0 deletions x/supplier/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func GetTxCmd() *cobra.Command {
}

cmd.AddCommand(CmdStakeSupplier())
cmd.AddCommand(CmdUnstakeSupplier())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
40 changes: 40 additions & 0 deletions x/supplier/client/cli/tx_unstake_supplier.go
Original file line number Diff line number Diff line change
@@ -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 CmdUnstakeSupplier() *cobra.Command {
cmd := &cobra.Command{
Use: "unstake-supplier",
Short: "Broadcast message unstake-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.NewMsgUnstakeSupplier(
clientCtx.GetFromAddress().String(),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
17 changes: 17 additions & 0 deletions x/supplier/keeper/msg_server_unstake_supplier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package keeper

import (
"context"

sdk "github.com/cosmos/cosmos-sdk/types"
"pocket/x/supplier/types"
)

func (k msgServer) UnstakeSupplier(goCtx context.Context, msg *types.MsgUnstakeSupplier) (*types.MsgUnstakeSupplierResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx

return &types.MsgUnstakeSupplierResponse{}, nil
}
30 changes: 27 additions & 3 deletions x/supplier/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -27,6 +28,10 @@ const (
// TODO: Determine the simulation weight value
defaultWeightMsgStakeSupplier int = 100

opWeightMsgUnstakeSupplier = "op_weight_msg_unstake_supplier"
// TODO: Determine the simulation weight value
defaultWeightMsgUnstakeSupplier int = 100

// this line is used by starport scaffolding # simapp/module/const
)

Expand Down Expand Up @@ -66,6 +71,17 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
suppliersimulation.SimulateMsgStakeSupplier(am.accountKeeper, am.bankKeeper, am.keeper),
))

var weightMsgUnstakeSupplier int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgUnstakeSupplier, &weightMsgUnstakeSupplier, nil,
func(_ *rand.Rand) {
weightMsgUnstakeSupplier = defaultWeightMsgUnstakeSupplier
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgUnstakeSupplier,
suppliersimulation.SimulateMsgUnstakeSupplier(am.accountKeeper, am.bankKeeper, am.keeper),
))

// this line is used by starport scaffolding # simapp/module/operation

return operations
Expand All @@ -82,6 +98,14 @@ func (am AppModule) ProposalMsgs(simState module.SimulationState) []simtypes.Wei
return nil
},
),
simulation.NewWeightedProposalMsg(
opWeightMsgUnstakeSupplier,
defaultWeightMsgUnstakeSupplier,
func(r *rand.Rand, ctx sdk.Context, accs []simtypes.Account) sdk.Msg {
suppliersimulation.SimulateMsgUnstakeSupplier(am.accountKeeper, am.bankKeeper, am.keeper)
return nil
},
),
// this line is used by starport scaffolding # simapp/module/OpMsg
}
}
29 changes: 29 additions & 0 deletions x/supplier/simulation/unstake_supplier.go
Original file line number Diff line number Diff line change
@@ -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 SimulateMsgUnstakeSupplier(
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.MsgUnstakeSupplier{
Address: simAccount.Address.String(),
}

// TODO: Handling the UnstakeSupplier simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "UnstakeSupplier simulation not implemented"), nil, nil
}
}
2 changes: 2 additions & 0 deletions x/supplier/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,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

Expand Down
45 changes: 45 additions & 0 deletions x/supplier/types/message_unstake_supplier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package types

import (
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)

const TypeMsgUnstakeSupplier = "unstake_supplier"

var _ sdk.Msg = &MsgUnstakeSupplier{}

func NewMsgUnstakeSupplier(address string) *MsgUnstakeSupplier {
return &MsgUnstakeSupplier{
Address: address,
}
}

func (msg *MsgUnstakeSupplier) Route() string {
return RouterKey
}

func (msg *MsgUnstakeSupplier) Type() string {
return TypeMsgUnstakeSupplier
}

func (msg *MsgUnstakeSupplier) GetSigners() []sdk.AccAddress {
address, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
panic(err)
}
return []sdk.AccAddress{address}
}

func (msg *MsgUnstakeSupplier) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}

func (msg *MsgUnstakeSupplier) ValidateBasic() error {
_, err := sdk.AccAddressFromBech32(msg.Address)
if err != nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid address address (%s)", err)
}
return nil
}
40 changes: 40 additions & 0 deletions x/supplier/types/message_unstake_supplier_test.go
Original file line number Diff line number Diff line change
@@ -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 TestMsgUnstakeSupplier_ValidateBasic(t *testing.T) {
tests := []struct {
name string
msg MsgUnstakeSupplier
err error
}{
{
name: "invalid address",
msg: MsgUnstakeSupplier{
Address: "invalid_address",
},
err: sdkerrors.ErrInvalidAddress,
}, {
name: "valid address",
msg: MsgUnstakeSupplier{
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)
})
}
}