referrerpolicy=no-referrer-when-downgrade

pallet_broker/
nonfungible_impl.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18use super::*;
19use alloc::vec::Vec;
20use frame_support::{
21	pallet_prelude::{DispatchResult, *},
22	traits::nonfungible::{Inspect, Mutate, Transfer},
23};
24
25impl<T: Config> Inspect<T::AccountId> for Pallet<T> {
26	type ItemId = u128;
27
28	fn owner(item: &Self::ItemId) -> Option<T::AccountId> {
29		let record = Regions::<T>::get(RegionId::from(*item))?;
30		record.owner
31	}
32
33	fn attribute(item: &Self::ItemId, key: &[u8]) -> Option<Vec<u8>> {
34		let id = RegionId::from(*item);
35		let item = Regions::<T>::get(id)?;
36		match key {
37			b"begin" => Some(id.begin.encode()),
38			b"end" => Some(item.end.encode()),
39			b"length" => Some(item.end.saturating_sub(id.begin).encode()),
40			b"core" => Some(id.core.encode()),
41			b"part" => Some(id.mask.encode()),
42			b"owner" => Some(item.owner.encode()),
43			b"paid" => Some(item.paid.encode()),
44			_ => None,
45		}
46	}
47}
48
49impl<T: Config> Transfer<T::AccountId> for Pallet<T> {
50	fn transfer(item: &Self::ItemId, dest: &T::AccountId) -> DispatchResult {
51		Self::do_transfer((*item).into(), None, dest.clone()).map_err(Into::into)
52	}
53}
54
55/// We don't really support burning and minting.
56///
57/// We only need this to allow the region to be reserve transferable.
58///
59/// For reserve transfers that are not 'local', the asset must first be withdrawn to the holding
60/// register and then deposited into the designated account. This process necessitates that the
61/// asset is capable of being 'burned' and 'minted'.
62///
63/// Since each region is associated with specific record data, we will not actually burn the asset.
64/// If we did, we wouldn't know what record to assign to the newly minted region. Therefore, instead
65/// of burning, we set the asset's owner to `None`. In essence, 'burning' a region involves setting
66/// its owner to `None`, whereas 'minting' the region assigns its owner to an actual account. This
67/// way we never lose track of the associated record data.
68impl<T: Config> Mutate<T::AccountId> for Pallet<T> {
69	/// Deposit a region into an account.
70	fn mint_into(item: &Self::ItemId, who: &T::AccountId) -> DispatchResult {
71		let region_id: RegionId = (*item).into();
72		let record = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
73
74		// 'Minting' can only occur if the asset has previously been burned (i.e. moved to the
75		// holding register)
76		ensure!(record.owner.is_none(), Error::<T>::NotAllowed);
77		Self::issue(
78			region_id.core,
79			region_id.begin,
80			region_id.mask,
81			record.end,
82			Some(who.clone()),
83			record.paid,
84		);
85
86		Ok(())
87	}
88
89	/// Withdraw a region from account.
90	fn burn(item: &Self::ItemId, maybe_check_owner: Option<&T::AccountId>) -> DispatchResult {
91		let region_id: RegionId = (*item).into();
92		let mut record = Regions::<T>::get(&region_id).ok_or(Error::<T>::UnknownRegion)?;
93		if let Some(owner) = maybe_check_owner {
94			ensure!(Some(owner.clone()) == record.owner, Error::<T>::NotOwner);
95		}
96
97		record.owner = None;
98		Regions::<T>::insert(region_id, record);
99
100		Ok(())
101	}
102}