Skip to content

Commit

Permalink
Fixing mistakes in hooks functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ndkazu committed Oct 12, 2023
1 parent 1136c9e commit 56ac17b
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 64 deletions.
2 changes: 1 addition & 1 deletion pallets/council/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<T: Config> Pallet<T> {
for proposal_hash in proposal_hashes{
let prop0 = Coll::Pallet::<T,Instance1>::proposal_of(proposal_hash.clone()).unwrap();
if proposal == prop0{
let mut proposal_all = ProposalOf::<T>::new(account.clone(), Some(Roles::Accounts::None),proposal_hash.clone());
let mut proposal_all = ProposalOf::<T>::new(account.clone(), Some(Roles::Accounts::NONE),proposal_hash.clone());
proposal_all.proposal_index = index;
proposal_all.proposal_hash = proposal_hash;
SellerProposal::<T>::insert(&account, proposal_all);
Expand Down
7 changes: 7 additions & 0 deletions pallets/council/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ pub mod pallet {
ProposalDoesNotExist,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
Self::begin_block(n)
}
}


#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down
39 changes: 23 additions & 16 deletions pallets/onboarding/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ impl<T: Config> Pallet<T> {
<T as DEM::Config>::Preimages::bound(call).unwrap()
}

pub fn add_proposal(who:T::AccountId,call: CallOf<T>) -> DispatchResult {

let value = <T as DEM::Config>::MinimumDeposit::get();
let proposal = Self::make_proposal(call);
DEM::Pallet::<T>::propose(RawOrigin::Signed(who).into(), proposal.clone(), value)?;
Ok(())
}

pub fn start_dem_referendum(proposal:BoundedCallOf<T> ,delay:BlockNumberFor<T>) -> DEM::ReferendumIndex{
let threshold = DEM::VoteThreshold::SimpleMajority;
let referendum_index =
Expand Down Expand Up @@ -222,7 +230,6 @@ impl<T: Config> Pallet<T> {
let coll_id = asset.0;
let item_id = asset.1;
let status = asset.2.status;
let index = asset.2.ref_index;
let items = Roles::Asset::<T>::iter();

//Start awaiting referendums
Expand All @@ -231,22 +238,17 @@ impl<T: Config> Pallet<T> {
//start Democracy referendum
//Send Proposal struct to voting pallet
//get the needed call and convert them to pallet_voting format
let out_call = Vcalls::<T>::get(coll_id, item_id).unwrap();
let call0 = Self::get_formatted_call(out_call.after_vote_status) ;
let proposal = Self::make_proposal(call0.into());
let delay = T::Delay::get();
let index=Self::start_dem_referendum(proposal,delay);
Houses::<T>::mutate_exists(coll_id,item_id,|val|{
let mut v0 = val.clone().unwrap();
v0.ref_index=index;
v0.status=Status::VOTING;
*val = Some(v0);
});
}
}

let owner_origin= RawOrigin::Signed(item.0);
Self::investor_referendum(owner_origin.into(),coll_id, item_id).ok();
let asset0 = Self::houses(coll_id,item_id).unwrap();
let index0 = asset0.ref_index;
//Event referendum started
Self::deposit_event(Event::ReferendumStarted{
index:index0
});

//Use index to get referendum infos
let infos = DEM::Pallet::<T>::referendum_info(index).unwrap();
let infos = DEM::Pallet::<T>::referendum_info(index0).unwrap();
let b = match infos {
DEM::ReferendumInfo::Finished { approved, end: _ } => {
(1, approved)
Expand All @@ -262,6 +264,11 @@ impl<T: Config> Pallet<T> {
Call::<T>::reject_edit { collection:coll, item_id, infos: asset.2.clone() }.into();
call2.dispatch_bypass_filter(frame_system::RawOrigin::Root.into()).ok();}
}

}
}



}

Expand Down
59 changes: 31 additions & 28 deletions pallets/onboarding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,20 +160,6 @@ pub mod pallet {
OptionQuery,
>;

#[pallet::storage]
#[pallet::getter(fn voting_calls)]
/// Stores Calls
pub(super) type Vcalls<T: Config> = StorageDoubleMap<
_,
Blake2_128Concat,
T::NftCollectionId,
Blake2_128Concat,
T::NftItemId,
VotingCalls<T>,
OptionQuery,
>;


// Test Genesis Configuration
#[derive(frame_support::DefaultNoBound)]
#[pallet::genesis_config]
Expand Down Expand Up @@ -248,6 +234,9 @@ pub struct GenesisConfig<T: Config> {
collection: T::NftCollectionId,
item: T::NftItemId,
},
ReferendumStarted{
index:u32
}
}

// Errors inform users that something went wrong.
Expand Down Expand Up @@ -283,6 +272,15 @@ pub struct GenesisConfig<T: Config> {
ReservedToInvestors,
/// Failed to unreserved fund in Housing fund
HousingFundUnreserveFundFailed,
///ReferendumFailed
FailedReferendum,
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberOf<T>) -> Weight {
Self::begin_block(n)
}
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
Expand Down Expand Up @@ -500,20 +498,6 @@ pub struct GenesisConfig<T: Config> {

let collection_id: T::NftCollectionId = collection.clone().value().into();

let house = Self::houses(collection_id, item_id).unwrap();

let _new_call = VotingCalls::<T>::new(collection_id, item_id).ok();


//Create Call for asset status change after Investor's vote
let call4 =
Call::<T>::change_status { collection, item_id, status: Status::ONBOARDED };
Vcalls::<T>::mutate(collection_id, item_id, |val| {
let mut v0 = val.clone().unwrap();
v0.after_vote_status = call4.clone().into();
*val = Some(v0);
});

Self::deposit_event(Event::ProposalCreated {
who: caller.clone(),
collection: collection_id,
Expand Down Expand Up @@ -605,6 +589,25 @@ pub struct GenesisConfig<T: Config> {
DEM::Pallet::<T>::vote(origin,index,config).ok();
Ok(())

}

#[pallet::call_index(8)]
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn investor_referendum(origin: OriginFor<T>,coll_id:T::NftCollectionId,item_id: T::NftItemId )-> DispatchResult {
let _who = ensure_signed(origin.clone())?;
let out_call = Call::<T>::change_status { collection:Self::collection_name(coll_id.into()), item_id, status: Status::ONBOARDED };
let call0 = Self::get_formatted_call(out_call) ;
let proposal = Self::make_proposal(call0.into());
let delay = T::Delay::get();
let index=Self::start_dem_referendum(proposal,delay);
Houses::<T>::mutate_exists(coll_id,item_id,|val|{
let mut v0 = val.clone().unwrap();
v0.ref_index=index;
v0.status=Status::VOTING;
*val = Some(v0);
});
Ok(())

}

}
Expand Down
15 changes: 0 additions & 15 deletions pallets/onboarding/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,18 +102,3 @@ pub struct VotingCalls<T: Config> {
pub(super) after_vote_status: Call<T>,
}

impl<T: Config> VotingCalls<T> {
pub fn new(collection: T::NftCollectionId, item: T::NftItemId) -> DispatchResult {
let nbr: u32 = 0;
let call:Call<T> = Call::<T>::do_something { something: nbr }.into();

let calls = VotingCalls::<T> {
reject_edit: call.clone(),
reject_destroy: call.clone(),
democracy_status: call.clone(),
after_vote_status: call,
};
Vcalls::<T>::insert(collection, item, calls);
Ok(())
}
}
1 change: 1 addition & 0 deletions pallets/roles/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ impl<T: Config> Pallet<T> {
proposal_len,
).ok();
let mut index:u32 = Coll::Pallet::<T,Instance2>::proposal_count();
debug_assert!(index>0);
index = index.saturating_sub(1);

//Update proposal index and hash
Expand Down
4 changes: 2 additions & 2 deletions pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,12 +428,12 @@ pub mod pallet {
.map_err(|_:Error<T>| <Error<T>>::InitializationError)?;
Self::deposit_event(Event::CreationRequestCreated(now, account.clone()));
},
Accounts::None => {Self::deposit_event(Event::NoCreationRequestCreated(now, account.clone()));}
Accounts::NONE => {Self::deposit_event(Event::NoCreationRequestCreated(now, account.clone()));}
}

let need_approval = !matches!(
account_type,
Accounts::INVESTOR | Accounts::TENANT | Accounts::REPRESENTATIVE | Accounts::None
Accounts::INVESTOR | Accounts::TENANT | Accounts::REPRESENTATIVE | Accounts::NONE
);
if need_approval {
Self::start_council_session(account.clone(),account_type).ok();
Expand Down
5 changes: 4 additions & 1 deletion pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ fn setting_roles(){
assert_eq!(RolesModule::get_pending_house_sellers().len(),1);
let account =RolesModule::get_pending_house_sellers()[0].account_id.clone();
assert_eq!(account,BOB);


//Check that collective referendum started
//assert_eq!(Collective::proposal_count(),1);

/*assert_eq!(RolesModule::get_pending_house_sellers().len(), 1);
/*
assert_ok!(RolesModule::council_vote(RuntimeOrigin::signed(council[0].clone()),BOB,true));
assert_ok!(RolesModule::council_vote(RuntimeOrigin::signed(council[1].clone()),BOB,true));
Expand Down
2 changes: 1 addition & 1 deletion pallets/roles/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ pub enum Accounts {
SERVICER,
NOTARY,
REPRESENTATIVE,
None,
NONE,
}

#[derive(Clone,Copy, Encode, Decode, Default, PartialEq, Eq, TypeInfo, MaxEncodedLen)]
Expand Down

0 comments on commit 56ac17b

Please sign in to comment.