This repository has been archived by the owner on Jul 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
handler.go
executable file
·75 lines (65 loc) · 1.95 KB
/
handler.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
package comment
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/coinexchain/cet-sdk/modules/comment/internal/types"
dex "github.com/coinexchain/cet-sdk/types"
)
func NewHandler(k Keeper) sdk.Handler {
return func(ctx sdk.Context, msg sdk.Msg) sdk.Result {
ctx = ctx.WithEventManager(sdk.NewEventManager())
switch msg := msg.(type) {
case types.MsgCommentToken:
return handleMsgCommentToken(ctx, k, msg)
default:
return dex.ErrUnknownRequest(ModuleName, msg)
}
}
}
func handleMsgCommentToken(ctx sdk.Context, k Keeper, msg types.MsgCommentToken) sdk.Result {
if !k.IsTokenExists(ctx, msg.Token) {
return types.ErrNoSuchAsset().Result()
}
if msg.Donation > 0 {
donatedCoin := sdk.Coins{sdk.NewCoin(dex.CET, sdk.NewInt(msg.Donation))}
res := k.DonateToCommunityPool(ctx, msg.Sender, donatedCoin)
if res != nil {
return res.Result()
}
}
for _, ref := range msg.References {
if ref.RewardAmount <= 0 {
continue
}
acc := k.GetAccount(ctx, ref.RewardTarget)
if acc == nil {
return types.ErrNoSuchAccount(ref.RewardTarget.String()).Result()
}
rewardCoin := sdk.NewCoin(ref.RewardToken, sdk.NewInt(ref.RewardAmount))
res := k.SendCoins(ctx, msg.Sender, ref.RewardTarget, sdk.Coins{rewardCoin})
if res != nil {
return res.Result()
}
}
lastCount := k.IncrCommentCount(ctx, msg.Token)
if len(k.GetEventTypeMsgQueue()) != 0 {
tokenComment := types.NewTokenComment(&msg, lastCount, ctx.BlockHeight())
bytes := dex.SafeJSONMarshal(tokenComment)
ctx.EventManager().EmitEvent(
sdk.NewEvent(
k.GetEventTypeMsgQueue(),
sdk.NewAttribute(types.TokenCommentKey, string(bytes)),
),
)
}
ctx.EventManager().EmitEvent(
sdk.NewEvent(
sdk.EventTypeMessage,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(sdk.AttributeKeySender, msg.Sender.String()),
),
)
return sdk.Result{
Codespace: types.CodeSpaceComment,
Events: ctx.EventManager().Events(),
}
}