-
Notifications
You must be signed in to change notification settings - Fork 6
/
message_add_mission_to_campaign.go
102 lines (87 loc) · 2.84 KB
/
message_add_mission_to_campaign.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
package types
import (
"cosmossdk.io/errors"
c4eerrors "github.com/chain4energy/c4e-chain/types/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"time"
)
const TypeMsgAddMission = "add_mission"
var _ sdk.Msg = &MsgAddMission{}
func NewMsgAddMission(owner string, campaignId uint64, name string, description string, missionType MissionType, weight *sdk.Dec, claimStartDate *time.Time) *MsgAddMission {
return &MsgAddMission{
Owner: owner,
Name: name,
Description: description,
CampaignId: campaignId,
MissionType: missionType,
Weight: weight,
ClaimStartDate: claimStartDate,
}
}
func NewInitialMission(campaignId uint64) *Mission {
return &Mission{
CampaignId: campaignId,
Name: "Initial mission",
Description: "Initial mission - basic mission that must be claimed first",
MissionType: MissionInitialClaim,
Weight: sdk.ZeroDec(),
}
}
func (msg *MsgAddMission) Route() string {
return RouterKey
}
func (msg *MsgAddMission) Type() string {
return TypeMsgAddMission
}
func (msg *MsgAddMission) GetSigners() []sdk.AccAddress {
creator, err := sdk.AccAddressFromBech32(msg.Owner)
if err != nil {
panic(err)
}
return []sdk.AccAddress{creator}
}
func (msg *MsgAddMission) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(msg)
return sdk.MustSortJSON(bz)
}
func (msg *MsgAddMission) ValidateBasic() error {
if msg.Weight == nil {
return errors.Wrapf(c4eerrors.ErrParam, "weight cannot be nil")
}
return ValidateAddMission(msg.Owner, msg.Name, msg.MissionType, *msg.Weight)
}
func ValidateAddMission(owner string, name string, missionType MissionType, weight sdk.Dec) error {
_, err := sdk.AccAddressFromBech32(owner)
if err != nil {
return errors.Wrapf(sdkerrors.ErrInvalidAddress, "invalid owner address (%s)", err)
}
if name == "" {
return errors.Wrap(c4eerrors.ErrParam, "empty name error")
}
if err = ValidateMissionWeight(weight, missionType); err != nil {
return err
}
return ValidateMissionType(missionType)
}
func ValidateMissionWeight(weight sdk.Dec, missionType MissionType) error {
if weight.IsNil() {
return errors.Wrapf(c4eerrors.ErrParam, "add mission to claim campaign weight is nil error")
}
if weight.GT(sdk.NewDec(1)) || weight.IsNegative() {
return errors.Wrapf(c4eerrors.ErrParam, "weight (%s) is not between 0 and 1 error", weight.String())
}
if missionType != MissionInitialClaim {
if weight.Equal(sdk.ZeroDec()) {
return errors.Wrapf(c4eerrors.ErrParam, "weight (%s) cannot be 0 for non-initial mission", weight.String())
}
}
return nil
}
func ValidateMissionType(missionType MissionType) error {
switch missionType {
case MissionClaim, MissionDelegate, MissionVote, MissionInitialClaim, MissionToDefine:
return nil
}
return errors.Wrap(sdkerrors.ErrInvalidType, "wrong mission type")
}