From 1136c9e08d4fcea3265713b32ea44091df511de3 Mon Sep 17 00:00:00 2001 From: ndkazu Date: Wed, 11 Oct 2023 16:40:00 +0900 Subject: [PATCH] Voting & Passive actions of Housing_Council --- pallets/council/src/functions.rs | 90 ++++++++++++++++++++++++++ pallets/council/src/lib.rs | 105 ++++++++++++++++++++++++++++++- 2 files changed, 192 insertions(+), 3 deletions(-) diff --git a/pallets/council/src/functions.rs b/pallets/council/src/functions.rs index 1b7dfef6..b373cb2a 100644 --- a/pallets/council/src/functions.rs +++ b/pallets/council/src/functions.rs @@ -78,4 +78,94 @@ impl Pallet { Ok(().into()) } + + pub fn vote_action(caller: T::AccountId,seller_account: T::AccountId,approve:bool) -> DispatchResultWithPostInfo{ + + // Check that the caller is a backgroundcouncil member + ensure!( + Coll::Pallet::::members().contains(&caller), + Error::::NotACouncilMember + ); + // Check that the proposal exists + ensure!( + SellerProposal::::contains_key(&seller_account), + Error::::ProposalDoesNotExist + ); + let proposal_all = Self::get_submitted_proposal(seller_account.clone()).unwrap(); + let proposal_hash = proposal_all.proposal_hash; + let proposal_index = proposal_all.proposal_index; + let origin = Self::get_origin(caller.clone()); + // Execute the council vote + Coll::Pallet::::vote( + origin, + proposal_hash, + proposal_index, + approve, + ).ok(); + + Ok(().into()) + } + + + pub fn closing_vote(caller: T::AccountId,seller_account: T::AccountId) -> DispatchResultWithPostInfo{ + + // Check that the caller is a backgroundcouncil member + ensure!( + Coll::Pallet::::members().contains(&caller), + Error::::NotACouncilMember + ); + // Check that the proposal exists + ensure!( + SellerProposal::::contains_key(&seller_account), + Error::::ProposalDoesNotExist + ); + let proposal_all = Self::get_submitted_proposal(seller_account.clone()).unwrap(); + let proposal_hash = proposal_all.proposal_hash; + let proposal = Coll::Pallet::::proposal_of(proposal_hash.clone()).unwrap(); + let proposal_len = proposal.clone().encoded_size(); + let index = proposal_all.proposal_index; + let proposal_weight = proposal.get_dispatch_info().weight; + let origin = Self::get_origin(caller.clone()); + Coll::Pallet::::close( + origin, + proposal_hash, + index, + proposal_weight, + proposal_len as u32, + ).ok(); + + SellerProposal::::mutate(&seller_account,|val|{ + let mut proposal = val.clone().unwrap(); + proposal.session_closed = true; + *val = Some(proposal); + }); + + Ok(().into()) + + } + + pub fn begin_block(now: BlockNumberFor) -> Weight{ + let max_block_weight = Weight::from_parts(1000_u64,0); + if (now % ::CheckPeriod::get()).is_zero(){ + let proposal_iter = SellerProposal::::iter(); + for proposal_all in proposal_iter{ + let test = (proposal_all.1.session_closed,proposal_all.1.approved); + let prop = match test{ + (true,false) => 0, + _ => 1, + }; + if prop == 0 { + let proposal = Call::::proposal_rejection + { + account: proposal_all.0 + }; + + let council_member = Coll::Pallet::::members()[0].clone(); + proposal.dispatch_bypass_filter(frame_system::RawOrigin::Signed(council_member).into()).ok(); + } + } + + } + max_block_weight + } } \ No newline at end of file diff --git a/pallets/council/src/lib.rs b/pallets/council/src/lib.rs index 84a12cf5..ab88e650 100644 --- a/pallets/council/src/lib.rs +++ b/pallets/council/src/lib.rs @@ -76,6 +76,16 @@ pub mod pallet { /// The account who set the new value. who: T::AccountId, }, + /// Request for new role accepted + ProposalApproved(BlockNumberFor, T::AccountId), + /// Request for new role Rejected + ProposalRejected(BlockNumberFor, T::AccountId), + /// A proposal has been added by a Background Council member + HousingCouncilAddedProposal{for_who: T::AccountId, proposal_index: u32, when: BlockNumberFor}, + /// A proposal has been closed by a Background Council member + HousingCouncilSessionClosed{who: T::AccountId, proposal_index: u32, when: BlockNumberFor}, + /// A member of the Background Council has voted + HousingCouncilVoted{who: T::AccountId, proposal_index: u32, when: BlockNumberFor}, } @@ -88,7 +98,9 @@ pub mod pallet { /// No Pending Request from this Seller NoPendingRequest, /// This is not a Council Member - NotACouncilMember + NotACouncilMember, + /// This proposal does not exist + ProposalDoesNotExist, } @@ -160,9 +172,11 @@ pub mod pallet { let _caller = T::HousingCouncilOrigin::ensure_origin(origin.clone())?; let collection_id:T::NftCollectionId = collection.value().into(); //get owner - let owner = Nft::Pallet::::owner(collection_id,item_id); + let owner = Nft::Pallet::::owner(collection_id,item_id).unwrap(); //Change status - Self::status(owner.unwrap()).ok(); + Self::status(owner.clone()).ok(); + let now = >::block_number(); + Self::deposit_event(Event::ProposalApproved(now, owner)); Ok(()) } @@ -185,5 +199,90 @@ pub mod pallet { Ok(().into()) } + /// Housing council member vote for a proposal + /// The origin must be signed and member of the Background Council + /// - candidate : account requesting the role + /// - approve : value of the vote (true or false) + #[pallet::call_index(4)] + #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())] + pub fn housing_council_vote(origin:OriginFor,seller:T::AccountId,approve:bool) -> DispatchResultWithPostInfo { + let caller = ensure_signed(origin)?; + ensure!( + Coll::Pallet::::members().contains(&caller), + Error::::NotACouncilMember + ); + let proposal_all = Self::get_submitted_proposal(&seller).unwrap(); + let index = proposal_all.proposal_index; + let result = Self::vote_action(caller.clone(),seller,approve); + + + match result{ + Ok(_) => { + let now = >::block_number(); + // deposit event + Self::deposit_event(Event::HousingCouncilVoted{ + who: caller, + proposal_index: index, + when: now, + }); + }, + Err(e) => return Err(e), + } + + + Ok(().into()) + } + + /// Housing council member close the vote session for a proposal + /// The origin must be signed and member of the Background Council + /// - seller : account submitting the proposal + #[pallet::call_index(5)] + #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())] + pub fn housing_council_close(origin:OriginFor,seller:T::AccountId) -> DispatchResultWithPostInfo{ + let caller = ensure_signed(origin)?; + let mut proposal_all = Self::get_submitted_proposal(&seller).unwrap(); + let index = proposal_all.proposal_index; + let result = Self::closing_vote(caller.clone(),seller.clone()); + + + match result{ + Ok(_) => { + let now = >::block_number(); + + Self::deposit_event(Event::HousingCouncilSessionClosed{ + who: caller, + proposal_index: index, + when: now, + }); + }, + Err(e) => return Err(e), + } + proposal_all = Self::get_submitted_proposal(&seller).unwrap(); + if proposal_all.approved==true{ + SellerProposal::::remove(&seller); + } + + Ok(().into()) + } + + ///Creation Refusal function for Sellers and Servicers. Only for admin level. + #[pallet::call_index(6)] + #[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())] + pub fn proposal_rejection(origin: OriginFor, account: T::AccountId) -> DispatchResultWithPostInfo { + let _sender = ensure_signed(origin.clone())?; + + let proposal = Self::get_submitted_proposal(&account); + ensure!(proposal.is_some(), Error::::ProposalDoesNotExist); + + SellerProposal::::remove(&account); + + let now = >::block_number(); + Self::deposit_event(Event::ProposalRejected(now, account)); + + + Ok(().into()) + } + + } } \ No newline at end of file