Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Representative can have several assets #249

Merged
merged 5 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions pallets/asset_management/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@ use sp_runtime::traits::{StaticLookup, Zero};
impl<T: Config> Pallet<T> {
pub fn approve_representative(origin: OriginFor<T>, who: T::AccountId) -> DispatchResult {
let caller = ensure_signed(origin.clone())?;

let mut representative = Roles::Pallet::<T>::get_pending_representatives(&who).unwrap();
representative.activated = true;
representative.assets_accounts.clear();
representative.assets_accounts.push(caller);
Roles::RepApprovalList::<T>::remove(&who);
let who2 = T::Lookup::unlookup(who.clone());
//get Rep number
let mut index = Roles::Pallet::<T>::rep_num();
//Update Rep index
representative.index = index;

Roles::RepresentativeLog::<T>::insert(&who, representative);
Roles::RepApprovalList::<T>::remove(&who);
Roles::AccountsRolesLog::<T>::insert(&who, Roles::Accounts::REPRESENTATIVE);
let who2 = T::Lookup::unlookup(who.clone());
//Check if we're dealing with an already registered representative
let registered = Roles::RepresentativeLog::<T>::contains_key(&who);

if registered == false{
representative.activated = true;
representative.assets_accounts.clear();
representative.assets_accounts.push(caller);

//Update Rep index
representative.index = index;

Roles::RepresentativeLog::<T>::insert(&who, representative);
Roles::AccountsRolesLog::<T>::insert(&who, Roles::Accounts::REPRESENTATIVE);

} else{
//Add the new asset_account to the representative struct
representative.assets_accounts.push(caller);
Roles::RepresentativeLog::<T>::mutate(&who,|val|{
*val = Some(representative);
})
}


//Check that the Representative is not already a Registrar
//If a Representative is revoked from a given asset, and approved
Expand Down Expand Up @@ -49,9 +65,11 @@ impl<T: Config> Pallet<T> {
let fees = bals0.ident_bal;
Ident::Pallet::<T>::set_fee(origin2, index, fees).ok();

//Update Rep number
if registered==false{
//Update Rep number if not yet a registered Representative
index += 1;
Roles::RepNumber::<T>::put(index);
}
}

Ok(())
Expand Down
12 changes: 10 additions & 2 deletions pallets/asset_management/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ pub mod pallet {
AssetOutOfControl,
/// The candidate is not a tenant
NotATenant,
/// An asset is already linked to the provided tenant
/// An asset is already linked to the provided account
AlreadyLinkedWithAsset,
/// The tenant is not linked to the asset
TenantAssetNotLinked,
Expand Down Expand Up @@ -470,14 +470,22 @@ pub mod pallet {
item: T::NftItemId,
) -> DispatchResult {
let caller = ensure_signed(origin.clone())?;
let asset_account = Share::Pallet::<T>::virtual_acc(collection, item).unwrap().virtual_account;

//Check that the caller is a stored virtual account
ensure!(
caller
== Share::Pallet::<T>::virtual_acc(collection, item).unwrap().virtual_account,
== asset_account.clone(),
Error::<T>::NotAnAssetAccount
);

//Ensure that the Representative is not already connected to this asset
let representative = Roles::Pallet::<T>::get_pending_representatives(&rep_account).unwrap();
let rep_assets = representative.assets_accounts;
for i in rep_assets{
ensure!(i != asset_account,Error::<T>::AlreadyLinkedWithAsset);
}

//Approve role request
Self::approve_representative(origin, rep_account.clone()).ok();

Expand Down
2 changes: 1 addition & 1 deletion pallets/roles/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl<T: Config> Pallet<T> {
ensure!(!InvestorLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
ensure!(!ServicerLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
ensure!(!TenantLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
ensure!(!RepresentativeLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
//ensure!(!RepresentativeLog::<T>::contains_key(&caller), Error::<T>::OneRoleAllowed);
ensure!(Self::total_members() < T::MaxMembers::get(), Error::<T>::TotalMembersExceeded);
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion pallets/roles/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ pub mod pallet {
Self::deposit_event(Event::CreationRequestCreated(now, account.clone()));
},
Accounts::REPRESENTATIVE => {
ensure!(!requested, <Error<T>>::AlreadyWaiting);
//ensure!(!requested, <Error<T>>::AlreadyWaiting);
let representative = <T as frame_system::Config>::Origin::from(
RawOrigin::Signed(account.clone()),
);
Expand All @@ -351,6 +351,7 @@ pub mod pallet {
let need_approval = match account_type {
Accounts::INVESTOR => false,
Accounts::TENANT => false,
Accounts::REPRESENTATIVE => false,
_ => true,
};

Expand Down
35 changes: 23 additions & 12 deletions pallets/roles/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,20 +205,31 @@ where
pub fn new(acc: OriginFor<T>) -> DispatchResult {
let caller = ensure_signed(acc)?;
let now = <frame_system::Pallet<T>>::block_number();
ensure!(!RepresentativeLog::<T>::contains_key(&caller), Error::<T>::NoneValue);

let rep = Representative {
account_id: caller.clone(),
age: now,
activated: false,
assets_accounts: Vec::new(),
index: Default::default(),
};
if !RepresentativeLog::<T>::contains_key(&caller){

RepApprovalList::<T>::mutate(caller, |val| {
//val.push(rep);
*val = Some(rep);
});
let rep = Representative::<T> {
account_id: caller.clone(),
age: now,
activated: false,
assets_accounts: Vec::new(),
index: Default::default(),
};
RepApprovalList::<T>::mutate(caller, |val| {
//val.push(rep);
*val = Some(rep);
});
} else {
let rep = RepresentativeLog::<T>::get(&caller);
RepApprovalList::<T>::mutate(caller, |val| {
//val.push(rep);
*val = rep;
});
}





Ok(())
}
Expand Down
4 changes: 2 additions & 2 deletions pallets/tenancy/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,6 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

pub fn expect_events(e: Vec<Event>) {
/*pub fn expect_events(e: Vec<Event>) {
e.into_iter().for_each(frame_system::Pallet::<Test>::assert_has_event);
}
}*/
Loading