Skip to content

Commit

Permalink
feat: pallet/asset: implement revoke() call
Browse files Browse the repository at this point in the history
Signed-off-by: Amar Tumballi <[email protected]>
  • Loading branch information
amarts committed Feb 4, 2024
1 parent 6a37df6 commit c9390f3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
56 changes: 56 additions & 0 deletions pallets/asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ pub mod pallet {
from: AssetCreatorOf<T>,
to: AssetCreatorOf<T>,
},
/// An asset entry has been revoked.
/// \[asset entry identifier, optional instance identifier\]
Revoke { identifier: AssetIdOf, instance: Option<AssetInstanceIdOf> },
}

#[pallet::error]
Expand Down Expand Up @@ -383,6 +386,59 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight({0})]
pub fn revoke(
origin: OriginFor<T>,
asset_id: AssetIdOf,
instance_id: Option<AssetInstanceIdOf>,
authorization: AuthorizationIdOf,
) -> DispatchResult {
let owner = <T as Config>::EnsureOrigin::ensure_origin(origin)?.subject();
let _space_id = pallet_chain_space::Pallet::<T>::ensure_authorization_origin(
&authorization,
&owner,
)
.map_err(<pallet_chain_space::Error<T>>::from)?;

let asset = <Assets<T>>::get(&asset_id).ok_or(Error::<T>::AssetIdNotFound)?;

ensure!(asset.asset_issuer == owner, Error::<T>::UnauthorizedOperation);

ensure!(AssetStatusOf::ACTIVE == asset.asset_status, Error::<T>::AssetNotActive);

/* If instance ID is provided, only revoke the instance, not the asset */
if let Some(ref inst_id) = instance_id {
let instance = <Issuance<T>>::get(&asset_id, &inst_id)
.ok_or(Error::<T>::AssetInstanceNotFound)?;
ensure!(
AssetStatusOf::ACTIVE == instance.asset_instance_status,
Error::<T>::InstanceNotActive
);

/* update the storage with new status */
<Issuance<T>>::insert(
&asset_id,
&inst_id,
AssetDistributionEntryOf::<T> {
asset_instance_status: AssetStatusOf::REVOKED,
..instance
},
);

Self::update_activity(&inst_id, CallTypeOf::Revoke).map_err(<Error<T>>::from)?;
} else {
<Assets<T>>::insert(
&asset_id,
AssetEntryOf::<T> { asset_status: AssetStatusOf::REVOKED, ..asset },
);
Self::update_activity(&asset_id, CallTypeOf::Revoke).map_err(<Error<T>>::from)?;
}
Self::deposit_event(Event::Revoke { identifier: asset_id, instance: instance_id });

Ok(())
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion pallets/asset/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub enum AssetTypeOf {
pub enum AssetStatusOf {
ACTIVE,
INACTIVE,
REVOKED,
EXPIRED,
}

Expand All @@ -60,7 +61,7 @@ impl AssetTypeOf {

impl AssetStatusOf {
pub fn is_valid_status_type(&self) -> bool {
matches!(self, Self::ACTIVE | Self::INACTIVE | Self::EXPIRED)
matches!(self, Self::ACTIVE | Self::INACTIVE | Self::EXPIRED | Self::REVOKED)
}
}

Expand Down
6 changes: 5 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ use frame_support::{
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot, EnsureSigned,
EnsureRoot,
};

#[cfg(feature = "runtime-benchmarks")]
use frame_system::EnsureSigned;

use sp_consensus_grandpa::AuthorityId as GrandpaId;

use pallet_identity::simple::IdentityInfo;
Expand Down

0 comments on commit c9390f3

Please sign in to comment.