Skip to content

Commit

Permalink
Merge pull request #137 from DecentralCardGame/134-add-route-for-chan…
Browse files Browse the repository at this point in the history
…ging-collection-name

Added MsgSetCollectionName and improved error handling
  • Loading branch information
lxgr-linux authored Sep 29, 2023
2 parents 9be7893 + a552d74 commit b7c3a67
Show file tree
Hide file tree
Showing 19 changed files with 830 additions and 195 deletions.
2 changes: 2 additions & 0 deletions docs/static/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68774,6 +68774,8 @@ definitions:
type: object
DecentralCardGame.cardchain.cardchain.MsgSetCollectionArtistResponse:
type: object
DecentralCardGame.cardchain.cardchain.MsgSetCollectionNameResponse:
type: object
DecentralCardGame.cardchain.cardchain.MsgSetCollectionStoryWriterResponse:
type: object
DecentralCardGame.cardchain.cardchain.MsgSetProfileCardResponse:
Expand Down
3 changes: 0 additions & 3 deletions package-lock.json

This file was deleted.

29 changes: 19 additions & 10 deletions proto/cardchain/cardchain/tx.proto
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ service Msg {
rpc SetCollectionArtist (MsgSetCollectionArtist ) returns (MsgSetCollectionArtistResponse );
rpc SetUserWebsite (MsgSetUserWebsite ) returns (MsgSetUserWebsiteResponse );
rpc SetUserBiography (MsgSetUserBiography ) returns (MsgSetUserBiographyResponse );

// this line is used by starport scaffolding # proto/tx/rpc
rpc MultiVoteCard (MsgMultiVoteCard) returns (MsgMultiVoteCardResponse);
rpc MsgOpenMatch (MsgMsgOpenMatch ) returns (MsgMsgOpenMatchResponse );
rpc MultiVoteCard (MsgMultiVoteCard ) returns (MsgMultiVoteCardResponse );
rpc MsgOpenMatch (MsgMsgOpenMatch ) returns (MsgMsgOpenMatchResponse );
rpc SetCollectionName (MsgSetCollectionName) returns (MsgSetCollectionNameResponse);
}
message MsgCreateuser {
string creator = 1;
Expand All @@ -74,7 +75,7 @@ message MsgBuyCardScheme {
cosmos.base.v1beta1.Coin bid = 2 [(gogoproto.nullable) = false, (gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coin"];

/*
string bid = 2;
string bid = 2;
*/}

message MsgBuyCardSchemeResponse {
Expand Down Expand Up @@ -335,9 +336,9 @@ message MsgRewokeCouncilRegistration {
message MsgRewokeCouncilRegistrationResponse {}

message MsgConfirmMatch {
string creator = 1;
uint64 matchId = 2;
Outcome outcome = 3;
string creator = 1;
uint64 matchId = 2;
Outcome outcome = 3;
repeated SingleVote votedCards = 4;
}

Expand Down Expand Up @@ -406,9 +407,9 @@ message MsgMultiVoteCard {
message MsgMultiVoteCardResponse {}

message MsgMsgOpenMatch {
string creator = 1;
string playerA = 2;
string playerB = 3;
string creator = 1;
string playerA = 2;
string playerB = 3;
repeated uint64 playerADeck = 4;
repeated uint64 playerBDeck = 5;
}
Expand All @@ -417,3 +418,11 @@ message MsgMsgOpenMatchResponse {
uint64 matchId = 1;
}

message MsgSetCollectionName {
string creator = 1;
uint64 collectionId = 2;
string name = 3;
}

message MsgSetCollectionNameResponse {}

1 change: 1 addition & 0 deletions x/cardchain/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func GetTxCmd() *cobra.Command {
cmd.AddCommand(CmdSetUserBiography())
cmd.AddCommand(CmdMultiVoteCard())
cmd.AddCommand(CmdMsgOpenMatch())
cmd.AddCommand(CmdSetCollectionName())
// this line is used by starport scaffolding # 1

return cmd
Expand Down
48 changes: 48 additions & 0 deletions x/cardchain/client/cli/tx_set_collection_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cli

import (
"strconv"

"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)

var _ = strconv.Itoa(0)

func CmdSetCollectionName() *cobra.Command {
cmd := &cobra.Command{
Use: "set-collection-name [collection-id] [name]",
Short: "Broadcast message SetCollectionName",
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) (err error) {
argCollectionId, err := cast.ToUint64E(args[0])
if err != nil {
return err
}
argName := args[1]

clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgSetCollectionName(
clientCtx.GetFromAddress().String(),
argCollectionId,
argName,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)

return cmd
}
20 changes: 18 additions & 2 deletions x/cardchain/keeper/collection.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package keeper

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

sdkerrors "cosmossdk.io/errors"
"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
Expand Down Expand Up @@ -104,5 +105,20 @@ func GetCardRarity(card *keywords.Card) (*cardobject.Rarity, error) {
} else if card.Headquarter != nil {
return card.Headquarter.Rarity, nil
}
return nil, errors.New("no card-attributes")
return nil, fmt.Errorf("no card-attributes")
}

func checkCollectionEditable(collection *types.Collection, user string) error {
if len(collection.Contributors) == 0 {
return sdkerrors.Wrap(types.ErrUninitializedType, "Collection not initialized")
}

if user != collection.Contributors[0] {
return sdkerrors.Wrap(errors.ErrUnauthorized, "Invalid creator")
}

if collection.Status != types.CStatus_design {
return types.ErrCollectionNotInDesign
}
return nil
}
10 changes: 4 additions & 6 deletions x/cardchain/keeper/msg_server_add_contributor_to_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,16 @@ func (k msgServer) AddContributorToCollection(goCtx context.Context, msg *types.
ctx := sdk.UnwrapSDKContext(goCtx)

collection := k.Collections.Get(ctx, msg.CollectionId)
if msg.Creator != collection.Contributors[0] {
return nil, sdkerrors.Wrap(errors.ErrUnauthorized, "Invalid creator")
}
if collection.Status != types.CStatus_design {
return nil, types.ErrCollectionNotInDesign
err := checkCollectionEditable(collection, msg.Creator)
if err != nil {
return nil, err
}

if slices.Contains(collection.Contributors, msg.User) {
return nil, sdkerrors.Wrap(types.ErrContributor, "Contributor allready Contributor: "+msg.User)
}

err := k.CollectCollectionConributionFee(ctx, msg.Creator)
err = k.CollectCollectionConributionFee(ctx, msg.Creator)
if err != nil {
return nil, sdkerrors.Wrap(errors.ErrInsufficientFunds, err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion x/cardchain/keeper/msg_server_set_card_rarity.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (k msgServer) SetCardRarity(goCtx context.Context, msg *types.MsgSetCardRar
if err != nil {
return nil, sdkerrors.Wrap(types.ErrCardobject, err.Error())
}

if cardobj.Action != nil {
cardobj.Action.Rarity = &rarity
} else if cardobj.Place != nil {
Expand Down
11 changes: 3 additions & 8 deletions x/cardchain/keeper/msg_server_set_collection_artist.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@ package keeper
import (
"context"

sdkerrors "cosmossdk.io/errors"
"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)

func (k msgServer) SetCollectionArtist(goCtx context.Context, msg *types.MsgSetCollectionArtist) (*types.MsgSetCollectionArtistResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

collection := k.Collections.Get(ctx, msg.CollectionId)
if msg.Creator != collection.Contributors[0] {
return nil, sdkerrors.Wrap(errors.ErrUnauthorized, "Invalid creator")
}

if collection.Status != types.CStatus_design {
return nil, types.ErrCollectionNotInDesign
err := checkCollectionEditable(collection, msg.Creator)
if err != nil {
return nil, err
}

collection.Artist = msg.Artist
Expand Down
24 changes: 24 additions & 0 deletions x/cardchain/keeper/msg_server_set_collection_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package keeper

import (
"context"

"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (k msgServer) SetCollectionName(goCtx context.Context, msg *types.MsgSetCollectionName) (*types.MsgSetCollectionNameResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

collection := k.Collections.Get(ctx, msg.CollectionId)
err := checkCollectionEditable(collection, msg.Creator)
if err != nil {
return nil, err
}

collection.Name = msg.Name

k.Collections.Set(ctx, msg.CollectionId, collection)

return &types.MsgSetCollectionNameResponse{}, nil
}
11 changes: 3 additions & 8 deletions x/cardchain/keeper/msg_server_set_collection_story_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,17 @@ package keeper
import (
"context"

sdkerrors "cosmossdk.io/errors"
"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/errors"
)

func (k msgServer) SetCollectionStoryWriter(goCtx context.Context, msg *types.MsgSetCollectionStoryWriter) (*types.MsgSetCollectionStoryWriterResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

collection := k.Collections.Get(ctx, msg.CollectionId)
if msg.Creator != collection.Contributors[0] {
return nil, sdkerrors.Wrap(errors.ErrUnauthorized, "Invalid creator")
}

if collection.Status != types.CStatus_design {
return nil, types.ErrCollectionNotInDesign
err := checkCollectionEditable(collection, msg.Creator)
if err != nil {
return nil, err
}

collection.StoryWriter = msg.StoryWriter
Expand Down
2 changes: 1 addition & 1 deletion x/cardchain/keeper/msg_server_set_user_website.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package keeper
import (
"context"

sdkerrors "cosmossdk.io/errors"
"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "cosmossdk.io/errors"
)

func (k msgServer) SetUserWebsite(goCtx context.Context, msg *types.MsgSetUserWebsite) (*types.MsgSetUserWebsiteResponse, error) {
Expand Down
15 changes: 15 additions & 0 deletions x/cardchain/module_simulation.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ const (
// TODO: Determine the simulation weight value
defaultWeightMsgMsgOpenMatch int = 100

opWeightMsgSetCollectionName = "op_weight_msg_set_collection_name"
// TODO: Determine the simulation weight value
defaultWeightMsgSetCollectionName int = 100

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

Expand Down Expand Up @@ -687,6 +691,17 @@ func (am AppModule) WeightedOperations(simState module.SimulationState) []simtyp
cardchainsimulation.SimulateMsgMsgOpenMatch(am.accountKeeper, am.bankKeeper, am.keeper),
))

var weightMsgSetCollectionName int
simState.AppParams.GetOrGenerate(simState.Cdc, opWeightMsgSetCollectionName, &weightMsgSetCollectionName, nil,
func(_ *rand.Rand) {
weightMsgSetCollectionName = defaultWeightMsgSetCollectionName
},
)
operations = append(operations, simulation.NewWeightedOperation(
weightMsgSetCollectionName,
cardchainsimulation.SimulateMsgSetCollectionName(am.accountKeeper, am.bankKeeper, am.keeper),
))

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

return operations
Expand Down
29 changes: 29 additions & 0 deletions x/cardchain/simulation/set_collection_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package simulation

import (
"math/rand"

"github.com/DecentralCardGame/Cardchain/x/cardchain/keeper"
"github.com/DecentralCardGame/Cardchain/x/cardchain/types"
"github.com/cosmos/cosmos-sdk/baseapp"
sdk "github.com/cosmos/cosmos-sdk/types"
simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
)

func SimulateMsgSetCollectionName(
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.MsgSetCollectionName{
Creator: simAccount.Address.String(),
}

// TODO: Handling the SetCollectionName simulation

return simtypes.NoOpMsg(types.ModuleName, msg.Type(), "SetCollectionName simulation not implemented"), nil, nil
}
}
4 changes: 4 additions & 0 deletions x/cardchain/types/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func RegisterCodec(cdc *codec.LegacyAmino) {
cdc.RegisterConcrete(&MsgSetUserBiography{}, "cardchain/SetUserBiography", nil)
cdc.RegisterConcrete(&MsgMultiVoteCard{}, "cardchain/MultiVoteCard", nil)
cdc.RegisterConcrete(&MsgMsgOpenMatch{}, "cardchain/MsgOpenMatch", nil)
cdc.RegisterConcrete(&MsgSetCollectionName{}, "cardchain/SetCollectionName", nil)
// this line is used by starport scaffolding # 2
}

Expand Down Expand Up @@ -193,6 +194,9 @@ func RegisterInterfaces(registry cdctypes.InterfaceRegistry) {
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgMsgOpenMatch{},
)
registry.RegisterImplementations((*sdk.Msg)(nil),
&MsgSetCollectionName{},
)
// this line is used by starport scaffolding # 3

msgservice.RegisterMsgServiceDesc(registry, &_Msg_serviceDesc)
Expand Down
1 change: 1 addition & 0 deletions x/cardchain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ var (
ErrStringLength = sdkerrors.Register(ModuleName, 21, "String literal too long")
ErrUserAlreadyExists = sdkerrors.Register(ModuleName, 22, "User already exists")
ErrWaitingForPlayers = sdkerrors.Register(ModuleName, 23, "Waiting for players")
ErrUninitializedType = sdkerrors.Register(ModuleName, 24, "Type not yet initialized")
)
Loading

0 comments on commit b7c3a67

Please sign in to comment.