forked from terra-money/classic-core
/
types.go
246 lines (206 loc) · 6.59 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
package budget
import (
"encoding/json"
"fmt"
"strings"
"time"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// RouterKey is they name of the budget module
const (
RouterKey = "budget"
// QuerierRoute is the querier route for budget
QuerierRoute = "budget"
)
// Program defines the basic properties of a staking Program
type Program struct {
Title string `json:"title"` // Title of the Program
Description string `json:"description"` // Description of the Program
Submitter sdk.AccAddress `json:"submitter"` // Validator address of the proposer
Executor sdk.AccAddress `json:"executor"` // Account address of the executor
SubmitTime time.Time `json:"submit_time"` // Block height from which the Program is open for votations
Deposit sdk.Coin `json:"deposit"` // Coins deposited in escrow
Tally sdk.Int `json:"tally_result"`
}
// NewProgram validates deposit and creates a new Program
func NewProgram(
title string,
description string,
submitter sdk.AccAddress,
executor sdk.AccAddress,
submitTime time.Time,
deposit sdk.Coin) Program {
return Program{
Title: title,
Description: description,
Submitter: submitter,
Executor: executor,
SubmitTime: submitTime,
Deposit: deposit,
Tally: sdk.ZeroInt(),
}
}
func (p *Program) getVotingEndTime(votingPeriod time.Duration) time.Time {
return p.SubmitTime.Add(votingPeriod)
}
// updateTally updates the counter for each of the available options
func (p *Program) updateTally(option bool, power sdk.Int) {
if option {
p.Tally = p.Tally.Add(power)
} else {
p.Tally = p.Tally.Sub(power)
}
}
// String implements fmt.Stringer
func (p Program) String() string {
return fmt.Sprintf("Program{ Title: %s, Description: %s, Submitter: %v, Executor: %v, SubmitTime: %v, Deposit: %v}",
p.Title, p.Description, p.Submitter, p.Executor, p.SubmitTime, p.Deposit)
}
//--------------------------------------------------------
//--------------------------------------------------------
//SubmitProgramMsg defines a message to create a Program
type SubmitProgramMsg struct {
Title string // Title of the Program
Description string // Description of the Program
Deposit sdk.Coin // Deposit paid by submitter. Must be > MinDeposit to enter voting period
Submitter sdk.AccAddress // Address of the submitter
Executor sdk.AccAddress // Address of the executor
}
// NewSubmitProgramMsg submits a message with a new Program
func NewSubmitProgramMsg(title string, description string, deposit sdk.Coin,
submitter sdk.AccAddress, executor sdk.AccAddress) SubmitProgramMsg {
return SubmitProgramMsg{
Title: title,
Description: description,
Deposit: deposit,
Submitter: submitter,
Executor: executor,
}
}
// Route Implements Msg
func (msg SubmitProgramMsg) Route() string { return "budget" }
// Type implements sdk.Msg
func (msg SubmitProgramMsg) Type() string { return "submitprogram" }
// Implements Msg
func (msg SubmitProgramMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
// Implements Msg
func (msg SubmitProgramMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Submitter}
}
// Implements Msg
func (msg SubmitProgramMsg) ValidateBasic() sdk.Error {
if len(msg.Submitter) == 0 {
return sdk.ErrInvalidAddress("Invalid address: " + msg.Submitter.String())
}
if len(msg.Executor) == 0 {
return sdk.ErrInvalidAddress("Invalid address: " + msg.Executor.String())
}
if len(strings.TrimSpace(msg.Title)) <= 0 {
return ErrInvalidTitle()
}
if len(strings.TrimSpace(msg.Description)) <= 0 {
return ErrInvalidDescription()
}
if !msg.Deposit.IsPositive() {
return sdk.ErrInvalidCoins("Deposit is not valid")
}
return nil
}
func (msg SubmitProgramMsg) String() string {
return fmt.Sprintf("SubmitProgramMsg{%v, %v}", msg.Title, msg.Description)
}
//--------------------------------------------------------
//--------------------------------------------------------
// WithdrawProgramMsg defines the msg of a staker containing the vote option to an
// specific Program
type WithdrawProgramMsg struct {
ProgramID uint64 // ID of the Program
Submitter sdk.AccAddress // Address of the voter
}
// NewVoteMsg creates a VoteMsg instance
func NewWithdrawProgramMsg(programID uint64, submitter sdk.AccAddress) WithdrawProgramMsg {
return WithdrawProgramMsg{
ProgramID: programID,
Submitter: submitter,
}
}
// Route Implements Msg
func (msg WithdrawProgramMsg) Route() string { return "budget" }
// Type implements sdk.Msg
func (msg WithdrawProgramMsg) Type() string { return "withdraw" }
// Implements Msg
func (msg WithdrawProgramMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
// Implements Msg
func (msg WithdrawProgramMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Submitter}
}
// Implements Msg
func (msg WithdrawProgramMsg) ValidateBasic() sdk.Error {
if len(msg.Submitter) == 0 {
return sdk.ErrInvalidAddress("Invalid address: " + msg.Submitter.String())
}
return nil
}
// Implements Msg
func (msg WithdrawProgramMsg) String() string {
return fmt.Sprintf("WithdrawProgramMsg{%v, %v}", msg.ProgramID, msg.Submitter)
}
//--------------------------------------------------------
//--------------------------------------------------------
// VoteMsg defines the msg of a staker containing the vote option to an
// specific Program
type VoteMsg struct {
ProgramID uint64 // ID of the Program
Option bool // Option chosen by voter
Voter sdk.AccAddress // Address of the voter
}
// NewVoteMsg creates a VoteMsg instance
func NewVoteMsg(programID uint64, option bool, voter sdk.AccAddress) VoteMsg {
return VoteMsg{
ProgramID: programID,
Option: option,
Voter: voter,
}
}
// Route Implements Msg
func (msg VoteMsg) Route() string { return "budget" }
// Type implements sdk.Msg
func (msg VoteMsg) Type() string { return "vote" }
// Implements Msg
func (msg VoteMsg) GetSignBytes() []byte {
b, err := json.Marshal(msg)
if err != nil {
panic(err)
}
return b
}
// Implements Msg
func (msg VoteMsg) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Voter}
}
// Implements Msg
func (msg VoteMsg) ValidateBasic() sdk.Error {
if len(msg.Voter) == 0 {
return sdk.ErrInvalidAddress("Invalid address: " + msg.Voter.String())
}
if msg.ProgramID <= 0 {
return ErrInvalidProgramID("ProgramID cannot be negative")
}
return nil
}
// Implements Msg
func (msg VoteMsg) String() string {
return fmt.Sprintf("VoteMsg{%v, %v, %v}", msg.ProgramID, msg.Voter, msg.Option)
}