Skip to content

Commit

Permalink
Corrected rewards_calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Jan 22, 2025
1 parent 67f1c19 commit db13ef3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 49 deletions.
29 changes: 15 additions & 14 deletions substrate/frame/opf/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*,
};
Expand Down Expand Up @@ -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::<T>(r, caller.clone())?;
ensure!(
WhiteListedProjectAccounts::<T>::contains_key(account.clone()) == true,
WhiteListedProjectAccounts::<T>::contains_key(account0.clone()) == true,
"Project_id not set up correctly."
);

Expand All @@ -86,45 +86,46 @@ mod benchmarks {

let value: BalanceOf<T> = 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::<T>(r)?;
let caller: T::AccountId = whitelisted_caller();
let account0: T::AccountId = account("project", r, SEED);
add_whitelisted_project::<T>(r, caller.clone())?;
ensure!(
WhiteListedProjectAccounts::<T>::get().len() as u32 == r,
WhiteListedProjectAccounts::<T>::contains_key(account0.clone()) == true,
"Project_id not set up correctly."
);

on_idle_full_block::<T>();
let when = T::BlockNumberProvider::current_block_number() + One::one();
let when = frame_system::Pallet::<T>::block_number() + One::one();
run_to_block::<T>(when);

ensure!(VotingRounds::<T>::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::<T>::get()[(r - 1) as usize].clone();
let value: BalanceOf<T> = T::NativeBalance::minimum_balance() * 100u32.into() * (r).into();
Opf::<T>::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() }>,
Expand Down
26 changes: 14 additions & 12 deletions substrate/frame/opf/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ impl<T: Config> Pallet<T> {
pub fn get_formatted_call(call: Call<T>) -> <T as Config>::RuntimeCall {
call.into()
}
pub fn conviction_amount(amount: BalanceOf<T>, conviction:Democracy::Conviction) -> Option<BalanceOf<T>>{

let conviction_amount: BalanceOf<T> = match conviction {
Democracy::Conviction::None => {
Percent::from_percent(10) * amount
}
_ => amount.saturating_mul(<u8 as From<Democracy::Conviction>>::from(conviction).into())
};
Some(conviction_amount)
}

pub fn create_proposal(
caller: T::AccountId,
Expand Down Expand Up @@ -72,9 +82,7 @@ impl<T: Config> Pallet<T> {
.ok_or(Error::<T>::NoProjectAvailable)?;
let ref_index = infos.index;

let conviction_fund = amount.saturating_add(
amount.saturating_mul(<u8 as From<Democracy::Conviction>>::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::<T>::get().saturating_sub(1);
Expand Down Expand Up @@ -105,10 +113,7 @@ impl<T: Config> Pallet<T> {
let old_vote = Votes::<T>::get(&project, &voter_id).ok_or(Error::<T>::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(
<u8 as From<Democracy::Conviction>>::from(old_conviction).into(),
));
let old_conviction_amount = Self::conviction_amount(old_amount, old_conviction).ok_or("Invalid conviction")?;
ProjectFunds::<T>::mutate(&project, |val| {
let mut val0 = val.clone().into_inner();
if is_fund {
Expand Down Expand Up @@ -210,9 +215,7 @@ impl<T: Config> Pallet<T> {
let conviction = infos.conviction;
let is_fund = infos.is_fund;

let conviction_fund = amount.saturating_add(
amount.saturating_mul(<u8 as From<Democracy::Conviction>>::from(conviction).into()),
);
let conviction_fund = Self::conviction_amount(amount, conviction).ok_or("Invalid conviction")?;

// Update Round infos
let round_number = NextVotingRoundNumber::<T>::get().saturating_sub(1);
Expand Down Expand Up @@ -264,10 +267,9 @@ impl<T: Config> Pallet<T> {
let round = VotingRounds::<T>::get(round_number).ok_or(Error::<T>::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
Expand Down
46 changes: 23 additions & 23 deletions substrate/frame/opf/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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(<Test as Config>::TemporaryRewards::get());

let reward_101 = WhiteListedProjectAccounts::<Test>::get(101).unwrap().amount;
let reward_102 = WhiteListedProjectAccounts::<Test>::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::<Test>::contains_key(101), false);
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -293,7 +293,7 @@ fn vote_removal_works() {

// ProjectFund is correctly updated
let project_fund_before = ProjectFunds::<Test>::get(101);
assert_eq!(project_fund_before[0], 12000);
assert_eq!(project_fund_before[0], 6000);

// Voter's funds are locked
let locked_balance0 =
Expand All @@ -320,7 +320,7 @@ fn vote_removal_works() {

// ProjectFund is correctly updated
let project_fund_after = ProjectFunds::<Test>::get(101);
assert_eq!(project_fund_after[0], 10000);
assert_eq!(project_fund_after[0], 5000);
})
}

Expand All @@ -330,7 +330,7 @@ fn vote_overwrite_works() {
let batch = project_list();
let now = <Test as Config>::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(),
Expand All @@ -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::<Test>::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(),
Expand All @@ -362,7 +362,7 @@ fn vote_overwrite_works() {

// 10000 is allocated to project 103
funds = ProjectFunds::<Test>::get(103);
assert_eq!(funds[0], 10000);
assert_eq!(funds[0], 5000);

// Voter's funds are locked
let mut locked_balance0 = <<Test as Config>::NativeBalance as fungible::hold::Inspect<
Expand All @@ -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,
Expand All @@ -382,7 +382,7 @@ fn vote_overwrite_works() {

// Allocated amount to project 103 is now 13500
funds = ProjectFunds::<Test>::get(103);
assert_eq!(funds[0], 13500);
assert_eq!(funds[0], 9000);

// Storage was correctly updated
let vote_info = Votes::<Test>::get(103, BOB).unwrap();
Expand Down Expand Up @@ -484,9 +484,9 @@ fn spends_creation_works_but_not_executed_after_claim_period() {
let batch = project_list();
let voting_period = <Test as Config>::VotingPeriod::get();
let mut now = <Test as Config>::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));
Expand Down

0 comments on commit db13ef3

Please sign in to comment.