Skip to content

Commit a42e490

Browse files
authored
refactor: rename choice to vote (#7)
1 parent f706fe7 commit a42e490

3 files changed

Lines changed: 44 additions & 44 deletions

File tree

src/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub const EVENT_STANDARD_VERSION: &str = "1.0.0";
1818
pub enum Event<'a> {
1919
Voted {
2020
validator_id: &'a AccountId,
21-
choice: &'a Choice,
21+
vote: &'a Vote,
2222
},
2323
ProposalApproved {
2424
proposal: &'a String,

src/lib.rs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Timestamp = u64;
1717

1818
#[near(serializers = [json])]
1919
#[serde(rename_all = "lowercase")]
20-
pub enum Choice {
20+
pub enum Vote {
2121
Yes,
2222
No,
2323
}
@@ -86,16 +86,16 @@ impl Contract {
8686
}
8787
}
8888

89-
/// Method for validators to vote with choice `Yes` or `No`.
89+
/// Method for validators to vote with `Yes` or `No`.
9090
/// The method is called by validator owners.
91-
pub fn vote(&mut self, choice: Choice, staking_pool_id: AccountId) -> Promise {
91+
pub fn vote(&mut self, vote: Vote, staking_pool_id: AccountId) -> Promise {
9292
ext_staking_pool::ext(staking_pool_id.clone())
9393
.with_static_gas(GET_OWNER_ID_GAS)
9494
.get_owner_id()
9595
.then(Self::ext(env::current_account_id()).on_get_pool_owner_id(
9696
env::predecessor_account_id(),
9797
staking_pool_id,
98-
choice,
98+
vote,
9999
))
100100
}
101101

@@ -105,30 +105,30 @@ impl Contract {
105105
&mut self,
106106
pool_owner_id: AccountId,
107107
staking_pool_id: AccountId,
108-
choice: Choice,
108+
vote: Vote,
109109
#[callback_result] pool_owner_id_result: Result<AccountId, PromiseError>,
110110
) {
111111
if let Ok(actual_owner_id) = pool_owner_id_result {
112112
require!(
113113
pool_owner_id == actual_owner_id,
114114
"Voting is only allowed for the staking pool owner"
115115
);
116-
self.internal_vote(choice, staking_pool_id);
116+
self.internal_vote(vote, staking_pool_id);
117117
} else {
118118
env::panic_str("Failed to get the staking pool owner id");
119119
}
120120
}
121121

122122
/// Internal method for voting.
123-
fn internal_vote(&mut self, choice: Choice, account_id: AccountId) {
123+
fn internal_vote(&mut self, vote: Vote, account_id: AccountId) {
124124
self.ping();
125125

126126
let stake = validator_stake(&account_id);
127127
require!(stake > 0, format!("{} is not a validator", account_id));
128128

129-
let account_stake = match choice {
130-
Choice::Yes => stake,
131-
Choice::No => 0,
129+
let account_stake = match vote {
130+
Vote::Yes => stake,
131+
Vote::No => 0,
132132
};
133133

134134
let voted_stake = self.votes.remove(&account_id).unwrap_or_default();
@@ -147,7 +147,7 @@ impl Contract {
147147
// emit event
148148
Event::Voted {
149149
validator_id: &account_id,
150-
choice: &choice,
150+
vote: &vote,
151151
}
152152
.emit();
153153
}
@@ -302,20 +302,20 @@ mod tests {
302302

303303
fn vote_with_account(
304304
contract: &mut Contract,
305-
choice: Choice,
305+
vote: Vote,
306306
staking_pool_id: &AccountId,
307307
account: &AccountId,
308308
) {
309309
contract.on_get_pool_owner_id(
310310
account.clone(),
311311
staking_pool_id.clone(),
312-
choice,
312+
vote,
313313
Ok(pool_owner()),
314314
);
315315
}
316316

317-
fn vote(contract: &mut Contract, choice: Choice, staking_pool_id: &AccountId) {
318-
vote_with_account(contract, choice, staking_pool_id, &pool_owner());
317+
fn vote(contract: &mut Contract, vote: Vote, staking_pool_id: &AccountId) {
318+
vote_with_account(contract, vote, staking_pool_id, &pool_owner());
319319
}
320320

321321
#[test]
@@ -328,7 +328,7 @@ mod tests {
328328
]);
329329
set_context_and_validators(&context, &validators);
330330
let mut contract = get_contract();
331-
vote(&mut contract, Choice::Yes, &validator(3));
331+
vote(&mut contract, Vote::Yes, &validator(3));
332332
}
333333

334334
#[test]
@@ -341,7 +341,7 @@ mod tests {
341341
]);
342342
set_context_and_validators(&context, &validators);
343343
let mut contract = get_contract();
344-
vote(&mut contract, Choice::No, &validator(3));
344+
vote(&mut contract, Vote::No, &validator(3));
345345
}
346346

347347
#[test]
@@ -356,10 +356,10 @@ mod tests {
356356
set_context_and_validators(&context, &validators);
357357
let mut contract = get_contract();
358358
// vote
359-
vote(&mut contract, Choice::Yes, &validator_id);
359+
vote(&mut contract, Vote::Yes, &validator_id);
360360
assert!(contract.get_result().is_some());
361361
// vote again. should panic because voting has ended
362-
vote(&mut contract, Choice::Yes, &validator_id);
362+
vote(&mut contract, Vote::Yes, &validator_id);
363363
}
364364

365365
#[test]
@@ -375,7 +375,7 @@ mod tests {
375375
let mut contract = get_contract();
376376
// vote with an account that is not the pool owner.
377377
// should panic because only the pool owner can vote.
378-
vote_with_account(&mut contract, Choice::Yes, &validator_id, &accounts(0));
378+
vote_with_account(&mut contract, Vote::Yes, &validator_id, &accounts(0));
379379
}
380380

381381
#[test]
@@ -387,7 +387,7 @@ mod tests {
387387
for i in 0..201 {
388388
// vote by each validator
389389
let voter = validator(i);
390-
vote(&mut contract, Choice::Yes, &voter);
390+
vote(&mut contract, Vote::Yes, &voter);
391391

392392
// check total voted stake
393393
context.is_view(true);
@@ -420,7 +420,7 @@ mod tests {
420420
// vote by each validator
421421
let context = get_context_with_epoch_height(&voting_contract_id(), i);
422422
set_context(&context);
423-
vote(&mut contract, Choice::Yes, &validator(i));
423+
vote(&mut contract, Vote::Yes, &validator(i));
424424
// check votes
425425
assert_eq!(contract.get_votes().len() as u64, i + 1);
426426
// check voting result
@@ -443,7 +443,7 @@ mod tests {
443443
let context = get_context_with_epoch_height(&voting_contract_id(), 1);
444444
set_context_and_validators(&context, &validators);
445445
let mut contract = get_contract();
446-
vote(&mut contract, Choice::Yes, &validator(1));
446+
vote(&mut contract, Vote::Yes, &validator(1));
447447
// ping at epoch 2
448448
validators.insert(validator(1).to_string(), NearToken::from_yoctonear(50));
449449
let context = get_context_with_epoch_height(&voting_contract_id(), 2);
@@ -453,7 +453,7 @@ mod tests {
453453
}
454454

455455
#[test]
456-
fn test_change_vote_choice() {
456+
fn test_change_vote() {
457457
let validators: HashMap<String, NearToken> = HashMap::from_iter(vec![
458458
(validator(1).to_string(), NearToken::from_yoctonear(10)),
459459
(validator(2).to_string(), NearToken::from_yoctonear(10)),
@@ -462,17 +462,17 @@ mod tests {
462462
set_context_and_validators(&context, &validators);
463463
let mut contract = get_contract();
464464
// vote YES at epoch 1
465-
vote(&mut contract, Choice::Yes, &validator(1));
465+
vote(&mut contract, Vote::Yes, &validator(1));
466466
assert_eq!(contract.get_votes().len(), 1);
467467
// vote NO at epoch 2
468468
let context = get_context_with_epoch_height(&voting_contract_id(), 2);
469469
set_context_and_validators(&context, &validators);
470-
vote(&mut contract, Choice::No, &validator(1));
470+
vote(&mut contract, Vote::No, &validator(1));
471471
assert!(contract.get_votes().is_empty());
472472
// vote YES at epoch 3
473473
let context = get_context_with_epoch_height(&voting_contract_id(), 3);
474474
set_context_and_validators(&context, &validators);
475-
vote(&mut contract, Choice::Yes, &validator(1));
475+
vote(&mut contract, Vote::Yes, &validator(1));
476476
assert_eq!(contract.get_votes().len(), 1);
477477
}
478478

@@ -487,7 +487,7 @@ mod tests {
487487
set_context_and_validators(&context, &validators);
488488
let mut contract = get_contract();
489489
// vote at epoch 1
490-
vote(&mut contract, Choice::Yes, &validator(1));
490+
vote(&mut contract, Vote::Yes, &validator(1));
491491
assert_eq!((contract.get_total_voted_stake().0).0, 40);
492492
assert_eq!(contract.get_votes().len(), 1);
493493
// remove validator at epoch 2
@@ -534,7 +534,7 @@ mod tests {
534534

535535
// vote after deadline
536536
set_context(context.block_timestamp(env::block_timestamp_ms() + 2000 * 1_000_000));
537-
vote(&mut contract, Choice::Yes, &validator(0));
537+
vote(&mut contract, Vote::Yes, &validator(0));
538538
}
539539

540540
#[test]
@@ -545,7 +545,7 @@ mod tests {
545545

546546
// vote at epoch 1
547547
set_context(&context);
548-
vote(&mut contract, Choice::Yes, &validator(0));
548+
vote(&mut contract, Vote::Yes, &validator(0));
549549

550550
// ping at epoch 2 after deadline
551551
set_context(

tests/test_vote.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async fn test_non_validator_cannot_vote() -> Result<(), Box<dyn std::error::Erro
2222
let user_account = sandbox.dev_create_account().await?;
2323
let outcome = user_account
2424
.call(contract.id(), "vote")
25-
.args_json(json!({"choice": "yes", "staking_pool_id": user_account.id()}))
25+
.args_json(json!({"vote": "yes", "staking_pool_id": user_account.id()}))
2626
.transact()
2727
.await?;
2828
assert!(
@@ -34,7 +34,7 @@ async fn test_non_validator_cannot_vote() -> Result<(), Box<dyn std::error::Erro
3434
let user_account = sandbox.dev_create_account().await?;
3535
let outcome = user_account
3636
.call(contract.id(), "vote")
37-
.args_json(json!({"choice": "no", "staking_pool_id": user_account.id()}))
37+
.args_json(json!({"vote": "no", "staking_pool_id": user_account.id()}))
3838
.transact()
3939
.await?;
4040
assert!(
@@ -66,7 +66,7 @@ async fn test_only_pool_owner_can_vote() -> Result<(), Box<dyn std::error::Error
6666

6767
let outcome = alice
6868
.call(voting_contract.id(), "vote")
69-
.args_json(json!({"choice": "yes", "staking_pool_id": staking_pool_contract.id()}))
69+
.args_json(json!({"vote": "yes", "staking_pool_id": staking_pool_contract.id()}))
7070
.transact()
7171
.await?;
7272
assert!(
@@ -77,7 +77,7 @@ async fn test_only_pool_owner_can_vote() -> Result<(), Box<dyn std::error::Error
7777

7878
let outcome = alice
7979
.call(voting_contract.id(), "vote")
80-
.args_json(json!({"choice": "no", "staking_pool_id": staking_pool_contract.id()}))
80+
.args_json(json!({"vote": "no", "staking_pool_id": staking_pool_contract.id()}))
8181
.transact()
8282
.await?;
8383
assert!(
@@ -117,7 +117,7 @@ async fn test_simple_vote() -> Result<(), Box<dyn std::error::Error>> {
117117
let outcome = owner
118118
.call(voting_contract.id(), "vote")
119119
.args_json(json!({
120-
"choice": "yes",
120+
"vote": "yes",
121121
"staking_pool_id": staking_pool_contract.id()
122122
}))
123123
.gas(Gas::from_tgas(200))
@@ -188,7 +188,7 @@ async fn test_many_votes() -> Result<(), Box<dyn std::error::Error>> {
188188
let outcome = owner
189189
.call(voting_contract.id(), "vote")
190190
.args_json(json!({
191-
"choice": "yes",
191+
"vote": "yes",
192192
"staking_pool_id": staking_pool_contract.id()
193193
}))
194194
.gas(Gas::from_tgas(200))
@@ -250,7 +250,7 @@ async fn test_vote_expiration() -> Result<(), Box<dyn std::error::Error>> {
250250
let outcome = owner
251251
.call(voting_contract.id(), "vote")
252252
.args_json(json!({
253-
"choice": "yes",
253+
"vote": "yes",
254254
"staking_pool_id": staking_pool_contract.id()
255255
}))
256256
.gas(Gas::from_tgas(200))
@@ -299,7 +299,7 @@ async fn test_vote_no_after_vote_yes() -> Result<(), Box<dyn std::error::Error>>
299299
let outcome = owner
300300
.call(voting_contract.id(), "vote")
301301
.args_json(json!({
302-
"choice": "yes",
302+
"vote": "yes",
303303
"staking_pool_id": staking_pool_contracts[0].id()
304304
}))
305305
.gas(Gas::from_tgas(200))
@@ -318,7 +318,7 @@ async fn test_vote_no_after_vote_yes() -> Result<(), Box<dyn std::error::Error>>
318318
let outcome = owner
319319
.call(voting_contract.id(), "vote")
320320
.args_json(json!({
321-
"choice": "no",
321+
"vote": "no",
322322
"staking_pool_id": staking_pool_contracts[0].id()
323323
}))
324324
.gas(Gas::from_tgas(200))
@@ -370,7 +370,7 @@ async fn test_vote_yes_after_vote_no() -> Result<(), Box<dyn std::error::Error>>
370370
let outcome = owner
371371
.call(voting_contract.id(), "vote")
372372
.args_json(json!({
373-
"choice": "no",
373+
"vote": "no",
374374
"staking_pool_id": staking_pool_contracts[0].id()
375375
}))
376376
.gas(Gas::from_tgas(200))
@@ -389,7 +389,7 @@ async fn test_vote_yes_after_vote_no() -> Result<(), Box<dyn std::error::Error>>
389389
let outcome = owner
390390
.call(voting_contract.id(), "vote")
391391
.args_json(json!({
392-
"choice": "yes",
392+
"vote": "yes",
393393
"staking_pool_id": staking_pool_contracts[0].id()
394394
}))
395395
.gas(Gas::from_tgas(200))
@@ -441,7 +441,7 @@ async fn test_unstake_after_voting() -> Result<(), Box<dyn std::error::Error>> {
441441
let outcome = owner
442442
.call(voting_contract.id(), "vote")
443443
.args_json(json!({
444-
"choice": "yes",
444+
"vote": "yes",
445445
"staking_pool_id": staking_pool_contracts[0].id()
446446
}))
447447
.gas(Gas::from_tgas(200))
@@ -478,7 +478,7 @@ async fn test_unstake_after_voting() -> Result<(), Box<dyn std::error::Error>> {
478478
let outcome = owner
479479
.call(voting_contract.id(), "vote")
480480
.args_json(json!({
481-
"choice": "yes",
481+
"vote": "yes",
482482
"staking_pool_id": staking_pool_contracts[1].id()
483483
}))
484484
.gas(Gas::from_tgas(200))

0 commit comments

Comments
 (0)