diff --git a/x/cardchain/keeper/msg_server_set_card_add.go b/x/cardchain/keeper/msg_server_set_card_add.go index 738c9cae..3411f9a4 100644 --- a/x/cardchain/keeper/msg_server_set_card_add.go +++ b/x/cardchain/keeper/msg_server_set_card_add.go @@ -2,16 +2,60 @@ package keeper import ( "context" + "slices" + errorsmod "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) SetCardAdd(goCtx context.Context, msg *types.MsgSetCardAdd) (*types.MsgSetCardAddResponse, error) { ctx := sdk.UnwrapSDKContext(goCtx) - // TODO: Handling the message - _ = ctx + setSize := int(k.GetParams(ctx).SetSize) + + set := k.Sets.Get(ctx, msg.SetId) + if !slices.Contains(set.Contributors, msg.Creator) { + return nil, errorsmod.Wrap(errors.ErrUnauthorized, "Invalid contributor") + } + if set.Status != types.CStatus_design { + return nil, errorsmod.Wrapf(errors.ErrUnauthorized, "Invalid set status is: %s", set.Status.String()) + } + + iter := k.Sets.GetItemIterator(ctx) + for ; iter.Valid(); iter.Next() { + idx, coll := iter.Value() + if coll.Status != types.CStatus_archived && slices.Contains(coll.Cards, msg.CardId) { + return nil, errorsmod.Wrapf(types.ErrCardAlreadyInSet, "Set: %d", idx) + } + } + + card := k.Cards.Get(ctx, msg.CardId) + if card.Status != types.Status_permanent { + return nil, errorsmod.Wrap(types.ErrCardDoesNotExist, "Card is not permanent or does not exist") + } + + if card.Owner != msg.Creator { + return nil, errorsmod.Wrap(errors.ErrUnauthorized, "Invalid creator") + } + + if len(set.Cards) >= setSize { + return nil, errorsmod.Wrapf(types.ErrInvalidData, "Max set size is %d", setSize) + } + + if slices.Contains(set.Cards, msg.CardId) { + return nil, errorsmod.Wrapf(types.ErrCardAlreadyInSet, "Card: %d", msg.CardId) + } + + err := k.CollectSetConributionFee(ctx, msg.Creator) + if err != nil { + return nil, errorsmod.Wrap(errors.ErrInsufficientFunds, err.Error()) + } + + set.Cards = append(set.Cards, msg.CardId) + + k.Sets.Set(ctx, msg.SetId, set) return &types.MsgSetCardAddResponse{}, nil } diff --git a/x/cardchain/types/errors.go b/x/cardchain/types/errors.go index 01c25658..8acf92a6 100644 --- a/x/cardchain/types/errors.go +++ b/x/cardchain/types/errors.go @@ -9,6 +9,8 @@ import ( // x/cardchain module sentinel errors var ( ErrInvalidSigner = sdkerrors.Register(ModuleName, 1100, "expected gov account as only signer for proposal message") + ErrCardDoesNotExist = sdkerrors.Register(ModuleName, 1, "card does not exist") + ErrCardAlreadyInSet = sdkerrors.Register(ModuleName, 8, "Card already in set") ErrInvalidCardStatus = sdkerrors.Register(ModuleName, 13, "Invalid card-status") ErrImageSizeExceeded = sdkerrors.Register(ModuleName, 17, "Image too big! Max size is 500kb") ErrInvalidData = sdkerrors.Register(ModuleName, 27, "Invalid data in transaction") diff --git a/x/cardchain/types/message_set_artwork_add.go b/x/cardchain/types/message_set_artwork_add.go index 8f2bab55..917bfed0 100644 --- a/x/cardchain/types/message_set_artwork_add.go +++ b/x/cardchain/types/message_set_artwork_add.go @@ -1,6 +1,8 @@ package types import ( + fmt "fmt" + errorsmod "cosmossdk.io/errors" sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" @@ -21,5 +23,10 @@ func (msg *MsgSetArtworkAdd) ValidateBasic() error { if err != nil { return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err) } + + if len(msg.Image) > ArtworkMaxSize { + return errorsmod.Wrap(ErrImageSizeExceeded, fmt.Sprint(len(msg.Image))) + } + return nil }