Skip to content

Commit

Permalink
project-tips content
Browse files Browse the repository at this point in the history
  • Loading branch information
amiyatulu committed Apr 9, 2024
1 parent beb2a4e commit da34371
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 23 deletions.
4 changes: 3 additions & 1 deletion pallets/project-tips/src/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ impl<T: Config> Project<T> {
pub fn new(
project_id: ProjectId,
department_id: DepartmentId,
content: Content,
tipping_name: TippingName,
funding_needed: BalanceOf<T>,
project_leader: T::AccountId,
Expand All @@ -12,6 +13,7 @@ impl<T: Config> Project<T> {
created: new_who_and_when::<T>(project_leader.clone()),
project_id,
department_id,
content,
tipping_name,
funding_needed,
project_leader,
Expand All @@ -28,7 +30,7 @@ impl<T: Config> Pallet<T> {
project_id: ProjectId,
user: T::AccountId,
) -> DispatchResult {
let project_option: Option<Project<T>> = Projects::get(project_id);
let project_option: Option<Project<T>> = <Projects<T>>::get(project_id);
match project_option {
Some(project) => {
let project_leader = project.project_leader;
Expand Down
42 changes: 30 additions & 12 deletions pallets/project-tips/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,46 +171,64 @@ pub mod pallet {
pub fn create_project(
origin: OriginFor<T>,
department_id: DepartmentId,
content: Content,
tipping_name: TippingName,
funding_needed: BalanceOf<T>,
) -> DispatchResult {
let who = ensure_signed(origin)?;

let new_project_id = Self::next_project_id();
let tipping_value = Self::value_of_tipping_name(tipping_name);
let max_tipping_value = tipping_value.max_tipping_value;
let stake_required = tipping_value.stake_required;
let new_project_id = Self::next_project_id();
ensure!(
funding_needed <= max_tipping_value,
Error::<T>::FundingMoreThanTippingValue
);
let new_project: Project<T> = Project::new(
new_project_id,
department_id,
content,
tipping_name,
funding_needed,
who.clone(),
);
ensure!(funding_needed <= max_tipping_value, Error::<T>::FundingMoreThanTippingValue);
// Check user has done kyc
let _ = <T as pallet::Config>::Currency::withdraw(
&who,
stake_required,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
)?;

Projects::insert(new_project_id, new_project);
NextProjectId::<T>::mutate(|n| {
*n += 1;
});

Self::deposit_event(Event::ProjectCreated { account: who, project_id: new_project_id });

Ok(())
}



// Check update and discussion time over, only project creator can apply staking period
#[pallet::call_index(1)]
#[pallet::weight(0)]
pub fn apply_staking_period(origin: OriginFor<T>, project_id: ProjectId) -> DispatchResult {
pub fn apply_staking(origin: OriginFor<T>, project_id: ProjectId) -> DispatchResult {
let who = ensure_signed(origin)?;

Self::ensure_user_is_project_creator_and_project_exists(project_id, who)?;
Self::ensure_user_is_project_creator_and_project_exists(project_id, who.clone())?;
Self::ensure_staking_period_set_once_project_id(project_id)?;
match <Projects<T>>::get(project_id) {
Some(project) => {
let tipping_name = project.tipping_name;
let tipping_value = Self::value_of_tipping_name(tipping_name);
let stake_required = tipping_value.stake_required;

let _ = <T as pallet::Config>::Currency::withdraw(
&who,
stake_required,
WithdrawReasons::TRANSFER,
ExistenceRequirement::AllowDeath,
)?;
},

None => Err(Error::<T>::ProjectDontExists)?,
}

let now = <frame_system::Pallet<T>>::block_number();

Expand Down
70 changes: 60 additions & 10 deletions pallets/project-tips/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use crate::types::TippingName;
use crate::{mock::*, Error, Event};
use frame_support::{assert_noop, assert_ok};
use pallet_support::Content;
use schelling_game_shared::types::Period;
use sortition_sum_game::types::SumTreeName;

#[test]
fn check_create_project_function() {
fn check_balance_on_staking() {
new_test_ext().execute_with(|| {
// Go past genesis block so events get deposited
System::set_block_number(1);
Expand All @@ -14,17 +15,53 @@ fn check_create_project_function() {
let max_tipping_value = tipping_value.max_tipping_value;
let stake_required = tipping_value.stake_required;
let funding_needed = max_tipping_value - 100;
let balance = Balances::free_balance(1);
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProjectTips::create_project(
RuntimeOrigin::signed(1),
2,
content.clone(),
tipping_name,
funding_needed
));

System::assert_last_event(Event::ProjectCreated { account: 1, project_id: 1 }.into());

let balance = Balances::free_balance(1);

assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1));

let after_balance = Balances::free_balance(1);

assert_eq!(after_balance, balance - stake_required);
});
}

#[test]
fn check_create_project_function() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
let tipping_name = TippingName::SmallTipper;
let tipping_value = ProjectTips::value_of_tipping_name(tipping_name);
let max_tipping_value = tipping_value.max_tipping_value;
let funding_needed = max_tipping_value - 100;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProjectTips::create_project(
RuntimeOrigin::signed(1),
2,
content.clone(),
tipping_name,
funding_needed
));



System::assert_last_event(Event::ProjectCreated { account: 1, project_id: 1 }.into());

Expand All @@ -35,7 +72,7 @@ fn check_create_project_function() {
let funding_needed = max_tipping_value + 100;

assert_noop!(
ProjectTips::create_project(RuntimeOrigin::signed(1), 2, tipping_name, funding_needed),
ProjectTips::create_project(RuntimeOrigin::signed(1), 2, content, tipping_name, funding_needed),
Error::<Test>::FundingMoreThanTippingValue
);
});
Expand All @@ -46,34 +83,40 @@ fn check_apply_staking_period_function() {
new_test_ext().execute_with(|| {
System::set_block_number(1);
assert_noop!(
ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 2),
ProjectTips::apply_staking(RuntimeOrigin::signed(1), 2),
Error::<Test>::ProjectDontExists
);

let tipping_name = TippingName::SmallTipper;
let tipping_value = ProjectTips::value_of_tipping_name(tipping_name);
let max_tipping_value = tipping_value.max_tipping_value;
let funding_needed = max_tipping_value - 100;
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProjectTips::create_project(
RuntimeOrigin::signed(1),
2,
content,
tipping_name,
funding_needed
));

assert_noop!(
ProjectTips::apply_staking_period(RuntimeOrigin::signed(3), 1),
ProjectTips::apply_staking(RuntimeOrigin::signed(3), 1),
Error::<Test>::ProjectCreatorDontMatch
);

assert_ok!(ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1));
assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1));

System::assert_last_event(
Event::StakinPeriodStarted { project_id: 1, block_number: 1 }.into(),
);
System::set_block_number(5);
assert_noop!(
ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1),
ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1),
Error::<Test>::ProjectIdStakingPeriodAlreadySet
);
});
Expand All @@ -88,20 +131,27 @@ fn schelling_game_test() {
let max_tipping_value = tipping_value.max_tipping_value;
let stake_required = tipping_value.stake_required;
let funding_needed = max_tipping_value - 100;
let balance = Balances::free_balance(1);
let content: Content = Content::IPFS(
"bafkreiaiq24be2iioasr6ftyaum3icmj7amtjkom2jeokov5k5ojwzhvqy"
.as_bytes()
.to_vec(),
);
assert_ok!(ProjectTips::create_project(
RuntimeOrigin::signed(1),
2,
content,
tipping_name,
funding_needed
));

let balance = Balances::free_balance(1);

assert_ok!(ProjectTips::apply_staking(RuntimeOrigin::signed(1), 1));

let after_balance = Balances::free_balance(1);

assert_eq!(after_balance, balance - stake_required);

assert_ok!(ProjectTips::apply_staking_period(RuntimeOrigin::signed(1), 1));

let phase_data = ProjectTips::get_phase_data();

let balance = Balances::free_balance(29);
Expand Down
1 change: 1 addition & 0 deletions pallets/project-tips/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ pub struct Project<T: Config> {
pub created: WhoAndWhenOf<T>,
pub project_id: ProjectId,
pub department_id: DepartmentId,
pub content: Content,
pub tipping_name: TippingName,
pub funding_needed: BalanceOf<T>,
pub project_leader: T::AccountId,
Expand Down

0 comments on commit da34371

Please sign in to comment.