/
proposal.go
226 lines (207 loc) 路 7.04 KB
/
proposal.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package types
import (
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
gov "github.com/cosmos/cosmos-sdk/x/gov/types"
)
const (
ProposalTypePoolParameterChange string = "PoolParameterChange"
ProposalTypePublicFarmingPlan string = "PublicFarmingPlan"
)
var (
_ gov.Content = &PoolParameterChangeProposal{}
_ gov.Content = &PublicFarmingPlanProposal{}
)
func init() {
gov.RegisterProposalType(ProposalTypePoolParameterChange)
gov.RegisterProposalTypeCodec(&PoolParameterChangeProposal{}, "crescent/PoolParameterChangeProposal")
gov.RegisterProposalType(ProposalTypePublicFarmingPlan)
gov.RegisterProposalTypeCodec(&PublicFarmingPlanProposal{}, "crescent/PublicFarmingPlanProposal")
}
func NewPoolParameterChangeProposal(title, description string, changes []PoolParameterChange) *PoolParameterChangeProposal {
return &PoolParameterChangeProposal{
Title: title,
Description: description,
Changes: changes,
}
}
func (p *PoolParameterChangeProposal) GetTitle() string { return p.Title }
func (p *PoolParameterChangeProposal) GetDescription() string { return p.Description }
func (p *PoolParameterChangeProposal) ProposalRoute() string { return RouterKey }
func (p *PoolParameterChangeProposal) ProposalType() string {
return ProposalTypePoolParameterChange
}
func (p *PoolParameterChangeProposal) ValidateBasic() error {
if err := gov.ValidateAbstract(p); err != nil {
return err
}
if len(p.Changes) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "changes must not be empty")
}
for _, change := range p.Changes {
if err := change.Validate(); err != nil {
return err
}
}
return nil
}
func (p PoolParameterChangeProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Pool Parameter Change Proposal:
Title: %s
Description: %s
Changes:
`, p.Title, p.Description))
for _, change := range p.Changes {
b.WriteString(fmt.Sprintf(` Pool Parameter Change:
Pool Id: %d
Tick Spacing: %d
Min Order Quantity: %s
Min Order Quote: %s
`, change.PoolId, change.TickSpacing, change.MinOrderQuantity, change.MinOrderQuote))
}
return b.String()
}
func NewPoolParameterChange(
poolId uint64, tickSpacing uint32, minOrderQty, minOrderQuote *sdk.Dec) PoolParameterChange {
return PoolParameterChange{
PoolId: poolId,
TickSpacing: tickSpacing,
MinOrderQuantity: minOrderQty,
MinOrderQuote: minOrderQuote,
}
}
func (change PoolParameterChange) Validate() error {
if change.PoolId == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "pool id must not be 0")
}
if change.TickSpacing == 0 && change.MinOrderQuantity == nil && change.MinOrderQuote == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "no changes")
}
if change.TickSpacing != 0 {
if !IsAllowedTickSpacing(change.TickSpacing) {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "tick spacing %d is not allowed", change.TickSpacing)
}
}
if change.MinOrderQuantity != nil {
if change.MinOrderQuantity.IsNegative() {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "min order quantity must not be negative: %s", change.MinOrderQuantity)
}
}
if change.MinOrderQuote != nil {
if change.MinOrderQuote.IsNegative() {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "min order quote must not be negative: %s", change.MinOrderQuote)
}
}
return nil
}
func NewPublicFarmingPlanProposal(
title, description string,
createReqs []CreatePublicFarmingPlanRequest, termReqs []TerminateFarmingPlanRequest) *PublicFarmingPlanProposal {
return &PublicFarmingPlanProposal{
Title: title,
Description: description,
CreateRequests: createReqs,
TerminateRequests: termReqs,
}
}
func (p *PublicFarmingPlanProposal) GetTitle() string { return p.Title }
func (p *PublicFarmingPlanProposal) GetDescription() string { return p.Description }
func (p *PublicFarmingPlanProposal) ProposalRoute() string { return RouterKey }
func (p *PublicFarmingPlanProposal) ProposalType() string {
return ProposalTypePublicFarmingPlan
}
func (p *PublicFarmingPlanProposal) ValidateBasic() error {
if err := gov.ValidateAbstract(p); err != nil {
return err
}
if len(p.CreateRequests) == 0 && len(p.TerminateRequests) == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "requests must not be empty")
}
for _, createReq := range p.CreateRequests {
if err := createReq.Validate(); err != nil {
return err
}
}
for _, termReq := range p.TerminateRequests {
if err := termReq.Validate(); err != nil {
return err
}
}
return nil
}
func (p PublicFarmingPlanProposal) String() string {
var b strings.Builder
b.WriteString(fmt.Sprintf(`Public Farming Plan Proposal:
Title: %s
Description: %s
Create Requests:
`, p.Title, p.Description))
for _, createReq := range p.CreateRequests {
b.WriteString(fmt.Sprintf(` Create Public Farming Plan Request:
Description: %s
Farming Pool Address: %s
Termination Address: %s
Start Time: %s
End Time: %s
Reward Allocations:
`, createReq.Description, createReq.FarmingPoolAddress, createReq.TerminationAddress,
createReq.StartTime, createReq.EndTime))
for _, rewardAlloc := range createReq.RewardAllocations {
b.WriteString(fmt.Sprintf(` Reward Allocation:
Pool Id: %d
Rewards Per Day: %s
`, rewardAlloc.PoolId, rewardAlloc.RewardsPerDay))
}
}
b.WriteString(" Terminate Farming Plan Request:\n")
for _, termReq := range p.TerminateRequests {
b.WriteString(fmt.Sprintf(` Terminate Public Farming Plan Request:
Farming Plan Id: %d
`, termReq.FarmingPlanId))
}
return b.String()
}
func NewCreatePublicFarmingPlanRequest(
description string, farmingPoolAddr, termAddr sdk.AccAddress,
rewardAllocs []FarmingRewardAllocation, startTime, endTime time.Time) CreatePublicFarmingPlanRequest {
return CreatePublicFarmingPlanRequest{
Description: description,
FarmingPoolAddress: farmingPoolAddr.String(),
TerminationAddress: termAddr.String(),
RewardAllocations: rewardAllocs,
StartTime: startTime,
EndTime: endTime,
}
}
func (req CreatePublicFarmingPlanRequest) Validate() error {
dummyPlan := FarmingPlan{
Id: 1,
Description: req.Description,
FarmingPoolAddress: req.FarmingPoolAddress,
TerminationAddress: req.TerminationAddress,
RewardAllocations: req.RewardAllocations,
StartTime: req.StartTime,
EndTime: req.EndTime,
IsPrivate: false,
IsTerminated: false,
}
if err := dummyPlan.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
return nil
}
func NewTerminateFarmingPlanRequest(planId uint64) TerminateFarmingPlanRequest {
return TerminateFarmingPlanRequest{FarmingPlanId: planId}
}
func (req TerminateFarmingPlanRequest) Validate() error {
if req.FarmingPlanId == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "farming plan id must not be zero")
}
return nil
}