/
proposal.go
106 lines (93 loc) 路 2.94 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
package types
import (
"fmt"
"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 (
ProposalTypeFarmingPlan string = "FarmingPlan"
)
var (
_ gov.Content = &FarmingPlanProposal{}
)
func init() {
gov.RegisterProposalType(ProposalTypeFarmingPlan)
gov.RegisterProposalTypeCodec(&FarmingPlanProposal{}, "crescent/FarmingPlanProposal")
}
func NewFarmingPlanProposal(
title, description string,
createPlanReqs []CreatePlanRequest,
terminatePlanReqs []TerminatePlanRequest) *FarmingPlanProposal {
return &FarmingPlanProposal{
Title: title,
Description: description,
CreatePlanRequests: createPlanReqs,
TerminatePlanRequests: terminatePlanReqs,
}
}
func (p *FarmingPlanProposal) GetTitle() string { return p.Title }
func (p *FarmingPlanProposal) GetDescription() string { return p.Description }
func (p *FarmingPlanProposal) ProposalRoute() string { return RouterKey }
func (p *FarmingPlanProposal) ProposalType() string { return ProposalTypeFarmingPlan }
func (p *FarmingPlanProposal) ValidateBasic() error {
for _, req := range p.CreatePlanRequests {
if err := req.Validate(); err != nil {
return err
}
}
for _, req := range p.TerminatePlanRequests {
if err := req.Validate(); err != nil {
return err
}
}
return gov.ValidateAbstract(p)
}
func (p FarmingPlanProposal) String() string {
return fmt.Sprintf(`Farming Plan Proposal:
Title: %s
Description: %s
CreatePlanRequests: %v
TerminatePlanRequests: %v
`, p.Title, p.Description, p.CreatePlanRequests, p.TerminatePlanRequests)
}
func NewCreatePlanRequest(
description string, farmingPoolAddr sdk.AccAddress,
rewardAllocs []RewardAllocation, startTime, endTime time.Time) CreatePlanRequest {
return CreatePlanRequest{
Description: description,
FarmingPoolAddress: farmingPoolAddr.String(),
RewardAllocations: rewardAllocs,
StartTime: startTime,
EndTime: endTime,
}
}
func (req CreatePlanRequest) Validate() error {
farmingPoolAddr, err := sdk.AccAddressFromBech32(req.FarmingPoolAddress)
if err != nil {
return err
}
dummyPlan := NewPlan(
1, req.Description, farmingPoolAddr, farmingPoolAddr,
req.RewardAllocations, req.StartTime, req.EndTime, false)
if err := dummyPlan.Validate(); err != nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
for _, rewardAlloc := range req.RewardAllocations {
if rewardAlloc.PairId > 0 {
return sdkerrors.Wrapf(
sdkerrors.ErrInvalidRequest, "pair reward allocation for %d is disabled", rewardAlloc.PairId)
}
}
return nil
}
func NewTerminatePlanRequest(planId uint64) TerminatePlanRequest {
return TerminatePlanRequest{PlanId: planId}
}
func (req TerminatePlanRequest) Validate() error {
if req.PlanId == 0 {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "plan id must not be zero")
}
return nil
}