1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// This file is part of Substrate.

// Copyright (C) 2019-2022 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.

//! Traits for dealing with a single non-fungible collection of items.
//!
//! This assumes a single level namespace identified by `Inspect::ItemId`, and could
//! reasonably be implemented by pallets which wants to expose a single collection of NFT-like
//! objects.
//!
//! For an NFT API which has dual-level namespacing, the traits in `nonfungibles` are better to
//! use.

use super::nonfungibles;
use crate::{dispatch::DispatchResult, traits::Get};
use codec::{Decode, Encode};
use sp_runtime::TokenError;
use sp_std::prelude::*;

/// Trait for providing an interface to a read-only NFT-like set of items.
pub trait Inspect<AccountId> {
	/// Type for identifying an item.
	type ItemId;

	/// Returns the owner of `item`, or `None` if the item doesn't exist or has no
	/// owner.
	fn owner(item: &Self::ItemId) -> Option<AccountId>;

	/// Returns the attribute value of `item` corresponding to `key`.
	///
	/// By default this is `None`; no attributes are defined.
	fn attribute(_item: &Self::ItemId, _key: &[u8]) -> Option<Vec<u8>> {
		None
	}

	/// Returns the strongly-typed attribute value of `item` corresponding to `key`.
	///
	/// By default this just attempts to use `attribute`.
	fn typed_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
		key.using_encoded(|d| Self::attribute(item, d))
			.and_then(|v| V::decode(&mut &v[..]).ok())
	}

	/// Returns `true` if the `item` may be transferred.
	///
	/// Default implementation is that all items are transferable.
	fn can_transfer(_item: &Self::ItemId) -> bool {
		true
	}
}

/// Interface for enumerating items in existence or owned by a given account over a collection
/// of NFTs.
pub trait InspectEnumerable<AccountId>: Inspect<AccountId> {
	/// Returns an iterator of the items within a `collection` in existence.
	fn items() -> Box<dyn Iterator<Item = Self::ItemId>>;

	/// Returns an iterator of the items of all collections owned by `who`.
	fn owned(who: &AccountId) -> Box<dyn Iterator<Item = Self::ItemId>>;
}

/// Trait for providing an interface for NFT-like items which may be minted, burned and/or have
/// attributes set on them.
pub trait Mutate<AccountId>: Inspect<AccountId> {
	/// Mint some `item` to be owned by `who`.
	///
	/// By default, this is not a supported operation.
	fn mint_into(_item: &Self::ItemId, _who: &AccountId) -> DispatchResult {
		Err(TokenError::Unsupported.into())
	}

	/// Burn some `item`.
	///
	/// By default, this is not a supported operation.
	fn burn(_item: &Self::ItemId, _maybe_check_owner: Option<&AccountId>) -> DispatchResult {
		Err(TokenError::Unsupported.into())
	}

	/// Set attribute `value` of `item`'s `key`.
	///
	/// By default, this is not a supported operation.
	fn set_attribute(_item: &Self::ItemId, _key: &[u8], _value: &[u8]) -> DispatchResult {
		Err(TokenError::Unsupported.into())
	}

	/// Attempt to set the strongly-typed attribute `value` of `item`'s `key`.
	///
	/// By default this just attempts to use `set_attribute`.
	fn set_typed_attribute<K: Encode, V: Encode>(
		item: &Self::ItemId,
		key: &K,
		value: &V,
	) -> DispatchResult {
		key.using_encoded(|k| value.using_encoded(|v| Self::set_attribute(item, k, v)))
	}
}

/// Trait for providing a non-fungible set of items which can only be transferred.
pub trait Transfer<AccountId>: Inspect<AccountId> {
	/// Transfer `item` into `destination` account.
	fn transfer(item: &Self::ItemId, destination: &AccountId) -> DispatchResult;
}

/// Convert a `fungibles` trait implementation into a `fungible` trait implementation by identifying
/// a single item.
pub struct ItemOf<
	F: nonfungibles::Inspect<AccountId>,
	A: Get<<F as nonfungibles::Inspect<AccountId>>::CollectionId>,
	AccountId,
>(sp_std::marker::PhantomData<(F, A, AccountId)>);

impl<
		F: nonfungibles::Inspect<AccountId>,
		A: Get<<F as nonfungibles::Inspect<AccountId>>::CollectionId>,
		AccountId,
	> Inspect<AccountId> for ItemOf<F, A, AccountId>
{
	type ItemId = <F as nonfungibles::Inspect<AccountId>>::ItemId;
	fn owner(item: &Self::ItemId) -> Option<AccountId> {
		<F as nonfungibles::Inspect<AccountId>>::owner(&A::get(), item)
	}
	fn attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
		<F as nonfungibles::Inspect<AccountId>>::attribute(&A::get(), item, key)
	}
	fn typed_attribute<K: Encode, V: Decode>(item: &Self::ItemId, key: &K) -> Option<V> {
		<F as nonfungibles::Inspect<AccountId>>::typed_attribute(&A::get(), item, key)
	}
	fn can_transfer(item: &Self::ItemId) -> bool {
		<F as nonfungibles::Inspect<AccountId>>::can_transfer(&A::get(), item)
	}
}

impl<
		F: nonfungibles::InspectEnumerable<AccountId>,
		A: Get<<F as nonfungibles::Inspect<AccountId>>::CollectionId>,
		AccountId,
	> InspectEnumerable<AccountId> for ItemOf<F, A, AccountId>
{
	fn items() -> Box<dyn Iterator<Item = Self::ItemId>> {
		<F as nonfungibles::InspectEnumerable<AccountId>>::items(&A::get())
	}
	fn owned(who: &AccountId) -> Box<dyn Iterator<Item = Self::ItemId>> {
		<F as nonfungibles::InspectEnumerable<AccountId>>::owned_in_collection(&A::get(), who)
	}
}

impl<
		F: nonfungibles::Mutate<AccountId>,
		A: Get<<F as nonfungibles::Inspect<AccountId>>::CollectionId>,
		AccountId,
	> Mutate<AccountId> for ItemOf<F, A, AccountId>
{
	fn mint_into(item: &Self::ItemId, who: &AccountId) -> DispatchResult {
		<F as nonfungibles::Mutate<AccountId>>::mint_into(&A::get(), item, who)
	}
	fn burn(item: &Self::ItemId, maybe_check_owner: Option<&AccountId>) -> DispatchResult {
		<F as nonfungibles::Mutate<AccountId>>::burn(&A::get(), item, maybe_check_owner)
	}
	fn set_attribute(item: &Self::ItemId, key: &[u8], value: &[u8]) -> DispatchResult {
		<F as nonfungibles::Mutate<AccountId>>::set_attribute(&A::get(), item, key, value)
	}
	fn set_typed_attribute<K: Encode, V: Encode>(
		item: &Self::ItemId,
		key: &K,
		value: &V,
	) -> DispatchResult {
		<F as nonfungibles::Mutate<AccountId>>::set_typed_attribute(&A::get(), item, key, value)
	}
}

impl<
		F: nonfungibles::Transfer<AccountId>,
		A: Get<<F as nonfungibles::Inspect<AccountId>>::CollectionId>,
		AccountId,
	> Transfer<AccountId> for ItemOf<F, A, AccountId>
{
	fn transfer(item: &Self::ItemId, destination: &AccountId) -> DispatchResult {
		<F as nonfungibles::Transfer<AccountId>>::transfer(&A::get(), item, destination)
	}
}