From db13ef3c7d4dce25b090202cee307d97c57cf52c Mon Sep 17 00:00:00 2001 From: ndkazu Date: Wed, 22 Jan 2025 22:36:41 +0900 Subject: [PATCH] Corrected rewards_calculation --- substrate/frame/opf/src/benchmarking.rs | 29 ++++++++-------- substrate/frame/opf/src/functions.rs | 26 +++++++------- substrate/frame/opf/src/tests.rs | 46 ++++++++++++------------- 3 files changed, 52 insertions(+), 49 deletions(-) diff --git a/substrate/frame/opf/src/benchmarking.rs b/substrate/frame/opf/src/benchmarking.rs index 90f939eed9fd..af7bbb627f05 100644 --- a/substrate/frame/opf/src/benchmarking.rs +++ b/substrate/frame/opf/src/benchmarking.rs @@ -22,7 +22,7 @@ use super::*; use crate::{Democracy::Conviction, Pallet as Opf}; //use pallet_distribution as Distribution; -use frame_benchmarking::{ +pub use frame_benchmarking::{ v1::{account, BenchmarkError}, v2::*, }; @@ -68,10 +68,10 @@ mod benchmarks { #[benchmark] fn vote(r: Linear<1, 1000>) -> Result<(), BenchmarkError> { let caller: T::AccountId = whitelisted_caller(); - let account: T::AccountId = account("project", r, SEED); + let account0: T::AccountId = account("project", r, SEED); add_whitelisted_project::(r, caller.clone())?; ensure!( - WhiteListedProjectAccounts::::contains_key(account.clone()) == true, + WhiteListedProjectAccounts::::contains_key(account0.clone()) == true, "Project_id not set up correctly." ); @@ -86,45 +86,46 @@ mod benchmarks { let value: BalanceOf = T::NativeBalance::minimum_balance() * 10u32.into() * (r).into(); #[extrinsic_call] - _(RawOrigin::Signed(caller.clone()), account, value, true, Conviction::Locked1x); + _(RawOrigin::Signed(caller.clone()), account0, value, true, Conviction::Locked1x); Ok(()) } - /*#[benchmark] + #[benchmark] fn remove_vote( - r: Linear<1, { T::MaxWhitelistedProjects::get() }>, + r: Linear<1, 1000>, ) -> Result<(), BenchmarkError> { - add_whitelisted_project::(r)?; + let caller: T::AccountId = whitelisted_caller(); + let account0: T::AccountId = account("project", r, SEED); + add_whitelisted_project::(r, caller.clone())?; ensure!( - WhiteListedProjectAccounts::::get().len() as u32 == r, + WhiteListedProjectAccounts::::contains_key(account0.clone()) == true, "Project_id not set up correctly." ); on_idle_full_block::(); - let when = T::BlockNumberProvider::current_block_number() + One::one(); + let when = frame_system::Pallet::::block_number() + One::one(); run_to_block::(when); ensure!(VotingRounds::::get(0).is_some(), "Round not created!"); - let caller_balance = T::NativeBalance::minimum_balance() * 10000u32.into(); + let caller_balance = T::NativeBalance::minimum_balance() * 100000000u32.into(); let caller: T::AccountId = whitelisted_caller(); let _ = T::NativeBalance::mint_into(&caller, caller_balance); - let account = WhiteListedProjectAccounts::::get()[(r - 1) as usize].clone(); let value: BalanceOf = T::NativeBalance::minimum_balance() * 100u32.into() * (r).into(); Opf::::vote( RawOrigin::Signed(caller.clone()).into(), - account.clone(), + account0.clone(), value, true, Conviction::Locked1x, )?; #[extrinsic_call] - _(RawOrigin::Signed(caller.clone()), account); + _(RawOrigin::Signed(caller.clone()), account0); Ok(()) } - +/* #[benchmark] fn unlock_funds( r: Linear<1, { T::MaxWhitelistedProjects::get() }>, diff --git a/substrate/frame/opf/src/functions.rs b/substrate/frame/opf/src/functions.rs index 0be0bf0ff440..a26bcc7dda38 100644 --- a/substrate/frame/opf/src/functions.rs +++ b/substrate/frame/opf/src/functions.rs @@ -21,6 +21,16 @@ impl Pallet { pub fn get_formatted_call(call: Call) -> ::RuntimeCall { call.into() } + pub fn conviction_amount(amount: BalanceOf, conviction:Democracy::Conviction) -> Option>{ + + let conviction_amount: BalanceOf = match conviction { + Democracy::Conviction::None => { + Percent::from_percent(10) * amount + } + _ => amount.saturating_mul(>::from(conviction).into()) + }; + Some(conviction_amount) + } pub fn create_proposal( caller: T::AccountId, @@ -72,9 +82,7 @@ impl Pallet { .ok_or(Error::::NoProjectAvailable)?; let ref_index = infos.index; - let conviction_fund = amount.saturating_add( - amount.saturating_mul(>::from(conviction).into()), - ); + let conviction_fund = Self::conviction_amount(amount, conviction).ok_or("Invalid conviction")?; // Create vote infos and store/adjust them let round_number = NextVotingRoundNumber::::get().saturating_sub(1); @@ -105,10 +113,7 @@ impl Pallet { let old_vote = Votes::::get(&project, &voter_id).ok_or(Error::::NoVoteData)?; let old_amount = old_vote.amount; let old_conviction = old_vote.conviction; - let old_conviction_amount = - old_amount.saturating_add(old_amount.saturating_mul( - >::from(old_conviction).into(), - )); + let old_conviction_amount = Self::conviction_amount(old_amount, old_conviction).ok_or("Invalid conviction")?; ProjectFunds::::mutate(&project, |val| { let mut val0 = val.clone().into_inner(); if is_fund { @@ -210,9 +215,7 @@ impl Pallet { let conviction = infos.conviction; let is_fund = infos.is_fund; - let conviction_fund = amount.saturating_add( - amount.saturating_mul(>::from(conviction).into()), - ); + let conviction_fund = Self::conviction_amount(amount, conviction).ok_or("Invalid conviction")?; // Update Round infos let round_number = NextVotingRoundNumber::::get().saturating_sub(1); @@ -264,10 +267,9 @@ impl Pallet { let round = VotingRounds::::get(round_number).ok_or(Error::::NoRoundFound)?; if projects.clone().len() > 0 as usize { let total_positive_votes_amount = round.total_positive_votes_amount; - let total_negative_votes_amount = round.total_negative_votes_amount; let when = T::BlockNumberProvider::current_block_number(); let total_votes_amount = - total_positive_votes_amount.saturating_sub(total_negative_votes_amount); + total_positive_votes_amount; // for each project, calculate the percentage of votes, the amount to be distributed, // and then populate the storage Projects diff --git a/substrate/frame/opf/src/tests.rs b/substrate/frame/opf/src/tests.rs index 36a29118885c..5ad74d4ef347 100644 --- a/substrate/frame/opf/src/tests.rs +++ b/substrate/frame/opf/src/tests.rs @@ -184,7 +184,7 @@ fn rewards_calculation_works() { assert_ok!(Opf::register_projects_batch(RuntimeOrigin::signed(EVE), batch)); // Bob nominate project_101 with an amount of 1000*BSX with a conviction x2 => equivalent to - // 3000*BSX locked + // 2000*BSX locked assert_ok!(Opf::vote( RawOrigin::Signed(BOB).into(), 101, @@ -194,7 +194,7 @@ fn rewards_calculation_works() { )); // Alice nominate project_101 with an amount of 5000*BSX with conviction 1x => equivalent to - // 10000*BSX locked + // 5000*BSX locked assert_ok!(Opf::vote( RawOrigin::Signed(ALICE).into(), 101, @@ -204,7 +204,7 @@ fn rewards_calculation_works() { )); // DAVE vote against project_102 with an amount of 3000*BSX with conviction 1x => equivalent - // to 6000*BSX locked + // to 3000*BSX locked assert_ok!(Opf::vote( RawOrigin::Signed(DAVE).into(), 102, @@ -213,7 +213,7 @@ fn rewards_calculation_works() { pallet_democracy::Conviction::Locked1x )); // Eve nominate project_102 with an amount of 5000*BSX with conviction 1x => equivalent to - // 10000*BSX locked + // 5000*BSX locked assert_ok!(Opf::vote( RawOrigin::Signed(EVE).into(), 102, @@ -235,15 +235,15 @@ fn rewards_calculation_works() { RuntimeEvent::Democracy(pallet_democracy::Event::Passed { ref_index: 0 }), ]); - // The total equivalent amount voted is 17000 = 23000 - 6000 - // Project 101: 13000 -> ~76.5%; Project 102: 4000 -> ~23.5% - // Distributed to project 101 -> 76%*100_000; Distributed to project 102 -> 23%*100_000 + // The total equivalent positive amount voted is 12000 + // Project 101: 7000 -> ~58.3%; Project 102: 2000 -> ~16.6% + // Distributed to project 101 -> 56%*100_000; Distributed to project 102 -> 16%*100_000 //Opf::calculate_rewards(::TemporaryRewards::get()); let reward_101 = WhiteListedProjectAccounts::::get(101).unwrap().amount; let reward_102 = WhiteListedProjectAccounts::::get(102).unwrap().amount; - assert_eq!(reward_101, 76000); - assert_eq!(reward_102, 23000); + assert_eq!(reward_101, 58000); + assert_eq!(reward_102, 16000); // Proposal Enactment did not happened yet assert_eq!(Spends::::contains_key(101), false); @@ -272,7 +272,7 @@ fn vote_removal_works() { assert_ok!(Opf::register_projects_batch(RuntimeOrigin::signed(EVE), batch)); // Bob nominate project_102 with an amount of 1000 & conviction of 1 equivalent to - // 2000 + // 1000 assert_ok!(Opf::vote( RawOrigin::Signed(BOB).into(), 101, @@ -282,7 +282,7 @@ fn vote_removal_works() { )); // Eve nominate project_101 with an amount of 5000 & conviction 1x => equivalent to - // 10000 + // 5000 assert_ok!(Opf::vote( RawOrigin::Signed(EVE).into(), 101, @@ -293,7 +293,7 @@ fn vote_removal_works() { // ProjectFund is correctly updated let project_fund_before = ProjectFunds::::get(101); - assert_eq!(project_fund_before[0], 12000); + assert_eq!(project_fund_before[0], 6000); // Voter's funds are locked let locked_balance0 = @@ -320,7 +320,7 @@ fn vote_removal_works() { // ProjectFund is correctly updated let project_fund_after = ProjectFunds::::get(101); - assert_eq!(project_fund_after[0], 10000); + assert_eq!(project_fund_after[0], 5000); }) } @@ -330,7 +330,7 @@ fn vote_overwrite_works() { let batch = project_list(); let now = ::BlockNumberProvider::current_block_number(); assert_ok!(Opf::register_projects_batch(RuntimeOrigin::signed(EVE), batch)); - // Bob nominate project_101 with an amount of 1000 with a conviction of 2 => amount+amount*2 + // Bob nominate project_101 with an amount of 1000 with a conviction of 2 => amount*2 // is the amount allocated to the project assert_ok!(Opf::vote( RawOrigin::Signed(BOB).into(), @@ -346,11 +346,11 @@ fn vote_overwrite_works() { project_id: 101, })]); - // 3000 is allocated to project 101 + // 2000 is allocated to project 101 let mut funds = ProjectFunds::::get(101); - assert_eq!(funds[0], 3000); + assert_eq!(funds[0], 2000); - // Bob nominate project_103 with an amount of 5000 with a conviction of 1 => amount+amount*1 + // Bob nominate project_103 with an amount of 5000 with a conviction of 1 => amount // is the amount allocated to the project assert_ok!(Opf::vote( RawOrigin::Signed(BOB).into(), @@ -362,7 +362,7 @@ fn vote_overwrite_works() { // 10000 is allocated to project 103 funds = ProjectFunds::::get(103); - assert_eq!(funds[0], 10000); + assert_eq!(funds[0], 5000); // Voter's funds are locked let mut locked_balance0 = <::NativeBalance as fungible::hold::Inspect< @@ -371,7 +371,7 @@ fn vote_overwrite_works() { assert!(locked_balance0 > 0); assert_eq!(locked_balance0, 6000); - // Bob changes amount in project_103 to 4500 + // Bob changes amount in project_103 to 4500 with conviction 2=> 9000 assert_ok!(Opf::vote( RawOrigin::Signed(BOB).into(), 103, @@ -382,7 +382,7 @@ fn vote_overwrite_works() { // Allocated amount to project 103 is now 13500 funds = ProjectFunds::::get(103); - assert_eq!(funds[0], 13500); + assert_eq!(funds[0], 9000); // Storage was correctly updated let vote_info = Votes::::get(103, BOB).unwrap(); @@ -484,9 +484,9 @@ fn spends_creation_works_but_not_executed_after_claim_period() { let batch = project_list(); let voting_period = ::VotingPeriod::get(); let mut now = ::BlockNumberProvider::current_block_number(); - let amount1 = 400 * BSX; - let amount2 = 320 * BSX; - let amount3 = 280 * BSX; + let amount1 = 400 ; + let amount2 = 320 ; + let amount3 = 280 ; //round_end_block let round_end = now.saturating_add(voting_period); assert_ok!(Opf::register_projects_batch(RuntimeOrigin::signed(EVE), batch));