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
//! # Votes Module
//!
//! This module provides utilities for tracking voting power per account with
//! historical checkpoints. It supports delegation (an account can delegate its
//! voting power to another account) and provides historical vote queries at any
//! past ledger sequence number.
//!
//! # Core Concepts
//!
//! - **Voting Units**: The base unit of voting power, typically 1:1 with token
//! balance
//! - **Delegation**: Accounts can delegate their voting power to another
//! account (delegatee). **Only delegated voting power counts as votes** while
//! undelegated voting units are not counted. Self-delegation is required for
//! an account to use its own voting power. There is no explicit "undelegate"
//! operation — to reclaim voting power, an account delegates to itself.
//! - **Checkpoints**: Historical snapshots of voting power at specific ledger
//! sequence numbers
//!
//! # Usage
//!
//! This module is to be integrated into a token contract and is responsible
//! for:
//! - Overriding the transfer method to call `transfer_voting_units` on every
//! balance change (mint/burn/transfer), as shown in the example below
//! - Exposing delegation functionality to users
//!
//! # Example
//!
//! ```ignore
//! use stellar_governance::votes::{
//! delegate, get_votes, get_votes_at_checkpoint, transfer_voting_units,
//! };
//!
//! // Override the token contract's transfer to update voting units:
//! pub fn transfer(e: &Env, from: Address, to: Address, amount: i128) {
//! // ... perform transfer logic ...
//! transfer_voting_units(e, Some(&from), Some(&to), amount as u128);
//! }
//!
//! // Expose delegation:
//! pub fn delegate(e: &Env, account: Address, delegatee: Address) {
//! votes::delegate(e, &account, &delegatee);
//! }
//! ```
use ;
pub use crate;
/// Trait for contracts that support vote tracking with delegation.
///
/// This trait defines the interface for vote tracking functionality.
/// Contracts implementing this trait can be used in governance systems
/// that require historical vote queries and delegation.
///
/// # Implementation Notes
///
/// The implementing contract must:
/// - Call `transfer_voting_units` on every balance change
/// - Expose `delegate` functionality to users
// ################## ERRORS ##################
/// Errors that can occur in votes operations.
// ################## CONSTANTS ##################
const DAY_IN_LEDGERS: u32 = 17280;
/// TTL extension amount for storage entries (in ledgers)
pub const VOTES_EXTEND_AMOUNT: u32 = 30 * DAY_IN_LEDGERS;
/// TTL threshold for extending storage entries (in ledgers)
pub const VOTES_TTL_THRESHOLD: u32 = VOTES_EXTEND_AMOUNT - DAY_IN_LEDGERS;
// ################## EVENTS ##################
/// Event emitted when an account changes its delegate.
/// Emits an event when an account changes its delegate.
///
/// # Arguments
///
/// * `e` - Access to Soroban environment.
/// * `delegator` - The account that changed its delegate.
/// * `from_delegate` - The previous delegate (if any).
/// * `to_delegate` - The new delegate.
/// Event emitted when a delegate's voting power changes.
/// Emits an event when a delegate's voting power changes.
///
/// # Arguments
///
/// * `e` - Access to Soroban environment.
/// * `delegate` - The delegate whose voting power changed.
/// * `previous_votes` - The previous voting power.
/// * `new_votes` - The new voting power.