-
Notifications
You must be signed in to change notification settings - Fork 3
/
types.go
292 lines (244 loc) · 6.69 KB
/
types.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package gov
import (
fmt "fmt"
"github.com/gogo/protobuf/proto"
yaml "gopkg.in/yaml.v2"
"github.com/oracleNetworkProtocol/plugchain-sdk-go/codec/types"
sdk "github.com/oracleNetworkProtocol/plugchain-sdk-go/types"
)
const (
ModuleName = "gov"
AttributeKeyProposalId = "proposal_id"
)
var (
_ sdk.Msg = &MsgSubmitProposal{}
_ sdk.Msg = &MsgDeposit{}
_ sdk.Msg = &MsgVote{}
)
// NewMsgSubmitProposal creates a new MsgSubmitProposal.
//nolint:interfacer
func NewMsgSubmitProposal(content Content, initialDeposit sdk.Coins, proposer sdk.AccAddress) (*MsgSubmitProposal, error) {
m := &MsgSubmitProposal{
InitialDeposit: initialDeposit,
Proposer: proposer.String(),
}
err := m.SetContent(content)
if err != nil {
return nil, err
}
return m, nil
}
func (m *MsgSubmitProposal) GetInitialDeposit() sdk.Coins { return m.InitialDeposit }
func (m *MsgSubmitProposal) GetProposer() sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return proposer
}
func (m *MsgSubmitProposal) SetContent(content Content) error {
msg, ok := content.(proto.Message)
if !ok {
return fmt.Errorf("can't proto marshal %T", msg)
}
any, err := types.NewAnyWithValue(msg)
if err != nil {
return err
}
m.Content = any
return nil
}
func (m *MsgSubmitProposal) GetContent() Content {
content, ok := m.Content.GetCachedValue().(Content)
if !ok {
return nil
}
return content
}
func (m MsgSubmitProposal) Route() string { return ModuleName }
// Type implements Msg
func (m MsgSubmitProposal) Type() string { return "submit_proposal" }
// ValidateBasic implements Msg
func (m MsgSubmitProposal) ValidateBasic() error {
if m.Proposer == "" {
return sdk.Wrapf("missing Proposer")
}
if !m.InitialDeposit.IsValid() {
return sdk.Wrapf("invalidCoins coins, %s", m.InitialDeposit.String())
}
if m.InitialDeposit.IsAnyNegative() {
return sdk.Wrapf("invalidCoins coins, %s", m.InitialDeposit.String())
}
content := m.GetContent()
if content == nil {
return sdk.Wrapf("missing content")
}
if err := content.ValidateBasic(); err != nil {
return err
}
return nil
}
// GetSignBytes implements Msg
func (m MsgSubmitProposal) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&m)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (m MsgSubmitProposal) GetSigners() []sdk.AccAddress {
proposer, _ := sdk.AccAddressFromBech32(m.Proposer)
return []sdk.AccAddress{proposer}
}
// String implements the Stringer interface
func (m MsgSubmitProposal) String() string {
out, _ := yaml.Marshal(m)
return string(out)
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (m MsgSubmitProposal) UnpackInterfaces(unpacker types.AnyUnpacker) error {
var content Content
return unpacker.UnpackAny(m.Content, &content)
}
func (msg MsgDeposit) Route() string { return ModuleName }
// Type implements Msg
func (msg MsgDeposit) Type() string { return "deposit" }
// ValidateBasic implements Msg
func (msg MsgDeposit) ValidateBasic() error {
if msg.Depositor == "" {
return sdk.Wrapf("missing Proposer")
}
if !msg.Amount.IsValid() {
return sdk.Wrapf("invalidCoins coins, %s", msg.Amount.String())
}
if msg.Amount.IsAnyNegative() {
return sdk.Wrapf("invalidCoins coins, %s", msg.Amount.String())
}
return nil
}
// String implements the Stringer interface
func (msg MsgDeposit) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSignBytes implements Msg
func (msg MsgDeposit) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgDeposit) GetSigners() []sdk.AccAddress {
depositor, _ := sdk.AccAddressFromBech32(msg.Depositor)
return []sdk.AccAddress{depositor}
}
func (msg MsgVote) Route() string { return ModuleName }
// Type implements Msg
func (msg MsgVote) Type() string { return "vote" }
// ValidateBasic implements Msg
func (msg MsgVote) ValidateBasic() error {
if msg.Voter == "" {
return sdk.Wrapf("missing Proposer")
}
if !ValidVoteOption(msg.Option) {
return sdk.Wrapf("invalid vote option %s", msg.Option.String())
}
return nil
}
func ValidVoteOption(option VoteOption) bool {
if option == OptionYes ||
option == OptionAbstain ||
option == OptionNo ||
option == OptionNoWithVeto {
return true
}
return false
}
// String implements the Stringer interface
func (msg MsgVote) String() string {
out, _ := yaml.Marshal(msg)
return string(out)
}
// GetSignBytes implements Msg
func (msg MsgVote) GetSignBytes() []byte {
bz := ModuleCdc.MustMarshalJSON(&msg)
return sdk.MustSortJSON(bz)
}
// GetSigners implements Msg
func (msg MsgVote) GetSigners() []sdk.AccAddress {
voter, _ := sdk.AccAddressFromBech32(msg.Voter)
return []sdk.AccAddress{voter}
}
func (q Proposal) Convert() interface{} {
return QueryProposalResp{
ProposalId: q.ProposalId,
Status: ProposalStatus_name[int32(q.Status)],
FinalTallyResult: QueryTallyResultResp{
Yes: q.FinalTallyResult.Yes,
Abstain: q.FinalTallyResult.Abstain,
No: q.FinalTallyResult.No,
NoWithVeto: q.FinalTallyResult.NoWithVeto,
},
SubmitTime: q.SubmitTime,
DepositEndTime: q.DepositEndTime,
TotalDeposit: q.TotalDeposit,
VotingStartTime: q.VotingStartTime,
VotingEndTime: q.VotingEndTime,
}
}
type Proposals []Proposal
func (qs Proposals) Convert() interface{} {
var res []QueryProposalResp
for _, q := range qs {
res = append(res, q.Convert().(QueryProposalResp))
}
return res
}
func (v Vote) Convert() interface{} {
return QueryVoteResp{
ProposalId: v.ProposalId,
Voter: v.Voter,
Option: int32(v.Option),
}
}
type Votes []Vote
func (vs Votes) Convert() interface{} {
var res []QueryVoteResp
for _, v := range vs {
res = append(res, v.Convert().(QueryVoteResp))
}
return res
}
func (q QueryParamsResponse) Convert() interface{} {
return QueryParamsResp{
VotingParams: votingParams{
VotingPeriod: q.VotingParams.VotingPeriod,
},
DepositParams: depositParams{
MinDeposit: q.DepositParams.MinDeposit,
MaxDepositPeriod: q.DepositParams.MaxDepositPeriod,
},
TallyParams: tallyParams{
Quorum: q.TallyParams.Quorum,
Threshold: q.TallyParams.Threshold,
VetoThreshold: q.TallyParams.VetoThreshold,
},
}
}
func (d Deposit) Convert() interface{} {
return QueryDepositResp{
ProposalId: d.ProposalId,
Depositor: d.Depositor,
Amount: d.Amount,
}
}
type Deposits []Deposit
func (ds Deposits) Convert() interface{} {
var res []QueryDepositResp
for _, d := range ds {
res = append(res, d.Convert().(QueryDepositResp))
}
return res
}
func (t TallyResult) Convert() interface{} {
return QueryTallyResultResp{
Yes: t.Yes,
Abstain: t.Abstain,
No: t.No,
NoWithVeto: t.NoWithVeto,
}
}