Skip to content

Commit

Permalink
feat(#242): SetContributorAdd
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Dec 17, 2024
1 parent b44f18a commit fa20805
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
21 changes: 19 additions & 2 deletions x/cardchain/keeper/msg_server_set_contributor_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ 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) SetContributorAdd(goCtx context.Context, msg *types.MsgSetContributorAdd) (*types.MsgSetContributorAddResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)

// TODO: Handling the message
_ = ctx
set := k.Sets.Get(ctx, msg.SetId)
if err := checkSetEditable(set, msg.Creator); err != nil {
return nil, err
}

if slices.Contains(set.Contributors, msg.User) {
return nil, errorsmod.Wrap(types.ErrInvalidData, "Contributor allready Contributor: "+msg.User)
}

if err := k.CollectSetConributionFee(ctx, msg.Creator); err != nil {
return nil, errorsmod.Wrap(errors.ErrInsufficientFunds, err.Error())
}

set.Contributors = append(set.Contributors, msg.User)

k.Sets.Set(ctx, msg.SetId, set)

return &types.MsgSetContributorAddResponse{}, nil
}
15 changes: 7 additions & 8 deletions x/cardchain/keeper/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package keeper
import (
"github.com/cosmos/cosmos-sdk/types/errors"

sdkerrors "cosmossdk.io/errors"
errorsmod "cosmossdk.io/errors"
"github.com/DecentralCardGame/cardchain/x/cardchain/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand Down Expand Up @@ -88,9 +88,7 @@ func (k Keeper) GetRarityDistribution(ctx sdk.Context, set types.Set, setSize ui

for _, cardId := range set.Cards {
card := k.Cards.Get(ctx, cardId)
if err != nil {
return dist, sdkerrors.Wrap(types.ErrCardobject, err.Error())
}

switch card.Rarity {
case types.CardRarity_common, types.CardRarity_unique, types.CardRarity_exceptional:
commons++
Expand All @@ -99,7 +97,7 @@ func (k Keeper) GetRarityDistribution(ctx sdk.Context, set types.Set, setSize ui
case types.CardRarity_rare:
rares++
default:
return dist, sdkerrors.Wrapf(errors.ErrInvalidType, "Invalid rarity (%d) for card (%d)", card.Rarity, cardId)
return dist, errorsmod.Wrapf(errors.ErrInvalidType, "Invalid rarity (%d) for card (%d)", card.Rarity, cardId)
}
}

Expand All @@ -111,15 +109,16 @@ func (k Keeper) GetRarityDistribution(ctx sdk.Context, set types.Set, setSize ui

func checkSetEditable(set *types.Set, user string) error {
if len(set.Contributors) == 0 {
return sdkerrors.Wrap(types.ErrUninitializedType, "Set not initialized")
return errorsmod.Wrap(types.ErrInvalidData, "Set not initialized")
}

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

if set.Status != types.CStatus_design {
return types.ErrSetNotInDesign
return errorsmod.Wrapf(errors.ErrUnauthorized, "Invalid set status is: %s", set.Status.String())

}
return nil
}

0 comments on commit fa20805

Please sign in to comment.