diff --git a/src/events.rs b/src/events.rs index 26c16e1..4784a16 100644 --- a/src/events.rs +++ b/src/events.rs @@ -18,7 +18,7 @@ pub const EVENT_STANDARD_VERSION: &str = "1.0.0"; pub enum Event<'a> { Voted { validator_id: &'a AccountId, - choice: &'a Choice, + vote: &'a Vote, }, ProposalApproved { proposal: &'a String, diff --git a/src/lib.rs b/src/lib.rs index 68fd9f3..c35dc81 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,7 @@ type Timestamp = u64; #[near(serializers = [json])] #[serde(rename_all = "lowercase")] -pub enum Choice { +pub enum Vote { Yes, No, } @@ -86,16 +86,16 @@ impl Contract { } } - /// Method for validators to vote with choice `Yes` or `No`. + /// Method for validators to vote with `Yes` or `No`. /// The method is called by validator owners. - pub fn vote(&mut self, choice: Choice, staking_pool_id: AccountId) -> Promise { + pub fn vote(&mut self, vote: Vote, staking_pool_id: AccountId) -> Promise { ext_staking_pool::ext(staking_pool_id.clone()) .with_static_gas(GET_OWNER_ID_GAS) .get_owner_id() .then(Self::ext(env::current_account_id()).on_get_pool_owner_id( env::predecessor_account_id(), staking_pool_id, - choice, + vote, )) } @@ -105,7 +105,7 @@ impl Contract { &mut self, pool_owner_id: AccountId, staking_pool_id: AccountId, - choice: Choice, + vote: Vote, #[callback_result] pool_owner_id_result: Result, ) { if let Ok(actual_owner_id) = pool_owner_id_result { @@ -113,22 +113,22 @@ impl Contract { pool_owner_id == actual_owner_id, "Voting is only allowed for the staking pool owner" ); - self.internal_vote(choice, staking_pool_id); + self.internal_vote(vote, staking_pool_id); } else { env::panic_str("Failed to get the staking pool owner id"); } } /// Internal method for voting. - fn internal_vote(&mut self, choice: Choice, account_id: AccountId) { + fn internal_vote(&mut self, vote: Vote, account_id: AccountId) { self.ping(); let stake = validator_stake(&account_id); require!(stake > 0, format!("{} is not a validator", account_id)); - let account_stake = match choice { - Choice::Yes => stake, - Choice::No => 0, + let account_stake = match vote { + Vote::Yes => stake, + Vote::No => 0, }; let voted_stake = self.votes.remove(&account_id).unwrap_or_default(); @@ -147,7 +147,7 @@ impl Contract { // emit event Event::Voted { validator_id: &account_id, - choice: &choice, + vote: &vote, } .emit(); } @@ -302,20 +302,20 @@ mod tests { fn vote_with_account( contract: &mut Contract, - choice: Choice, + vote: Vote, staking_pool_id: &AccountId, account: &AccountId, ) { contract.on_get_pool_owner_id( account.clone(), staking_pool_id.clone(), - choice, + vote, Ok(pool_owner()), ); } - fn vote(contract: &mut Contract, choice: Choice, staking_pool_id: &AccountId) { - vote_with_account(contract, choice, staking_pool_id, &pool_owner()); + fn vote(contract: &mut Contract, vote: Vote, staking_pool_id: &AccountId) { + vote_with_account(contract, vote, staking_pool_id, &pool_owner()); } #[test] @@ -328,7 +328,7 @@ mod tests { ]); set_context_and_validators(&context, &validators); let mut contract = get_contract(); - vote(&mut contract, Choice::Yes, &validator(3)); + vote(&mut contract, Vote::Yes, &validator(3)); } #[test] @@ -341,7 +341,7 @@ mod tests { ]); set_context_and_validators(&context, &validators); let mut contract = get_contract(); - vote(&mut contract, Choice::No, &validator(3)); + vote(&mut contract, Vote::No, &validator(3)); } #[test] @@ -356,10 +356,10 @@ mod tests { set_context_and_validators(&context, &validators); let mut contract = get_contract(); // vote - vote(&mut contract, Choice::Yes, &validator_id); + vote(&mut contract, Vote::Yes, &validator_id); assert!(contract.get_result().is_some()); // vote again. should panic because voting has ended - vote(&mut contract, Choice::Yes, &validator_id); + vote(&mut contract, Vote::Yes, &validator_id); } #[test] @@ -375,7 +375,7 @@ mod tests { let mut contract = get_contract(); // vote with an account that is not the pool owner. // should panic because only the pool owner can vote. - vote_with_account(&mut contract, Choice::Yes, &validator_id, &accounts(0)); + vote_with_account(&mut contract, Vote::Yes, &validator_id, &accounts(0)); } #[test] @@ -387,7 +387,7 @@ mod tests { for i in 0..201 { // vote by each validator let voter = validator(i); - vote(&mut contract, Choice::Yes, &voter); + vote(&mut contract, Vote::Yes, &voter); // check total voted stake context.is_view(true); @@ -420,7 +420,7 @@ mod tests { // vote by each validator let context = get_context_with_epoch_height(&voting_contract_id(), i); set_context(&context); - vote(&mut contract, Choice::Yes, &validator(i)); + vote(&mut contract, Vote::Yes, &validator(i)); // check votes assert_eq!(contract.get_votes().len() as u64, i + 1); // check voting result @@ -443,7 +443,7 @@ mod tests { let context = get_context_with_epoch_height(&voting_contract_id(), 1); set_context_and_validators(&context, &validators); let mut contract = get_contract(); - vote(&mut contract, Choice::Yes, &validator(1)); + vote(&mut contract, Vote::Yes, &validator(1)); // ping at epoch 2 validators.insert(validator(1).to_string(), NearToken::from_yoctonear(50)); let context = get_context_with_epoch_height(&voting_contract_id(), 2); @@ -453,7 +453,7 @@ mod tests { } #[test] - fn test_change_vote_choice() { + fn test_change_vote() { let validators: HashMap = HashMap::from_iter(vec![ (validator(1).to_string(), NearToken::from_yoctonear(10)), (validator(2).to_string(), NearToken::from_yoctonear(10)), @@ -462,17 +462,17 @@ mod tests { set_context_and_validators(&context, &validators); let mut contract = get_contract(); // vote YES at epoch 1 - vote(&mut contract, Choice::Yes, &validator(1)); + vote(&mut contract, Vote::Yes, &validator(1)); assert_eq!(contract.get_votes().len(), 1); // vote NO at epoch 2 let context = get_context_with_epoch_height(&voting_contract_id(), 2); set_context_and_validators(&context, &validators); - vote(&mut contract, Choice::No, &validator(1)); + vote(&mut contract, Vote::No, &validator(1)); assert!(contract.get_votes().is_empty()); // vote YES at epoch 3 let context = get_context_with_epoch_height(&voting_contract_id(), 3); set_context_and_validators(&context, &validators); - vote(&mut contract, Choice::Yes, &validator(1)); + vote(&mut contract, Vote::Yes, &validator(1)); assert_eq!(contract.get_votes().len(), 1); } @@ -487,7 +487,7 @@ mod tests { set_context_and_validators(&context, &validators); let mut contract = get_contract(); // vote at epoch 1 - vote(&mut contract, Choice::Yes, &validator(1)); + vote(&mut contract, Vote::Yes, &validator(1)); assert_eq!((contract.get_total_voted_stake().0).0, 40); assert_eq!(contract.get_votes().len(), 1); // remove validator at epoch 2 @@ -534,7 +534,7 @@ mod tests { // vote after deadline set_context(context.block_timestamp(env::block_timestamp_ms() + 2000 * 1_000_000)); - vote(&mut contract, Choice::Yes, &validator(0)); + vote(&mut contract, Vote::Yes, &validator(0)); } #[test] @@ -545,7 +545,7 @@ mod tests { // vote at epoch 1 set_context(&context); - vote(&mut contract, Choice::Yes, &validator(0)); + vote(&mut contract, Vote::Yes, &validator(0)); // ping at epoch 2 after deadline set_context( diff --git a/tests/test_vote.rs b/tests/test_vote.rs index 7e0ee0f..b2e8b0d 100644 --- a/tests/test_vote.rs +++ b/tests/test_vote.rs @@ -22,7 +22,7 @@ async fn test_non_validator_cannot_vote() -> Result<(), Box Result<(), Box Result<(), Box Result<(), Box Result<(), Box> { let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contract.id() })) .gas(Gas::from_tgas(200)) @@ -188,7 +188,7 @@ async fn test_many_votes() -> Result<(), Box> { let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contract.id() })) .gas(Gas::from_tgas(200)) @@ -250,7 +250,7 @@ async fn test_vote_expiration() -> Result<(), Box> { let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contract.id() })) .gas(Gas::from_tgas(200)) @@ -299,7 +299,7 @@ async fn test_vote_no_after_vote_yes() -> Result<(), Box> let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contracts[0].id() })) .gas(Gas::from_tgas(200)) @@ -318,7 +318,7 @@ async fn test_vote_no_after_vote_yes() -> Result<(), Box> let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "no", + "vote": "no", "staking_pool_id": staking_pool_contracts[0].id() })) .gas(Gas::from_tgas(200)) @@ -370,7 +370,7 @@ async fn test_vote_yes_after_vote_no() -> Result<(), Box> let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "no", + "vote": "no", "staking_pool_id": staking_pool_contracts[0].id() })) .gas(Gas::from_tgas(200)) @@ -389,7 +389,7 @@ async fn test_vote_yes_after_vote_no() -> Result<(), Box> let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contracts[0].id() })) .gas(Gas::from_tgas(200)) @@ -441,7 +441,7 @@ async fn test_unstake_after_voting() -> Result<(), Box> { let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contracts[0].id() })) .gas(Gas::from_tgas(200)) @@ -478,7 +478,7 @@ async fn test_unstake_after_voting() -> Result<(), Box> { let outcome = owner .call(voting_contract.id(), "vote") .args_json(json!({ - "choice": "yes", + "vote": "yes", "staking_pool_id": staking_pool_contracts[1].id() })) .gas(Gas::from_tgas(200))