/
msg_server_royalty_config.go
135 lines (114 loc) · 3.2 KB
/
msg_server_royalty_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/likecoin/likecoin-chain/v4/x/likenft/types"
)
func (k msgServer) validateReqToMutateRoyaltyConfig(ctx sdk.Context, msgCreator string, classId string, configInput *types.RoyaltyConfigInput) error {
// Check user is class owner
userAddress, err := sdk.AccAddressFromBech32(msgCreator)
if err != nil {
return sdkerrors.ErrInvalidAddress
}
class, classData, err := k.GetClass(ctx, classId)
if err != nil {
return err
}
parent, err := k.ValidateAndRefreshClassParent(ctx, class.Id, classData.Parent)
if err != nil {
return err
}
if !parent.Owner.Equals(userAddress) {
return sdkerrors.ErrUnauthorized.Wrapf("user is not the class owner")
}
// Validate royalty config
if configInput != nil {
if err := k.validateRoyaltyConfigInput(ctx, *configInput); err != nil {
return err
}
}
return nil
}
func (k msgServer) CreateRoyaltyConfig(goCtx context.Context, msg *types.MsgCreateRoyaltyConfig) (*types.MsgCreateRoyaltyConfigResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.validateReqToMutateRoyaltyConfig(ctx, msg.Creator, msg.ClassId, &msg.RoyaltyConfig)
if err != nil {
return nil, err
}
// Check if the value already exists
_, isFound := k.GetRoyaltyConfig(
ctx,
msg.ClassId,
)
if isFound {
return nil, types.ErrRoyaltyConfigAlreadyExists
}
// Set config
config := msg.RoyaltyConfig.ToConfig()
k.SetRoyaltyConfig(
ctx,
types.RoyaltyConfigByClass{
ClassId: msg.ClassId,
RoyaltyConfig: config,
},
)
ctx.EventManager().EmitTypedEvent(&types.EventCreateRoyaltyConfig{
ClassId: msg.ClassId,
})
return &types.MsgCreateRoyaltyConfigResponse{
RoyaltyConfig: config,
}, nil
}
func (k msgServer) UpdateRoyaltyConfig(goCtx context.Context, msg *types.MsgUpdateRoyaltyConfig) (*types.MsgUpdateRoyaltyConfigResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.validateReqToMutateRoyaltyConfig(ctx, msg.Creator, msg.ClassId, &msg.RoyaltyConfig)
if err != nil {
return nil, err
}
// Check if the value exists
_, isFound := k.GetRoyaltyConfig(
ctx,
msg.ClassId,
)
if !isFound {
return nil, types.ErrRoyaltyConfigNotFound
}
config := msg.RoyaltyConfig.ToConfig()
k.SetRoyaltyConfig(
ctx,
types.RoyaltyConfigByClass{
ClassId: msg.ClassId,
RoyaltyConfig: config,
},
)
ctx.EventManager().EmitTypedEvent(&types.EventUpdateRoyaltyConfig{
ClassId: msg.ClassId,
})
return &types.MsgUpdateRoyaltyConfigResponse{
RoyaltyConfig: config,
}, nil
}
func (k msgServer) DeleteRoyaltyConfig(goCtx context.Context, msg *types.MsgDeleteRoyaltyConfig) (*types.MsgDeleteRoyaltyConfigResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
err := k.validateReqToMutateRoyaltyConfig(ctx, msg.Creator, msg.ClassId, nil)
if err != nil {
return nil, err
}
// Check if the value exists
_, isFound := k.GetRoyaltyConfig(
ctx,
msg.ClassId,
)
if !isFound {
return nil, types.ErrRoyaltyConfigNotFound
}
k.RemoveRoyaltyConfig(
ctx,
msg.ClassId,
)
ctx.EventManager().EmitTypedEvent(&types.EventDeleteRoyaltyConfig{
ClassId: msg.ClassId,
})
return &types.MsgDeleteRoyaltyConfigResponse{}, nil
}