Skip to content

Commit

Permalink
feat: pallet/asset: implement status_change() call (#277)
Browse files Browse the repository at this point in the history
Signed-off-by: Amar Tumballi <amar@dhiway.com>
  • Loading branch information
amarts authored Feb 13, 2024
1 parent 5da4076 commit 5f3e37c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
60 changes: 60 additions & 0 deletions pallets/asset/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,13 @@ pub mod pallet {
from: AssetCreatorOf<T>,
to: AssetCreatorOf<T>,
},
/// An asset (or instance) entry has a new Status now
/// \[asset entry identifier, optional instance identifier, new status\]
StatusChange {
identifier: AssetIdOf,
instance: Option<AssetInstanceIdOf>,
status: AssetStatusOf,
},
}

#[pallet::error]
Expand Down Expand Up @@ -196,6 +203,8 @@ pub mod pallet {
DistributionLimitExceeded,
/// asset instance not found
AssetInstanceNotFound,
/// Asset is in same status as asked for
AssetInSameState,
}

#[pallet::call]
Expand Down Expand Up @@ -383,6 +392,57 @@ pub mod pallet {

Ok(())
}

#[pallet::call_index(3)]
#[pallet::weight({0})]
pub fn status_change(
origin: OriginFor<T>,
asset_id: AssetIdOf,
instance_id: Option<AssetInstanceIdOf>,
new_status: AssetStatusOf,
) -> DispatchResult {
let issuer = <T as Config>::EnsureOrigin::ensure_origin(origin)?.subject();

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

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

/* 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!(
new_status.clone() == instance.asset_instance_status,
Error::<T>::AssetInSameState
);

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

Self::update_activity(&inst_id, CallTypeOf::Update).map_err(<Error<T>>::from)?;
} else {
ensure!(new_status.clone() != asset.asset_status, Error::<T>::AssetInSameState);
<Assets<T>>::insert(
&asset_id,
AssetEntryOf::<T> { asset_status: new_status.clone(), ..asset },
);
Self::update_activity(&asset_id, CallTypeOf::Update).map_err(<Error<T>>::from)?;
}
Self::deposit_event(Event::StatusChange {
identifier: asset_id,
instance: instance_id,
status: new_status,
});

Ok(())
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions 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 Expand Up @@ -123,7 +127,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("cord"),
impl_name: create_runtime_str!("dhiway-cord"),
authoring_version: 0,
spec_version: 9010,
spec_version: 9012,
impl_version: 0,
apis: RUNTIME_API_VERSIONS,
transaction_version: 2,
Expand Down

0 comments on commit 5f3e37c

Please sign in to comment.