mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-05-30 09:21:04 +00:00
Non-fungible token traits (#8993)
* Non-fungible token traits * Docs * Fixes * Implement non-fungible trait for Uniques * Update frame/uniques/src/impl_nonfungibles.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> * Update frame/uniques/src/impl_nonfungibles.rs Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com> Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Various pieces of common functionality.
|
||||
|
||||
use super::*;
|
||||
use frame_support::{ensure, traits::Get};
|
||||
use sp_runtime::{DispatchResult, DispatchError};
|
||||
|
||||
impl<T: Config<I>, I: 'static> Pallet<T, I> {
|
||||
pub(crate) fn do_transfer(
|
||||
class: T::ClassId,
|
||||
instance: T::InstanceId,
|
||||
dest: T::AccountId,
|
||||
with_details: impl FnOnce(
|
||||
&ClassDetailsFor<T, I>,
|
||||
&mut InstanceDetailsFor<T, I>,
|
||||
) -> DispatchResult,
|
||||
) -> DispatchResult {
|
||||
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(!class_details.is_frozen, Error::<T, I>::Frozen);
|
||||
|
||||
let mut details = Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(!details.is_frozen, Error::<T, I>::Frozen);
|
||||
with_details(&class_details, &mut details)?;
|
||||
|
||||
Account::<T, I>::remove((&details.owner, &class, &instance));
|
||||
Account::<T, I>::insert((&dest, &class, &instance), ());
|
||||
let origin = details.owner;
|
||||
details.owner = dest;
|
||||
Asset::<T, I>::insert(&class, &instance, &details);
|
||||
|
||||
Self::deposit_event(Event::Transferred(class, instance, origin, details.owner));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn do_mint(
|
||||
class: T::ClassId,
|
||||
instance: T::InstanceId,
|
||||
owner: T::AccountId,
|
||||
with_details: impl FnOnce(
|
||||
&ClassDetailsFor<T, I>,
|
||||
) -> DispatchResult,
|
||||
) -> DispatchResult {
|
||||
ensure!(!Asset::<T, I>::contains_key(class, instance), Error::<T, I>::AlreadyExists);
|
||||
|
||||
Class::<T, I>::try_mutate(&class, |maybe_class_details| -> DispatchResult {
|
||||
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
|
||||
|
||||
with_details(&class_details)?;
|
||||
|
||||
let instances = class_details.instances.checked_add(1)
|
||||
.ok_or(ArithmeticError::Overflow)?;
|
||||
class_details.instances = instances;
|
||||
|
||||
let deposit = match class_details.free_holding {
|
||||
true => Zero::zero(),
|
||||
false => T::InstanceDeposit::get(),
|
||||
};
|
||||
T::Currency::reserve(&class_details.owner, deposit)?;
|
||||
class_details.total_deposit += deposit;
|
||||
|
||||
let owner = owner.clone();
|
||||
Account::<T, I>::insert((&owner, &class, &instance), ());
|
||||
let details = InstanceDetails { owner, approved: None, is_frozen: false, deposit};
|
||||
Asset::<T, I>::insert(&class, &instance, details);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Self::deposit_event(Event::Issued(class, instance, owner));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(super) fn do_burn(
|
||||
class: T::ClassId,
|
||||
instance: T::InstanceId,
|
||||
with_details: impl FnOnce(
|
||||
&ClassDetailsFor<T, I>,
|
||||
&InstanceDetailsFor<T, I>,
|
||||
) -> DispatchResult,
|
||||
) -> DispatchResult {
|
||||
let owner = Class::<T, I>::try_mutate(&class, |maybe_class_details| -> Result<T::AccountId, DispatchError> {
|
||||
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
|
||||
let details = Asset::<T, I>::get(&class, &instance)
|
||||
.ok_or(Error::<T, I>::Unknown)?;
|
||||
with_details(&class_details, &details)?;
|
||||
|
||||
// Return the deposit.
|
||||
T::Currency::unreserve(&class_details.owner, details.deposit);
|
||||
class_details.total_deposit.saturating_reduce(details.deposit);
|
||||
class_details.instances.saturating_dec();
|
||||
Ok(details.owner)
|
||||
})?;
|
||||
|
||||
Asset::<T, I>::remove(&class, &instance);
|
||||
Account::<T, I>::remove((&owner, &class, &instance));
|
||||
|
||||
Self::deposit_event(Event::Burned(class, instance, owner));
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// This file is part of Substrate.
|
||||
|
||||
// Copyright (C) 2017-2021 Parity Technologies (UK) Ltd.
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//! Implementations for `nonfungibles` traits.
|
||||
|
||||
use super::*;
|
||||
use sp_std::convert::TryFrom;
|
||||
use frame_support::traits::tokens::nonfungibles::{Inspect, Mutate, Transfer};
|
||||
use frame_support::BoundedSlice;
|
||||
use sp_runtime::DispatchResult;
|
||||
|
||||
impl<T: Config<I>, I: 'static> Inspect<<T as SystemConfig>::AccountId> for Pallet<T, I> {
|
||||
type InstanceId = T::InstanceId;
|
||||
type ClassId = T::ClassId;
|
||||
|
||||
fn owner(
|
||||
class: &Self::ClassId,
|
||||
instance: &Self::InstanceId,
|
||||
) -> Option<<T as SystemConfig>::AccountId> {
|
||||
Asset::<T, I>::get(class, instance).map(|a| a.owner)
|
||||
}
|
||||
|
||||
fn class_owner(class: &Self::ClassId) -> Option<<T as SystemConfig>::AccountId> {
|
||||
Class::<T, I>::get(class).map(|a| a.owner)
|
||||
}
|
||||
|
||||
/// Returns the attribute value of `instance` of `class` corresponding to `key`.
|
||||
///
|
||||
/// When `key` is empty, we return the instance metadata value.
|
||||
///
|
||||
/// By default this is `None`; no attributes are defined.
|
||||
fn attribute(class: &Self::ClassId, instance: &Self::InstanceId, key: &[u8])
|
||||
-> Option<Vec<u8>>
|
||||
{
|
||||
if key.is_empty() {
|
||||
// We make the empty key map to the instance metadata value.
|
||||
InstanceMetadataOf::<T, I>::get(class, instance).map(|m| m.data.into())
|
||||
} else {
|
||||
let key = BoundedSlice::<_, _>::try_from(key).ok()?;
|
||||
Attribute::<T, I>::get((class, Some(instance), key)).map(|a| a.0.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the attribute value of `instance` of `class` corresponding to `key`.
|
||||
///
|
||||
/// When `key` is empty, we return the instance metadata value.
|
||||
///
|
||||
/// By default this is `None`; no attributes are defined.
|
||||
fn class_attribute(class: &Self::ClassId, key: &[u8])
|
||||
-> Option<Vec<u8>>
|
||||
{
|
||||
if key.is_empty() {
|
||||
// We make the empty key map to the instance metadata value.
|
||||
ClassMetadataOf::<T, I>::get(class).map(|m| m.data.into())
|
||||
} else {
|
||||
let key = BoundedSlice::<_, _>::try_from(key).ok()?;
|
||||
Attribute::<T, I>::get((class, Option::<T::InstanceId>::None, key)).map(|a| a.0.into())
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns `true` if the asset `instance` of `class` may be transferred.
|
||||
///
|
||||
/// Default implementation is that all assets are transferable.
|
||||
fn can_transfer(class: &Self::ClassId, instance: &Self::InstanceId) -> bool {
|
||||
match (Class::<T, I>::get(class), Asset::<T, I>::get(class, instance)) {
|
||||
(Some(cd), Some(id)) if !cd.is_frozen && !id.is_frozen => true,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: 'static> Mutate<<T as SystemConfig>::AccountId> for Pallet<T, I> {
|
||||
fn mint_into(
|
||||
class: &Self::ClassId,
|
||||
instance: &Self::InstanceId,
|
||||
who: &T::AccountId,
|
||||
) -> DispatchResult {
|
||||
Self::do_mint(class.clone(), instance.clone(), who.clone(), |_| Ok(()))
|
||||
}
|
||||
|
||||
fn burn_from(class: &Self::ClassId, instance: &Self::InstanceId) -> DispatchResult {
|
||||
Self::do_burn(class.clone(), instance.clone(), |_, _| Ok(()))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Config<I>, I: 'static> Transfer<T::AccountId> for Pallet<T, I> {
|
||||
fn transfer(
|
||||
class: &Self::ClassId,
|
||||
instance: &Self::InstanceId,
|
||||
destination: &T::AccountId,
|
||||
) -> DispatchResult {
|
||||
Self::do_transfer(class.clone(), instance.clone(), destination.clone(), |_, _| Ok(()))
|
||||
}
|
||||
}
|
||||
@@ -36,6 +36,8 @@ pub mod mock;
|
||||
mod tests;
|
||||
|
||||
mod types;
|
||||
mod functions;
|
||||
mod impl_nonfungibles;
|
||||
pub use types::*;
|
||||
|
||||
use sp_std::prelude::*;
|
||||
@@ -448,32 +450,10 @@ pub mod pallet {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let owner = T::Lookup::lookup(owner)?;
|
||||
|
||||
ensure!(!Asset::<T, I>::contains_key(class, instance), Error::<T, I>::AlreadyExists);
|
||||
|
||||
Class::<T, I>::try_mutate(&class, |maybe_class_details| -> DispatchResult {
|
||||
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
|
||||
Self::do_mint(class, instance, owner, |class_details| {
|
||||
ensure!(class_details.issuer == origin, Error::<T, I>::NoPermission);
|
||||
|
||||
let instances = class_details.instances.checked_add(1)
|
||||
.ok_or(ArithmeticError::Overflow)?;
|
||||
class_details.instances = instances;
|
||||
|
||||
let deposit = match class_details.free_holding {
|
||||
true => Zero::zero(),
|
||||
false => T::InstanceDeposit::get(),
|
||||
};
|
||||
T::Currency::reserve(&class_details.owner, deposit)?;
|
||||
class_details.total_deposit += deposit;
|
||||
|
||||
let owner = owner.clone();
|
||||
Account::<T, I>::insert((&owner, &class, &instance), ());
|
||||
let details = InstanceDetails { owner, approved: None, is_frozen: false, deposit};
|
||||
Asset::<T, I>::insert(&class, &instance, details);
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
Self::deposit_event(Event::Issued(class, instance, owner));
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Destroy a single asset instance.
|
||||
@@ -499,27 +479,12 @@ pub mod pallet {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let check_owner = check_owner.map(T::Lookup::lookup).transpose()?;
|
||||
|
||||
let owner = Class::<T, I>::try_mutate(&class, |maybe_class_details| -> Result<T::AccountId, DispatchError> {
|
||||
let class_details = maybe_class_details.as_mut().ok_or(Error::<T, I>::Unknown)?;
|
||||
let details = Asset::<T, I>::get(&class, &instance)
|
||||
.ok_or(Error::<T, I>::Unknown)?;
|
||||
Self::do_burn(class, instance, |class_details, details| {
|
||||
let is_permitted = class_details.admin == origin || details.owner == origin;
|
||||
ensure!(is_permitted, Error::<T, I>::NoPermission);
|
||||
ensure!(check_owner.map_or(true, |o| o == details.owner), Error::<T, I>::WrongOwner);
|
||||
|
||||
// Return the deposit.
|
||||
T::Currency::unreserve(&class_details.owner, details.deposit);
|
||||
class_details.total_deposit.saturating_reduce(details.deposit);
|
||||
class_details.instances.saturating_dec();
|
||||
Ok(details.owner)
|
||||
})?;
|
||||
|
||||
|
||||
Asset::<T, I>::remove(&class, &instance);
|
||||
Account::<T, I>::remove((&owner, &class, &instance));
|
||||
|
||||
Self::deposit_event(Event::Burned(class, instance, owner));
|
||||
Ok(())
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Move an asset from the sender account to another.
|
||||
@@ -547,24 +512,13 @@ pub mod pallet {
|
||||
let origin = ensure_signed(origin)?;
|
||||
let dest = T::Lookup::lookup(dest)?;
|
||||
|
||||
let class_details = Class::<T, I>::get(&class).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(!class_details.is_frozen, Error::<T, I>::Frozen);
|
||||
|
||||
let mut details = Asset::<T, I>::get(&class, &instance).ok_or(Error::<T, I>::Unknown)?;
|
||||
ensure!(!details.is_frozen, Error::<T, I>::Frozen);
|
||||
if details.owner != origin && class_details.admin != origin {
|
||||
let approved = details.approved.take().map_or(false, |i| i == origin);
|
||||
ensure!(approved, Error::<T, I>::NoPermission);
|
||||
}
|
||||
|
||||
Account::<T, I>::remove((&details.owner, &class, &instance));
|
||||
Account::<T, I>::insert((&dest, &class, &instance), ());
|
||||
details.owner = dest;
|
||||
Asset::<T, I>::insert(&class, &instance, &details);
|
||||
|
||||
Self::deposit_event(Event::Transferred(class, instance, origin, details.owner));
|
||||
|
||||
Ok(())
|
||||
Self::do_transfer(class, instance, dest, |class_details, details| {
|
||||
if details.owner != origin && class_details.admin != origin {
|
||||
let approved = details.approved.take().map_or(false, |i| i == origin);
|
||||
ensure!(approved, Error::<T, I>::NoPermission);
|
||||
}
|
||||
Ok(())
|
||||
})
|
||||
}
|
||||
|
||||
/// Reevaluate the deposits on some assets.
|
||||
|
||||
@@ -22,6 +22,11 @@ use frame_support::{traits::Get, BoundedVec};
|
||||
|
||||
pub(super) type DepositBalanceOf<T, I = ()> =
|
||||
<<T as Config<I>>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;
|
||||
pub(super) type ClassDetailsFor<T, I> =
|
||||
ClassDetails<<T as SystemConfig>::AccountId, DepositBalanceOf<T, I>>;
|
||||
pub(super) type InstanceDetailsFor<T, I> =
|
||||
InstanceDetails<<T as SystemConfig>::AccountId, DepositBalanceOf<T, I>>;
|
||||
|
||||
|
||||
#[derive(Clone, Encode, Decode, Eq, PartialEq, RuntimeDebug)]
|
||||
pub struct ClassDetails<
|
||||
|
||||
Reference in New Issue
Block a user