-
Notifications
You must be signed in to change notification settings - Fork 28
/
params.go
158 lines (138 loc) · 3.72 KB
/
params.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
153
154
155
156
157
158
package types
import (
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
// DefaultBidClosePeriod Default period for closing bids for an auction
DefaultBidClosePeriod time.Duration = time.Hour * 12 // 12 Hours
DefaultMaxAuctionDuration time.Duration = time.Hour * 24 * 90 // 90 Days
)
func NewMarketplaceParams(
saleCommission sdk.Dec,
distribution Distribution,
bidCloseDuration time.Duration,
maxAuctionDuration time.Duration,
) Params {
return Params{
SaleCommission: saleCommission,
Distribution: distribution,
BidCloseDuration: bidCloseDuration,
MaxAuctionDuration: maxAuctionDuration,
}
}
// DefaultParams returns default marketplace parameters
func DefaultParams() Params {
return NewMarketplaceParams(
sdk.NewDecWithPrec(1, 2), // 1%
Distribution{
Staking: sdk.NewDecWithPrec(50, 2), // 50%
CommunityPool: sdk.NewDecWithPrec(50, 2), // 50%
},
DefaultBidClosePeriod,
DefaultMaxAuctionDuration,
)
}
// ValidateBasic performs basic validation on marketplace parameters.
func (p Params) ValidateBasic() error {
if err := validateSaleCommission(p.SaleCommission); err != nil {
return err
}
if err := validateMarketplaceDistributionParams(p.Distribution); err != nil {
return err
}
if err := validateBidCloseDuration(p.BidCloseDuration); err != nil {
return err
}
if err := validateMaxAuctionDuration(p.MaxAuctionDuration); err != nil {
return err
}
return nil
}
func validateSaleCommission(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("sale commission must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("sale commission must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("sale commission too large: %s", v)
}
return nil
}
func validateStakingDistribution(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("staking distribution value must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("staking distribution value must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("staking distribution value too large: %s", v)
}
return nil
}
func validateCommunityPoolDistribution(i interface{}) error {
v, ok := i.(sdk.Dec)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.IsNil() {
return fmt.Errorf("community pool distribution value must be not nil")
}
if v.IsNegative() {
return fmt.Errorf("community pool distribution value must be positive: %s", v)
}
if v.GT(sdk.OneDec()) {
return fmt.Errorf("community pool distribution value too large: %s", v)
}
return nil
}
func validateMarketplaceDistributionParams(i interface{}) error {
v, ok := i.(Distribution)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
err := validateStakingDistribution(v.Staking)
if err != nil {
return err
}
err = validateCommunityPoolDistribution(v.CommunityPool)
if err != nil {
return err
}
if !v.Staking.Add(v.CommunityPool).Equal(sdk.OneDec()) {
return fmt.Errorf("marketplace distribtution commission params sum must be equal to : %d", 1)
}
return nil
}
func validateBidCloseDuration(i interface{}) error {
v, ok := i.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", i)
}
if v.Seconds() <= 0 {
return fmt.Errorf("bid close duration must be positive: %f", v.Seconds())
}
return nil
}
func validateMaxAuctionDuration(d interface{}) error {
v, ok := d.(time.Duration)
if !ok {
return fmt.Errorf("invalid parameter type: %T", d)
}
if v.Seconds() <= 0 {
return fmt.Errorf("max auction duration must be positive: %f", v.Seconds())
}
return nil
}