-
Notifications
You must be signed in to change notification settings - Fork 364
/
params.go
90 lines (77 loc) · 2.7 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
package types
import (
"bytes"
"fmt"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/params/subspace"
)
// Defaults for auction params
const (
// DefaultMaxAuctionDuration max length of auction
DefaultMaxAuctionDuration time.Duration = 2 * 24 * time.Hour
// DefaultBidDuration how long an auction gets extended when someone bids
DefaultBidDuration time.Duration = 1 * time.Hour
)
// Parameter keys
var (
// ParamStoreKeyParams Param store key for auction params
KeyAuctionBidDuration = []byte("BidDuration")
KeyAuctionDuration = []byte("MaxAuctionDuration")
)
var _ subspace.ParamSet = &Params{}
// Params is the governance parameters for the auction module.
type Params struct {
MaxAuctionDuration time.Duration `json:"max_auction_duration" yaml:"max_auction_duration"` // max length of auction
BidDuration time.Duration `json:"bid_duration" yaml:"bid_duration"` // additional time added to the auction end time after each bid, capped by the expiry.
}
// NewParams returns a new Params object.
func NewParams(maxAuctionDuration time.Duration, bidDuration time.Duration) Params {
return Params{
MaxAuctionDuration: maxAuctionDuration,
BidDuration: bidDuration,
}
}
// DefaultParams returns the default parameters for auctions.
func DefaultParams() Params {
return NewParams(
DefaultMaxAuctionDuration,
DefaultBidDuration,
)
}
// ParamKeyTable Key declaration for parameters
func ParamKeyTable() subspace.KeyTable {
return subspace.NewKeyTable().RegisterParamSet(&Params{})
}
// ParamSetPairs implements the ParamSet interface and returns all the key/value pairs.
func (p *Params) ParamSetPairs() subspace.ParamSetPairs {
return subspace.ParamSetPairs{
{Key: KeyAuctionBidDuration, Value: &p.BidDuration},
{Key: KeyAuctionDuration, Value: &p.MaxAuctionDuration},
}
}
// Equal returns a boolean determining if two Params types are identical.
func (p Params) Equal(p2 Params) bool {
bz1 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p)
bz2 := ModuleCdc.MustMarshalBinaryLengthPrefixed(&p2)
return bytes.Equal(bz1, bz2)
}
// String implements stringer interface
func (p Params) String() string {
return fmt.Sprintf(`Auction Params:
Max Auction Duration: %s
Bid Duration: %s`, p.MaxAuctionDuration, p.BidDuration)
}
// Validate checks that the parameters have valid values.
func (p Params) Validate() error {
if p.BidDuration < 0 {
return sdk.ErrInternal("bid duration cannot be negative")
}
if p.MaxAuctionDuration < 0 {
return sdk.ErrInternal("max auction duration cannot be negative")
}
if p.BidDuration > p.MaxAuctionDuration {
return sdk.ErrInternal("bid duration param cannot be larger than max auction duration")
}
return nil
}