1#![cfg_attr(not(feature = "std"), no_std)]23use sp_std::collections::btree_set::BTreeSet;45use frame_support::dispatch::DispatchError;6use frame_support::fail;7pub use pallet::*;8use pallet_common::{dispatch::CollectionDispatch, CollectionHandle};9use up_data_structs::{CollectionId, TokenId, mapping::TokenAddressMapping, budget::Budget};1011#[cfg(feature = "runtime-benchmarks")]12pub mod benchmarking;13pub mod weights;1415pub type SelfWeightOf<T> = <T as crate::Config>::WeightInfo;1617#[frame_support::pallet]18pub mod pallet {19 use frame_support::Parameter;20 use frame_support::dispatch::{GetDispatchInfo, UnfilteredDispatchable};21 use frame_support::pallet_prelude::*;2223 use super::*;2425 #[pallet::error]26 pub enum Error<T> {27 28 OuroborosDetected,29 30 DepthLimit,31 32 TokenNotFound,33 }3435 #[pallet::event]36 pub enum Event<T> {37 38 Executed(DispatchResult),39 }4041 #[pallet::config]42 pub trait Config: frame_system::Config + pallet_common::Config {43 type WeightInfo: weights::WeightInfo;44 type Event: IsType<<Self as frame_system::Config>::Event> + From<Event<Self>>;45 type Call: Parameter + UnfilteredDispatchable<Origin = Self::Origin> + GetDispatchInfo;46 }4748 #[pallet::pallet]49 pub struct Pallet<T>(_);5051 #[pallet::call]52 impl<T: Config> Pallet<T> {53 54 5556 57 58 59 60 61 62 63 64 65 66 67 68 }69}7071#[derive(PartialEq)]72pub enum Parent<CrossAccountId> {73 74 Normal(CrossAccountId),75 76 TokenNotFound,77 78 Token(CollectionId, TokenId),79}8081impl<T: Config> Pallet<T> {82 pub fn find_parent(83 collection: CollectionId,84 token: TokenId,85 ) -> Result<Parent<T::CrossAccountId>, DispatchError> {86 87 let handle = match CollectionHandle::try_get(collection) {88 Ok(v) => v,89 Err(_) => return Ok(Parent::TokenNotFound),90 };91 let handle = T::CollectionDispatch::dispatch(handle);92 let handle = handle.as_dyn();9394 Ok(match handle.token_owner(token) {95 Some(owner) => match T::CrossTokenAddressMapping::address_to_token(&owner) {96 Some((collection, token)) => Parent::Token(collection, token),97 None => Parent::Normal(owner),98 },99 None => Parent::TokenNotFound,100 })101 }102103 pub fn parent_chain(104 mut collection: CollectionId,105 mut token: TokenId,106 ) -> impl Iterator<Item = Result<Parent<T::CrossAccountId>, DispatchError>> {107 let mut finished = false;108 let mut visited = BTreeSet::new();109 visited.insert((collection, token));110 core::iter::from_fn(move || {111 if finished {112 return None;113 }114 let parent = Self::find_parent(collection, token);115 match parent {116 Ok(Parent::Token(new_collection, new_token)) => {117 collection = new_collection;118 token = new_token;119 if !visited.insert((new_collection, new_token)) {120 finished = true;121 return Some(Err(<Error<T>>::OuroborosDetected.into()));122 }123 }124 _ => finished = true,125 }126 Some(parent as Result<_, DispatchError>)127 })128 }129130 131 132 133 pub fn find_topmost_owner(134 collection: CollectionId,135 token: TokenId,136 budget: &dyn Budget,137 ) -> Result<T::CrossAccountId, DispatchError> {138 let owner = Self::parent_chain(collection, token)139 .take_while(|_| budget.consume())140 .find(|p| matches!(p, Ok(Parent::Normal(_) | Parent::TokenNotFound)))141 .ok_or(<Error<T>>::DepthLimit)??;142143 Ok(match owner {144 Parent::Normal(v) => v,145 _ => fail!(<Error<T>>::TokenNotFound),146 })147 }148149 150 pub fn indirectly_owned(151 user: T::CrossAccountId,152 collection: CollectionId,153 token: TokenId,154 budget: &dyn Budget,155 ) -> Result<bool, DispatchError> {156 let target_parent = match T::CrossTokenAddressMapping::address_to_token(&user) {157 Some((collection, token)) => Parent::Token(collection, token),158 None => Parent::Normal(user),159 };160161 Ok(Self::parent_chain(collection, token)162 .take_while(|_| budget.consume())163 .any(|parent| Ok(&target_parent) == parent.as_ref()))164 }165}