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

feat: pallet/asset: implement status_change() call #277

Merged
merged 5 commits into from
Feb 13, 2024
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
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>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to implement the status change behaviour for assets and instances separately.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@smohan-dw looks like the comment is bit older than the review. Currently, the status change takes instance status change if instance is sent. Asset state is not changed. If instance is None, then asset status change happens.

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
Loading