Skip to content

Commit

Permalink
Voting & Passive actions of Housing_Council
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Oct 11, 2023
1 parent 7c39d5f commit 1136c9e
Show file tree
Hide file tree
Showing 2 changed files with 192 additions and 3 deletions.
90 changes: 90 additions & 0 deletions pallets/council/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,94 @@ impl<T: Config> Pallet<T> {

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::<T, Instance1>::members().contains(&caller),
Error::<T>::NotACouncilMember
);
// Check that the proposal exists
ensure!(
SellerProposal::<T>::contains_key(&seller_account),
Error::<T>::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::<T, Instance1>::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::<T, Instance1>::members().contains(&caller),
Error::<T>::NotACouncilMember
);
// Check that the proposal exists
ensure!(
SellerProposal::<T>::contains_key(&seller_account),
Error::<T>::ProposalDoesNotExist
);
let proposal_all = Self::get_submitted_proposal(seller_account.clone()).unwrap();
let proposal_hash = proposal_all.proposal_hash;
let proposal = Coll::Pallet::<T,Instance1>::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::<T,Instance1>::close(
origin,
proposal_hash,
index,
proposal_weight,
proposal_len as u32,
).ok();

SellerProposal::<T>::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<T>) -> Weight{
let max_block_weight = Weight::from_parts(1000_u64,0);
if (now % <T as Config>::CheckPeriod::get()).is_zero(){
let proposal_iter = SellerProposal::<T>::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::<T>::proposal_rejection
{
account: proposal_all.0
};

let council_member = Coll::Pallet::<T,Instance1>::members()[0].clone();
proposal.dispatch_bypass_filter(frame_system::RawOrigin::Signed(council_member).into()).ok();
}
}

}
max_block_weight
}
}
105 changes: 102 additions & 3 deletions pallets/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>, T::AccountId),
/// Request for new role Rejected
ProposalRejected(BlockNumberFor<T>, T::AccountId),
/// A proposal has been added by a Background Council member
HousingCouncilAddedProposal{for_who: T::AccountId, proposal_index: u32, when: BlockNumberFor<T>},
/// A proposal has been closed by a Background Council member
HousingCouncilSessionClosed{who: T::AccountId, proposal_index: u32, when: BlockNumberFor<T>},
/// A member of the Background Council has voted
HousingCouncilVoted{who: T::AccountId, proposal_index: u32, when: BlockNumberFor<T>},
}


Expand All @@ -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,
}


Expand Down Expand Up @@ -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::<T>::owner(collection_id,item_id);
let owner = Nft::Pallet::<T>::owner(collection_id,item_id).unwrap();
//Change status
Self::status(owner.unwrap()).ok();
Self::status(owner.clone()).ok();
let now = <frame_system::Pallet<T>>::block_number();
Self::deposit_event(Event::ProposalApproved(now, owner));

Ok(())
}
Expand All @@ -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<T>,seller:T::AccountId,approve:bool) -> DispatchResultWithPostInfo {
let caller = ensure_signed(origin)?;
ensure!(
Coll::Pallet::<T, Instance1>::members().contains(&caller),
Error::<T>::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 = <frame_system::Pallet<T>>::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<T>,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 = <frame_system::Pallet<T>>::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::<T>::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<T>, account: T::AccountId) -> DispatchResultWithPostInfo {
let _sender = ensure_signed(origin.clone())?;

let proposal = Self::get_submitted_proposal(&account);
ensure!(proposal.is_some(), Error::<T>::ProposalDoesNotExist);

SellerProposal::<T>::remove(&account);

let now = <frame_system::Pallet<T>>::block_number();
Self::deposit_event(Event::ProposalRejected(now, account));


Ok(().into())
}


}
}

0 comments on commit 1136c9e

Please sign in to comment.