-
Notifications
You must be signed in to change notification settings - Fork 28
/
validation.go
152 lines (139 loc) · 4.38 KB
/
validation.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package types
import (
"time"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// ValidateCampaign checks campaign is valid or not
func ValidateCampaign(campaign Campaign) error {
if _, err := sdk.AccAddressFromBech32(campaign.Creator); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid creator address (%s)", err)
}
if err := ValidateClaimType(campaign.ClaimType); err != nil {
return err
}
if err := ValidateInteractionType(campaign.Interaction); err != nil {
return err
}
if campaign.ClaimType != CLAIM_TYPE_NFT {
if err := ValidateTokens(campaign.TotalTokens, campaign.TokensPerClaim); err != nil {
return err
}
if err := ValidateTokens(campaign.TotalTokens, campaign.AvailableTokens); err != nil {
return err
}
}
if campaign.ClaimType == CLAIM_TYPE_NFT || campaign.ClaimType == CLAIM_TYPE_FT_AND_NFT {
if err := validateNFTMintDetails(campaign.NftMintDetails); err != nil {
return err
}
}
if campaign.ClaimType == CLAIM_TYPE_FT || campaign.ClaimType == CLAIM_TYPE_FT_AND_NFT {
if err := ValidateDistribution(campaign.Distribution); err != nil {
return err
}
}
if campaign.MaxAllowedClaims == 0 {
return errorsmod.Wrapf(ErrInValidMaxAllowedClaims,
"max allowed claims must be a positive number (%d)", campaign.MaxAllowedClaims)
}
return nil
}
func ValidateDuration(t interface{}) error {
duration, ok := t.(time.Duration)
if !ok {
return errorsmod.Wrapf(ErrInvalidDuration, "invalid value for duration: %T", t)
}
if duration.Nanoseconds() <= 0 {
return errorsmod.Wrapf(ErrInvalidDuration,
"invalid duration %s, only accepts positive value", duration.String())
}
return nil
}
func ValidateTimestamp(t interface{}) error {
_, ok := t.(time.Time)
if !ok {
return errorsmod.Wrapf(ErrInvalidTimestamp, "invalid timestamp: %T", t)
}
return nil
}
func ValidCampaignStatus(status CampaignStatus) bool {
if status == CAMPAIGN_STATUS_INACTIVE ||
status == CAMPAIGN_STATUS_ACTIVE || status == CAMPAIGN_STATUS_UNSPECIFIED {
return true
}
return false
}
func ValidateClaimType(claimType ClaimType) error {
if claimType == CLAIM_TYPE_FT || claimType == CLAIM_TYPE_NFT || claimType == CLAIM_TYPE_FT_AND_NFT {
return nil
}
return errorsmod.Wrapf(ErrInvalidClaimType, "unknown claim type (%s)", claimType)
}
func ValidateClaim(claim Claim) error {
if _, err := sdk.AccAddressFromBech32(claim.Address); err != nil {
return errorsmod.Wrapf(sdkerrors.ErrInvalidAddress, "invalid claimer address (%s)", err)
}
return nil
}
func ValidateTokens(tokensA, tokensB sdk.Coin) error {
if tokensA.IsNil() || tokensB.IsNil() {
return errorsmod.Wrapf(
ErrInvalidTokens,
"invalid tokens, only accepts positive amount",
)
}
if !tokensA.IsValid() {
return errorsmod.Wrapf(
ErrInvalidTokens,
"invalid tokens %s, only accepts positive amount",
tokensA.String(),
)
}
if !tokensB.IsValid() {
return errorsmod.Wrapf(
ErrInvalidTokens,
"invalid tokens %s, only accepts positive amount",
tokensB.String(),
)
}
if tokensA.Denom != tokensB.Denom {
return errorsmod.Wrapf(
ErrInvalidTokens,
"mismatched token denoms (%s, %s)",
tokensA.Denom,
tokensB.Denom,
)
}
return nil
}
func validateNFTMintDetails(details *NFTDetails) error {
if details == nil || len(details.Name) == 0 || len(details.DenomId) == 0 || len(details.MediaUri) == 0 {
return errorsmod.Wrapf(
ErrInvalidNFTMintDetails,
"invalid nft mint details, details should not be nil and name, media_uri can not be empty.")
}
return nil
}
func ValidateDistribution(distribution *Distribution) error {
if distribution == nil {
return errorsmod.Wrapf(ErrInvalidDistribution, "distribution can not be nil")
}
if !(distribution.Type == DISTRIBUTION_TYPE_STREAM || distribution.Type == DISTRIBUTION_TYPE_INSTANT) {
return errorsmod.Wrapf(ErrInvalidClaimType, "invalid distribution type (%s)", distribution.Type)
}
if distribution.Type == DISTRIBUTION_TYPE_STREAM {
if err := ValidateDuration(distribution.StreamDuration); err != nil {
return err
}
}
return nil
}
func ValidateInteractionType(interaction InteractionType) error {
if !(interaction == INTERACTION_TYPE_BURN ||
interaction == INTERACTION_TYPE_TRANSFER || interaction == INTERACTION_TYPE_HOLD) {
return errorsmod.Wrapf(ErrInteractionMismatch, "unknown interaction type (%s)", interaction)
}
return nil
}