Skip to content

Commit

Permalink
feat(#242): SetCardAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Dec 17, 2024
1 parent dd418fb commit b44f18a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
48 changes: 46 additions & 2 deletions x/cardchain/keeper/msg_server_set_card_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions x/cardchain/types/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions x/cardchain/types/message_set_artwork_add.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
}

0 comments on commit b44f18a

Please sign in to comment.