Skip to content

Commit

Permalink
201 representative role approval (#202)
Browse files Browse the repository at this point in the history
* Representative approval

* Check of the Representative waiting list

* Representative approval call
  • Loading branch information
ndkazu authored Nov 14, 2022
1 parent f352641 commit 2f286bc
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 33 deletions.
15 changes: 15 additions & 0 deletions pallets/asset_management/src/functions.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
//Helper functions that will be used in proposal's calls
//helper 1) get shares/owners from asset_id
pub use super::*;

pub use frame_support::pallet_prelude::*;
impl<T: Config> Pallet<T> {
pub fn approve_representative(caller: T::AccountId, who:T::AccountId) -> DispatchResult{
let mut representative = Roles::Pallet::<T>::get_pending_representatives(&who).unwrap();
representative.activated = true;
representative.assets_accounts.push(caller.clone());
Roles::RepresentativeLog::<T>::insert(&who,representative);
Roles::RepApprovalList::<T>::remove(&who);
Roles::AccountsRolesLog::<T>::insert(&who, Roles::Accounts::REPRESENTATIVE);

Ok(())
}

}
21 changes: 19 additions & 2 deletions pallets/asset_management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@
//Needed calls:
//Call 1) Create a Representative role

pub use pallet::*;
pub use pallet_roles as Roles;
pub use pallet_democracy as Dem;
pub use pallet_share_distributor as Share;
pub use pallet_nft as Nft;
pub use pallet::*;


mod functions;
//#[cfg(test)]
//mod mock;

Expand All @@ -25,6 +26,7 @@ pub use pallet::*;

#[frame_support::pallet]
pub mod pallet {
use super::*;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use frame_system::WeightInfo;
Expand Down Expand Up @@ -64,6 +66,8 @@ pub mod pallet {
pub enum Error<T> {
/// Error names should be descriptive.
NoneValue,
/// The account is not an Asset account
NotAnAssetAccount,
/// Errors should have helpful documentation associated with them.
StorageOverflow,
}
Expand Down Expand Up @@ -110,7 +114,20 @@ pub mod pallet {
}
}

/// approve a Representative role request
#[pallet::weight(10_000 + T::DbWeight::get().reads_writes(1,1).ref_time())]
pub fn representative_approval(origin: OriginFor<T>, account: T::AccountId,collection: T::NftCollectionId,item: T::NftItemId) -> DispatchResult {
let caller = ensure_signed(origin)?;
//Check that the caller is a stored virtual account
ensure!(caller == Share::Pallet::<T>::virtual_acc(collection,item).unwrap().virtual_account, Error::<T>::NotAnAssetAccount);
//Check that the account is in the representative waiting list
ensure!(!Roles::Pallet::<T>::get_pending_representatives(&account).is_none(),"problem");
//Approve role request
Self::approve_representative(caller,account).ok();

Ok(())
}


//Create Representative Role
}
}
9 changes: 2 additions & 7 deletions pallets/roles/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,8 @@ impl<T: Config> Pallet<T> {
ensure!(&account != id, Error::<T>::AlreadyWaiting);
}
}
let representatives = Self::get_pending_representatives();
if !representatives.is_empty() {
for representative in representatives.iter() {
let id = &representative.account_id;
ensure!(&account != id, Error::<T>::AlreadyWaiting);
}
}
ensure!(!RepApprovalList::<T>::contains_key(&account),Error::<T>::AlreadyWaiting);


Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn reps)]
///Registry of Sellers organized by AccountId
pub(super) type RepresentativeLog<T: Config> =
pub type RepresentativeLog<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, Representative<T>, OptionQuery>;

#[pallet::storage]
Expand Down Expand Up @@ -161,13 +161,13 @@ pub mod pallet {
#[pallet::storage]
#[pallet::getter(fn get_pending_representatives)]
///Approval waiting list for Representatives
pub(super) type RepApprovalList<T: Config> =
StorageValue<_, Vec<Representative<T>>, ValueQuery, InitRepApprovalList<T>>;
pub type RepApprovalList<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, Representative<T>, OptionQuery>;

#[pallet::storage]
#[pallet::getter(fn get_roles)]
///Registry of Roles by AccountId
pub(super) type AccountsRolesLog<T: Config> =
pub type AccountsRolesLog<T: Config> =
StorageMap<_, Twox64Concat, AccountIdOf<T>, Accounts, OptionQuery>;

#[pallet::type_value]
Expand Down
21 changes: 4 additions & 17 deletions pallets/roles/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,14 @@ fn test_struct_methods() {
assert_ok!(Representative::<Test>::new(Origin::signed(3)));
//checking struct in Representative waiting list
assert_eq!(
RoleModule::get_pending_representatives(),
vec![Representative {
RoleModule::get_pending_representatives(3).unwrap(),
Representative {
account_id: 3,
age: System::block_number(),
activated: false,
assets_accounts: vec![],
}]
);

// Notary
assert_ok!(Notary::<Test>::new(Origin::signed(5)));
// Test notary approval list
assert_eq!(
RoleModule::get_pending_notaries(),
vec![Notary {
account_id: 5,
age: System::block_number(),
activated: false,
verifier: 4
}]
);
}
)
});
}

Expand Down
7 changes: 4 additions & 3 deletions pallets/roles/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,15 @@ where
ensure!(!RepresentativeLog::<T>::contains_key(&caller), Error::<T>::NoneValue);

let rep = Representative {
account_id: caller,
account_id: caller.clone(),
age: now,
activated: false,
assets_accounts: Vec::new(),
};

RepApprovalList::<T>::mutate(|val| {
val.push(rep);
RepApprovalList::<T>::mutate(caller,|val| {
//val.push(rep);
*val=Some(rep);
});

Ok(())
Expand Down

0 comments on commit 2f286bc

Please sign in to comment.