-
Notifications
You must be signed in to change notification settings - Fork 38
/
proposal.go
143 lines (114 loc) · 4.42 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
package v2
import (
"encoding/json"
"fmt"
errorsmod "cosmossdk.io/errors"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/ethereum/go-ethereum/common"
types "github.com/peggyjv/sommelier/v7/x/cork/types"
pubsubtypes "github.com/peggyjv/sommelier/v7/x/pubsub/types"
)
const (
ProposalTypeAddManagedCellarIDs = "v2/AddManagedCellarIDs"
ProposalTypeRemoveManagedCellarIDs = "v2/RemoveManagedCellarIDs"
ProposalTypeScheduledCork = "v2/ScheduledCork"
)
var _ govtypesv1beta1.Content = &AddManagedCellarIDsProposal{}
var _ govtypesv1beta1.Content = &RemoveManagedCellarIDsProposal{}
var _ govtypesv1beta1.Content = &ScheduledCorkProposal{}
func init() {
// The RegisterProposalTypeCodec function was mysteriously removed by in 0.46.0 even though
// the claim was that the old API would be preserved in .../x/gov/types/v1beta1 so we have
// to interact with the codec directly.
//
// The PR that removed it: https://github.com/cosmos/cosmos-sdk/pull/11240
// This PR was later reverted, but RegisterProposalTypeCodec was still left out. Not sure if
// this was intentional or not.
govtypesv1beta1.RegisterProposalType(ProposalTypeAddManagedCellarIDs)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&AddManagedCellarIDsProposal{}, "sommelier/v2/AddManagedCellarIDsProposal", nil)
govtypesv1beta1.RegisterProposalType(ProposalTypeRemoveManagedCellarIDs)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&RemoveManagedCellarIDsProposal{}, "sommelier/v2/RemoveManagedCellarIDsProposal", nil)
govtypesv1beta1.RegisterProposalType(ProposalTypeScheduledCork)
govtypesv1beta1.ModuleCdc.RegisterConcrete(&ScheduledCorkProposal{}, "sommelier/v2/ScheduledCorkProposal", nil)
}
func NewAddManagedCellarIDsProposal(title string, description string, cellarIds *CellarIDSet, publisherDomain string) *AddManagedCellarIDsProposal {
return &AddManagedCellarIDsProposal{
Title: title,
Description: description,
CellarIds: cellarIds,
PublisherDomain: publisherDomain,
}
}
func (m *AddManagedCellarIDsProposal) ProposalRoute() string {
return types.RouterKey
}
func (m *AddManagedCellarIDsProposal) ProposalType() string {
return ProposalTypeAddManagedCellarIDs
}
func (m *AddManagedCellarIDsProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(m); err != nil {
return err
}
if len(m.CellarIds.Ids) == 0 {
return fmt.Errorf("can't have an add prosoposal with no cellars")
}
if err := pubsubtypes.ValidateDomain(m.PublisherDomain); err != nil {
return err
}
return nil
}
func NewRemoveManagedCellarIDsProposal(title string, description string, cellarIds *CellarIDSet) *RemoveManagedCellarIDsProposal {
return &RemoveManagedCellarIDsProposal{
Title: title,
Description: description,
CellarIds: cellarIds,
}
}
func (m *RemoveManagedCellarIDsProposal) ProposalRoute() string {
return types.RouterKey
}
func (m *RemoveManagedCellarIDsProposal) ProposalType() string {
return ProposalTypeRemoveManagedCellarIDs
}
func (m *RemoveManagedCellarIDsProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(m); err != nil {
return err
}
if len(m.CellarIds.Ids) == 0 {
return fmt.Errorf("can't have a remove prosoposal with no cellars")
}
return nil
}
func NewScheduledCorkProposal(title string, description string, blockHeight uint64, targetContractAddress string, contractCallProtoJSON string) *ScheduledCorkProposal {
return &ScheduledCorkProposal{
Title: title,
Description: description,
BlockHeight: blockHeight,
TargetContractAddress: targetContractAddress,
ContractCallProtoJson: contractCallProtoJSON,
}
}
func (m *ScheduledCorkProposal) ProposalRoute() string {
return types.RouterKey
}
func (m *ScheduledCorkProposal) ProposalType() string {
return ProposalTypeScheduledCork
}
func (m *ScheduledCorkProposal) ValidateBasic() error {
if err := govtypesv1beta1.ValidateAbstract(m); err != nil {
return err
}
if m.BlockHeight == 0 {
return fmt.Errorf("block height must be non-zero")
}
if len(m.ContractCallProtoJson) == 0 {
return errorsmod.Wrapf(types.ErrInvalidJSON, "cannot have empty contract call")
}
if !json.Valid([]byte(m.ContractCallProtoJson)) {
return errorsmod.Wrapf(types.ErrInvalidJSON, "%s", m.ContractCallProtoJson)
}
if !common.IsHexAddress(m.TargetContractAddress) {
return errorsmod.Wrapf(types.ErrInvalidEthereumAddress, "%s", m.TargetContractAddress)
}
return nil
}