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

Improvement/refactor try origin pattern #39

Merged
merged 3 commits into from
Dec 29, 2021
Merged
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
64 changes: 23 additions & 41 deletions pallets/rmrk-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,7 @@ pub mod pallet {
royalty: Option<Permill>,
metadata: BoundedVec<u8, T::StringLimit>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let (collection_id, nft_id) = Self::nft_mint(
sender.clone().unwrap_or_default(),
Expand Down Expand Up @@ -299,10 +296,7 @@ pub mod pallet {
max: Option<u32>,
symbol: BoundedVec<u8, T::StringLimit>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let max = max.unwrap_or_default();

Expand Down Expand Up @@ -356,10 +350,7 @@ pub mod pallet {
origin: OriginFor<T>,
collection_id: CollectionId,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

Self::collection_burn(sender.clone().unwrap_or_default(), collection_id)?;

Expand Down Expand Up @@ -389,11 +380,8 @@ pub mod pallet {
nft_id: NftId,
new_owner: AccountIdOrCollectionNftTuple<T::AccountId>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
}
.unwrap_or_default();
let sender = Self::ensure_protocol_or_signed(origin)?
.unwrap_or_default();

let max_recursions = T::MaxRecursions::get();
Self::nft_send(
Expand Down Expand Up @@ -421,10 +409,7 @@ pub mod pallet {
collection_id: CollectionId,
new_issuer: <T::Lookup as StaticLookup>::Source,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;
let new_issuer = T::Lookup::lookup(new_issuer)?;

ensure!(
Expand Down Expand Up @@ -453,10 +438,7 @@ pub mod pallet {
key: BoundedVec<u8, T::KeyLimit>,
value: BoundedVec<u8, T::ValueLimit>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let collection =
Collections::<T>::get(&collection_id).ok_or(Error::<T>::NoAvailableCollectionId)?;
Expand All @@ -483,10 +465,7 @@ pub mod pallet {
origin: OriginFor<T>,
collection_id: CollectionId,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let collection_id = Self::collection_lock(collection_id)?;

Expand All @@ -511,10 +490,7 @@ pub mod pallet {
license: Option<BoundedVec<u8, T::StringLimit>>,
thumb: Option<BoundedVec<u8, T::StringLimit>>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let mut pending = false;
let nft = NFTs::<T>::get(collection_id, nft_id).ok_or(Error::<T>::NoAvailableNftId)?;
Expand Down Expand Up @@ -558,10 +534,7 @@ pub mod pallet {
nft_id: NftId,
resource_id: ResourceId,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let sender = Self::ensure_protocol_or_signed(origin)?;

let nft = NFTs::<T>::get(collection_id, nft_id).ok_or(Error::<T>::NoAvailableNftId)?;
ensure!(nft.rootowner == sender.unwrap_or_default(), Error::<T>::NoPermission);
Expand Down Expand Up @@ -589,10 +562,7 @@ pub mod pallet {
nft_id: NftId,
priorities: Vec<Vec<u8>>,
) -> DispatchResult {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
let _ = Self::ensure_protocol_or_signed(origin)?;
let mut bounded_priorities = Vec::<BoundedVec<u8, T::StringLimit>>::new();
for priority in priorities {
let bounded_priority = Self::to_bounded_string(priority)?;
Expand All @@ -603,4 +573,16 @@ pub mod pallet {
Ok(())
}
}

// Helpers
impl<T: Config> Pallet<T> {
pub(super) fn ensure_protocol_or_signed(origin: OriginFor<T>) -> Result<Option<T::AccountId>, sp_runtime::DispatchError> {
let sender = match T::ProtocolOrigin::try_origin(origin) {
Ok(_) => None,
Err(origin) => Some(ensure_signed(origin)?),
};
Ok(sender)
}

}
}