use super::*;
pub type PredefinedIdNftsInstance = unique_items::Instance3;
pub type NftLocalId = u64;
pub type NftFullId = (CollectionAutoId, NftLocalId);
impl unique_items::Config<PredefinedIdNftsInstance> for Test {
type ItemId = NftFullId;
}
impl Create<WithConfig<ConfigValue<Owner<AccountId>>, PredefinedId<NftFullId>>>
for PredefinedIdNfts
{
fn create(
strategy: WithConfig<ConfigValue<Owner<AccountId>>, PredefinedId<NftFullId>>,
) -> Result<NftFullId, DispatchError> {
let WithConfig { config: ConfigValue(owner), extra: id_assignment } = strategy;
let id = id_assignment.params;
unique_items::ItemOwner::<Test, PredefinedIdNftsInstance>::try_mutate(
id,
|current_owner| {
if current_owner.is_none() {
*current_owner = Some(owner);
Ok(())
} else {
Err(unique_items::Error::<Test, PredefinedIdNftsInstance>::AlreadyExists)
}
},
)?;
Ok(id)
}
}
impl AssetDefinition for PredefinedIdNfts {
type Id = (CollectionAutoId, NftLocalId);
}
impl Inspect<CanCreate> for PredefinedIdNfts {
fn inspect(id: &Self::Id, _: CanCreate) -> Result<bool, DispatchError> {
let nft_exists =
unique_items::ItemOwner::<Test, PredefinedIdNftsInstance>::contains_key(id);
Ok(!nft_exists)
}
}
impl Update<ChangeOwnerFrom<AccountId>> for PredefinedIdNfts {
fn update(
id: &Self::Id,
strategy: ChangeOwnerFrom<AccountId>,
new_owner: &AccountId,
) -> DispatchResult {
let CheckState(check_owner, _) = strategy;
unique_items::ItemOwner::<Test, PredefinedIdNftsInstance>::try_mutate(id, |owner| {
match owner {
Some(current_owner) => {
if *current_owner == check_owner {
*owner = Some(*new_owner);
Ok(())
} else {
Err(unique_items::Error::<Test, PredefinedIdNftsInstance>::NoPermission
.into())
}
},
None => {
Err(unique_items::Error::<Test, PredefinedIdNftsInstance>::UnknownItem.into())
},
}
})
}
}