1
- use near_sdk:: json_types:: U128 ;
2
1
use crate :: * ;
3
-
2
+ use near_sdk :: json_types :: U128 ;
4
3
5
4
#[ near( serializers=[ borsh, json] ) ]
6
5
#[ derive( Clone , PartialEq ) ]
@@ -11,8 +10,6 @@ pub enum ElectionType {
11
10
Custom ( String , Option < AccountId > ) ,
12
11
}
13
12
14
-
15
-
16
13
#[ near( serializers=[ borsh, json] ) ]
17
14
#[ derive( Clone , PartialEq ) ]
18
15
pub enum ApplicationStatus {
@@ -25,10 +22,9 @@ pub enum ElectionPhase {
25
22
Pending ,
26
23
Nomination ,
27
24
Voting ,
28
- Ended
25
+ Ended ,
29
26
}
30
27
31
-
32
28
#[ near( serializers=[ borsh, json] ) ]
33
29
#[ derive( Clone , PartialEq ) ]
34
30
pub struct Candidate {
@@ -38,14 +34,13 @@ pub struct Candidate {
38
34
pub application_date : U64 ,
39
35
}
40
36
41
-
42
37
#[ near( serializers=[ borsh, json] ) ]
43
38
#[ derive( Clone , PartialEq ) ]
44
39
pub enum EligibilityType {
45
40
Open ,
46
- ListBased ( AccountId , U128 ) , // list contract and id
41
+ ListBased ( AccountId , U128 ) , // list contract and id
47
42
TokenBased ( AccountId , U128 ) , // Token contract and minimum balance
48
- Custom ( String ) , // Custom eligibility contract address
43
+ Custom ( String ) , // Custom eligibility contract address
49
44
}
50
45
51
46
#[ near( serializers=[ borsh, json] ) ]
@@ -59,7 +54,6 @@ pub enum ElectionStatus {
59
54
Cancelled ,
60
55
}
61
56
62
-
63
57
#[ near( serializers=[ borsh, json] ) ]
64
58
#[ derive( Clone , PartialEq ) ]
65
59
pub struct Election {
@@ -93,11 +87,14 @@ impl Contract {
93
87
voter_eligibility : EligibilityType ,
94
88
voting_type : VotingType ,
95
89
election_type : ElectionType ,
96
- candidates : Vec < AccountId >
90
+ candidates : Vec < AccountId > ,
97
91
) -> ElectionId {
98
92
self . assert_not_paused ( ) ;
99
93
self . assert_admin_or_owner ( ) ;
100
- assert ! ( start_date. 0 < end_date. 0 , "Start date must be before end date" ) ;
94
+ assert ! (
95
+ start_date. 0 < end_date. 0 ,
96
+ "Start date must be before end date"
97
+ ) ;
101
98
let initial_storage_usage = env:: storage_usage ( ) ;
102
99
103
100
let election_id = self . election_counter ;
@@ -124,12 +121,15 @@ impl Contract {
124
121
let mut candidates_map: IterableMap < AccountId , Candidate > =
125
122
IterableMap :: new ( StorageKey :: Candidates { election_id } ) ;
126
123
for candidate in candidates. clone ( ) {
127
- candidates_map. insert ( candidate. clone ( ) , Candidate {
128
- account_id : candidate,
129
- status : ApplicationStatus :: Approved ,
130
- votes_received : 0 ,
131
- application_date : U64 ( env:: block_timestamp ( ) ) ,
132
- } ) ;
124
+ candidates_map. insert (
125
+ candidate. clone ( ) ,
126
+ Candidate {
127
+ account_id : candidate,
128
+ status : ApplicationStatus :: Approved ,
129
+ votes_received : 0 ,
130
+ application_date : U64 ( env:: block_timestamp_ms ( ) ) ,
131
+ } ,
132
+ ) ;
133
133
}
134
134
self . candidates . insert ( election_id, candidates_map) ;
135
135
@@ -148,8 +148,8 @@ impl Contract {
148
148
#[payable]
149
149
pub fn apply(&mut self, election_id: ElectionId) {
150
150
// let election = self.elections.get(&election_id).expect("Election not found");
151
- // println!("Nomination end date: {:?}", env::block_timestamp ());
152
-
151
+ // println!("Nomination end date: {:?}", env::block_timestamp_ms ());
152
+
153
153
154
154
// let applicant = env::predecessor_account_id();
155
155
// // assert!(self.is_eligible_candidate(&election, &applicant), "Not eligible to be a candidate");
@@ -162,9 +162,9 @@ impl Contract {
162
162
// ApplicationStatus::Pending
163
163
// },
164
164
// votes_received: 0,
165
- // application_date: U64(env::block_timestamp ()),
165
+ // application_date: U64(env::block_timestamp_ms ()),
166
166
// approval_date: if election.auto_approval {
167
- // Some(U64(env::block_timestamp ()))
167
+ // Some(U64(env::block_timestamp_ms ()))
168
168
// } else {
169
169
// None
170
170
// },
@@ -184,7 +184,7 @@ impl Contract {
184
184
// ) {
185
185
// // self.assert_not_paused();
186
186
// self.assert_admin_or_owner();
187
-
187
+
188
188
// let election = self.elections.get(&election_id).expect("Election not found");
189
189
// let mut candidates_map = self.candidates
190
190
// .get_mut(&election_id)
@@ -205,9 +205,9 @@ impl Contract {
205
205
// account_id: candidate,
206
206
// status: ApplicationStatus::Approved,
207
207
// votes_received: 0,
208
- // application_date: U64(env::block_timestamp ()),
208
+ // application_date: U64(env::block_timestamp_ms ()),
209
209
// };
210
-
210
+
211
211
// candidates_map.insert(candidate.account_id.clone(), candidate);
212
212
213
213
// }
@@ -216,7 +216,6 @@ impl Contract {
216
216
// // candidates_map.insert(applicant, candidate);
217
217
// // // self.candidates.insert(election_id, &candidates_map);
218
218
219
-
220
219
// self.candidates.insert(election_id, candidates_map);
221
220
222
221
// // Refund excess deposit
@@ -235,16 +234,24 @@ impl Contract {
235
234
236
235
// candidate.status = status;
237
236
// if matches!(candidate.status, ApplicationStatus::Approved) {
238
- // candidate.approval_date = Some(U64(env::block_timestamp ()));
237
+ // candidate.approval_date = Some(U64(env::block_timestamp_ms ()));
239
238
// }
240
239
// }
241
240
242
- pub fn get_elections ( & self , from_index : Option < u128 > , limit : Option < u128 > , ) -> Vec < Election > {
241
+ pub fn get_elections ( & self , from_index : Option < u128 > , limit : Option < u128 > ) -> Vec < Election > {
243
242
let start_index = from_index. unwrap_or_default ( ) ;
244
- assert ! ( start_index < self . election_counter as u128 , "Invalid start index" ) ;
243
+ assert ! (
244
+ start_index < self . election_counter as u128 ,
245
+ "Invalid start index"
246
+ ) ;
245
247
let limit = limit. map ( |v| v as usize ) . unwrap_or ( usize:: MAX ) ;
246
248
247
- self . elections . iter ( ) . map ( |( _, election) | election. clone ( ) ) . skip ( start_index as usize ) . take ( limit) . collect ( )
249
+ self . elections
250
+ . iter ( )
251
+ . map ( |( _, election) | election. clone ( ) )
252
+ . skip ( start_index as usize )
253
+ . take ( limit)
254
+ . collect ( )
248
255
}
249
256
250
257
pub fn get_election ( & self , election_id : & ElectionId ) -> Option < Election > {
@@ -254,15 +261,15 @@ impl Contract {
254
261
/// Returns whether an election is currently in the voting period
255
262
pub fn is_voting_period ( & self , election_id : & ElectionId ) -> bool {
256
263
self . elections . get ( election_id) . map_or ( false , |election| {
257
- let now = env:: block_timestamp ( ) ;
264
+ let now = env:: block_timestamp_ms ( ) ;
258
265
now >= election. start_date . 0 && now < election. end_date . 0
259
266
} )
260
267
}
261
268
262
269
/// Returns whether an election has ended
263
270
pub fn is_election_ended ( & self , election_id : & ElectionId ) -> bool {
264
271
self . elections . get ( election_id) . map_or ( false , |election| {
265
- env:: block_timestamp ( ) >= election. end_date . 0
272
+ env:: block_timestamp_ms ( ) >= election. end_date . 0
266
273
} )
267
274
}
268
275
@@ -277,25 +284,21 @@ impl Contract {
277
284
278
285
/// Returns all active elections (in nomination or voting period)
279
286
pub fn get_active_elections ( & self ) -> Vec < ( ElectionId , Election ) > {
280
- let now = env:: block_timestamp ( ) ;
287
+ let now = env:: block_timestamp_ms ( ) ;
281
288
self . elections
282
289
. iter ( )
283
- . filter ( |( _, election) | {
284
- now >= election. start_date . 0 && now < election. end_date . 0
285
- } )
290
+ . filter ( |( _, election) | now >= election. start_date . 0 && now < election. end_date . 0 )
286
291
. map ( |( id, election) | ( * id, election. clone ( ) ) )
287
292
. collect ( )
288
293
}
289
294
290
295
/// Returns the current phase of an election
291
296
pub fn get_election_phase ( & self , election_id : & ElectionId ) -> Option < ElectionPhase > {
292
297
self . elections . get ( election_id) . map ( |election| {
293
- let now = env:: block_timestamp ( ) ;
298
+ let now = env:: block_timestamp_ms ( ) ;
294
299
if now < election. start_date . 0 {
295
300
ElectionPhase :: Pending
296
- } else if now < election. end_date . 0 {
297
- ElectionPhase :: Nomination
298
- } else if now < election. end_date . 0 {
301
+ } else if now >= election. start_date . 0 && now < election. end_date . 0 {
299
302
ElectionPhase :: Voting
300
303
} else {
301
304
ElectionPhase :: Ended
@@ -304,27 +307,27 @@ impl Contract {
304
307
}
305
308
306
309
pub fn get_election_candidates ( & self , election_id : ElectionId ) -> Vec < Candidate > {
307
- let candidates_map = self . candidates
310
+ let candidates_map = self
311
+ . candidates
308
312
. get ( & election_id)
309
313
. expect ( "Election not found" ) ;
310
-
314
+
311
315
candidates_map. values ( ) . cloned ( ) . collect ( )
312
316
}
313
317
314
318
pub fn get_election_votes ( & self , election_id : ElectionId ) -> Vec < Vote > {
315
- let votes_map = self . votes
316
- . get ( & election_id)
317
- . expect ( "Election not found" ) ;
318
-
319
- votes_map. values ( )
319
+ let votes_map = self . votes . get ( & election_id) . expect ( "Election not found" ) ;
320
+
321
+ votes_map
322
+ . values ( )
320
323
. flat_map ( |votes| votes. iter ( ) . cloned ( ) )
321
324
. collect ( )
322
325
}
323
326
324
327
/// Returns time remaining (in nanoseconds) in the current phase
325
328
pub fn get_time_remaining ( & self , election_id : & ElectionId ) -> Option < u64 > {
326
329
self . elections . get ( election_id) . map ( |election| {
327
- let now = env:: block_timestamp ( ) ;
330
+ let now = env:: block_timestamp_ms ( ) ;
328
331
if now < election. start_date . 0 {
329
332
election. start_date . 0 - now
330
333
} else if now < election. end_date . 0 {
0 commit comments