difftreelog
chore rebase develop
in: master
2 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -2567,15 +2567,15 @@
[[package]]
name = "data-encoding"
-version = "2.4.0"
+version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c2e66c9d817f1720209181c316d28635c050fa304f9c79e47a520882661b7308"
+checksum = "7e962a19be5cfc3f3bf6dd8f61eb50107f356ad6270fbb3ed41476571db78be5"
[[package]]
name = "data-encoding-macro"
-version = "0.1.13"
+version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "c904b33cc60130e1aeea4956ab803d08a3f4a0ca82d64ed757afac3891f2bb99"
+checksum = "20c01c06f5f429efdf2bae21eb67c28b3df3cf85b7dd2d8ef09c0838dac5d33e"
dependencies = [
"data-encoding",
"data-encoding-macro-internal",
@@ -2583,9 +2583,9 @@
[[package]]
name = "data-encoding-macro-internal"
-version = "0.1.11"
+version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8fdf3fce3ce863539ec1d7fd1b6dcc3c645663376b43ed376bbf887733e4f772"
+checksum = "0047d07f2c89b17dd631c80450d69841a6b5d7fb17278cbc43d7e4cfcf2576f3"
dependencies = [
"data-encoding",
"syn 1.0.109",
@@ -4678,21 +4678,21 @@
[[package]]
name = "if-addrs"
-version = "0.7.0"
+version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "cbc0fa01ffc752e9dbc72818cdb072cd028b86be5e09dd04c5a643704fe101a9"
+checksum = "cabb0019d51a643781ff15c9c8a3e5dedc365c47211270f4e8f82812fedd8f0a"
dependencies = [
"libc",
- "winapi",
+ "windows-sys 0.48.0",
]
[[package]]
name = "if-watch"
-version = "3.1.0"
+version = "3.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bbb892e5777fe09e16f3d44de7802f4daa7267ecbe8c466f19d94e25bb0c303e"
+checksum = "d6b0422c86d7ce0e97169cc42e04ae643caf278874a7a3c87b8150a220dc7e1e"
dependencies = [
- "async-io 1.13.0",
+ "async-io 2.2.0",
"core-foundation",
"fnv",
"futures",
@@ -6706,8 +6706,8 @@
[[package]]
name = "orml-traits"
-version = "0.5.0-dev"
-source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#6f36a9e79b75421ac56b259fd47d03db6c3e1603"
+version = "0.6.1"
+source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#99604b000be6252597cfcb82eee82554f6a07a0e"
dependencies = [
"frame-support",
"impl-trait-for-tuples",
@@ -6726,8 +6726,8 @@
[[package]]
name = "orml-utilities"
-version = "0.5.0-dev"
-source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#6f36a9e79b75421ac56b259fd47d03db6c3e1603"
+version = "0.6.1"
+source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#99604b000be6252597cfcb82eee82554f6a07a0e"
dependencies = [
"frame-support",
"parity-scale-codec",
@@ -6741,8 +6741,8 @@
[[package]]
name = "orml-vesting"
-version = "0.5.0-dev"
-source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#6f36a9e79b75421ac56b259fd47d03db6c3e1603"
+version = "0.6.1"
+source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#99604b000be6252597cfcb82eee82554f6a07a0e"
dependencies = [
"frame-support",
"frame-system",
@@ -6756,8 +6756,8 @@
[[package]]
name = "orml-xcm-support"
-version = "0.5.0-dev"
-source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#6f36a9e79b75421ac56b259fd47d03db6c3e1603"
+version = "0.6.1"
+source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#99604b000be6252597cfcb82eee82554f6a07a0e"
dependencies = [
"frame-support",
"orml-traits",
@@ -6770,8 +6770,8 @@
[[package]]
name = "orml-xtokens"
-version = "0.5.0-dev"
-source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#6f36a9e79b75421ac56b259fd47d03db6c3e1603"
+version = "0.6.1"
+source = "git+https://github.com/uniquenetwork/open-runtime-module-library?branch=unique-polkadot-v1.3.0#99604b000be6252597cfcb82eee82554f6a07a0e"
dependencies = [
"cumulus-primitives-core",
"frame-support",
@@ -13675,9 +13675,9 @@
[[package]]
name = "spinners"
-version = "4.1.0"
+version = "4.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "08615eea740067d9899969bc2891c68a19c315cb1f66640af9a9ecb91b13bcab"
+checksum = "a0ef947f358b9c238923f764c72a4a9d42f2d637c46e059dbd319d6e7cfb4f82"
dependencies = [
"lazy_static",
"maplit",
pallets/foreign-assets/src/lib.rsdiffbeforeafterboth1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! # Foreign Assets18//!19//! ## Overview20//!21//! The Foreign Assets is a proxy that maps XCM operations to the Unique Network's pallets logic.2223#![cfg_attr(not(feature = "std"), no_std)]24#![allow(clippy::unused_unit)]2526use core::ops::Deref;2728use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin, PalletId};29use frame_system::pallet_prelude::*;30use pallet_common::{31 dispatch::CollectionDispatch, erc::CrossAccountId, XcmExtensions, NATIVE_FUNGIBLE_COLLECTION_ID,32};33use sp_runtime::traits::AccountIdConversion;34use sp_std::{boxed::Box, vec, vec::Vec};35use staging_xcm::{36 opaque::latest::{prelude::XcmError, Weight},37 v3::{prelude::*, MultiAsset, XcmContext},38};39use staging_xcm_executor::{40 traits::{ConvertLocation, Error as XcmExecutorError, TransactAsset, WeightTrader},41 Assets,42};43use up_data_structs::{44 budget::ZeroBudget, CollectionId, CollectionMode, CollectionName, CollectionTokenPrefix,45 CreateCollectionData, CreateFungibleData, CreateItemData, CreateNftData, Property, PropertyKey,46 TokenId,47};4849pub mod weights;5051#[cfg(feature = "runtime-benchmarks")]52mod benchmarking;5354pub use module::*;55pub use weights::WeightInfo;5657#[frame_support::pallet]58pub mod module {59 use up_data_structs::{60 CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,61 };6263 use super::*;6465 #[pallet::config]66 pub trait Config:67 frame_system::Config68 + pallet_common::Config69 + pallet_fungible::Config70 + pallet_balances::Config71 {72 /// The overarching event type.73 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;7475 /// Origin for force registering of a foreign asset.76 type ForceRegisterOrigin: EnsureOrigin<Self::RuntimeOrigin>;7778 /// The ID of the foreign assets pallet.79 type PalletId: Get<PalletId>;8081 /// Self-location of this parachain.82 type SelfLocation: Get<MultiLocation>;8384 /// The converter from a MultiLocation to a CrossAccountId.85 type LocationToAccountId: ConvertLocation<Self::CrossAccountId>;8687 /// Weight information for the extrinsics in this module.88 type WeightInfo: WeightInfo;89 }9091 #[pallet::error]92 pub enum Error<T> {93 /// The foreign asset is already registered.94 ForeignAssetAlreadyRegistered,95 }9697 #[pallet::event]98 #[pallet::generate_deposit(fn deposit_event)]99 pub enum Event<T: Config> {100 /// The foreign asset registered.101 ForeignAssetRegistered {102 collection_id: CollectionId,103 asset_id: Box<AssetId>,104 },105 }106107 /// The corresponding collections of foreign assets.108 #[pallet::storage]109 #[pallet::getter(fn foreign_asset_to_collection)]110 pub type ForeignAssetToCollection<T: Config> =111 StorageMap<_, Twox64Concat, staging_xcm::v3::AssetId, CollectionId, OptionQuery>;112113 /// The corresponding foreign assets of collections.114 #[pallet::storage]115 #[pallet::getter(fn collection_to_foreign_asset)]116 pub type CollectionToForeignAsset<T: Config> =117 StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::AssetId, OptionQuery>;118119 /// The correponding NFT token id of reserve NFTs120 #[pallet::storage]121 #[pallet::getter(fn foreign_reserve_asset_instance_to_token_id)]122 pub type ForeignReserveAssetInstanceToTokenId<T: Config> = StorageDoubleMap<123 Hasher1 = Twox64Concat,124 Key1 = CollectionId,125 Hasher2 = Twox64Concat,126 Key2 = staging_xcm::v3::AssetInstance,127 Value = TokenId,128 QueryKind = OptionQuery,129 >;130131 /// The correponding reserve NFT of a token ID132 #[pallet::storage]133 #[pallet::getter(fn token_id_to_foreign_reserve_asset_instance)]134 pub type TokenIdToForeignReserveAssetInstance<T: Config> = StorageDoubleMap<135 Hasher1 = Twox64Concat,136 Key1 = CollectionId,137 Hasher2 = Twox64Concat,138 Key2 = TokenId,139 Value = staging_xcm::v3::AssetInstance,140 QueryKind = OptionQuery,141 >;142143 #[pallet::pallet]144 pub struct Pallet<T>(_);145146 #[pallet::call]147 impl<T: Config> Pallet<T> {148 #[pallet::call_index(0)]149 #[pallet::weight(<T as Config>::WeightInfo::register_foreign_asset())]150 pub fn force_register_foreign_asset(151 origin: OriginFor<T>,152 asset_id: Box<AssetId>,153 name: CollectionName,154 token_prefix: CollectionTokenPrefix,155 mode: ForeignCollectionMode,156 ) -> DispatchResult {157 T::ForceRegisterOrigin::ensure_origin(origin.clone())?;158159 ensure!(160 !<ForeignAssetToCollection<T>>::contains_key(*asset_id),161 <Error<T>>::ForeignAssetAlreadyRegistered,162 );163164 let foreign_collection_owner = Self::pallet_account();165166 let description: CollectionDescription = "Foreign Assets Collection"167 .encode_utf16()168 .collect::<Vec<_>>()169 .try_into()170 .expect("description length < max description length; qed");171172 let payer = None;173 let is_special_collection = true;174 let collection_id = T::CollectionDispatch::create_raw(175 foreign_collection_owner,176 payer,177 is_special_collection,178 CreateCollectionData {179 name,180 token_prefix,181 description,182 mode: mode.into(),183184 properties: vec![Property {185 key: Self::reserve_location_property_key(),186 value: asset_id187 .encode()188 .try_into()189 .expect("multilocation is less than 32k; qed"),190 }]191 .try_into()192 .expect("just one property can always be stored; qed"),193194 token_property_permissions: vec![PropertyKeyPermission {195 key: Self::reserve_asset_instance_property_key(),196 permission: PropertyPermission {197 mutable: false,198 collection_admin: true,199 token_owner: false,200 },201 }]202 .try_into()203 .expect("just one property permission can always be stored; qed"),204 ..Default::default()205 },206 )?;207208 <ForeignAssetToCollection<T>>::insert(*asset_id, collection_id);209 <CollectionToForeignAsset<T>>::insert(collection_id, *asset_id);210211 Self::deposit_event(Event::<T>::ForeignAssetRegistered {212 collection_id,213 asset_id,214 });215216 Ok(())217 }218 }219}220221impl<T: Config> Pallet<T> {222 fn pallet_account() -> T::CrossAccountId {223 let owner: T::AccountId = T::PalletId::get().into_account_truncating();224 T::CrossAccountId::from_sub(owner)225 }226227 fn reserve_location_property_key() -> PropertyKey {228 b"reserve-location"229 .to_vec()230 .try_into()231 .expect("key length < max property key length; qed")232 }233234 fn reserve_asset_instance_property_key() -> PropertyKey {235 b"reserve-asset-instance"236 .to_vec()237 .try_into()238 .expect("key length < max property key length; qed")239 }240241 /// Converts a concrete asset ID (the asset multilocation) to a local collection on Unique Network.242 ///243 /// The multilocation corresponds to a local collection if:244 /// * It is `Here` location that corresponds to the native token of this parachain.245 /// * It is `../Parachain(<Unique Network Para ID>)` that also corresponds to the native token of this parachain.246 /// * It is `../Parachain(<Unique Network Para ID>)/GeneralIndex(<Collection ID>)` that corresponds247 /// to the collection with the ID equal to `<Collection ID>`. The `<Collection ID>` must be in the valid range,248 /// otherwise `None` is returned.249 /// * It is `GeneralIndex(<Collection ID>)`. Same as the last one above.250 ///251 /// If the multilocation doesn't match the patterns listed above,252 /// or the `<Collection ID>` points to a foreign collection,253 /// `None` is returned, identifying that the given multilocation doesn't correspond to a local collection.254 fn local_asset_id_to_collection(asset_id: &AssetId) -> Option<CollectionLocality> {255 let AssetId::Concrete(asset_location) = asset_id else {256 return None;257 };258259 let self_location = T::SelfLocation::get();260261 if *asset_location == Here.into() || *asset_location == self_location {262 return Some(CollectionLocality::Local(NATIVE_FUNGIBLE_COLLECTION_ID));263 }264265 let prefix = if asset_location.parents == 0 {266 &Here267 } else if asset_location.parents == self_location.parents {268 &self_location.interior269 } else {270 return None;271 };272273 let GeneralIndex(collection_id) = asset_location.interior.match_and_split(prefix)? else {274 return None;275 };276277 let collection_id = CollectionId((*collection_id).try_into().ok()?);278279 Self::collection_to_foreign_asset(collection_id)280 .is_none()281 .then_some(CollectionLocality::Local(collection_id))282 }283284 /// Converts an asset ID to a Unique Network's collection locality (either foreign or a local one).285 ///286 /// The function will check if the asset's reserve location has the corresponding287 /// foreign collection on Unique Network,288 /// and will return the "foreign" locality containing the collection ID if found.289 ///290 /// If no corresponding foreign collection is found, the function will check291 /// if the asset's reserve location corresponds to a local collection.292 /// If the local collection is found, the "local" locality with the collection ID is returned.293 ///294 /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned.295 fn asset_to_collection(asset_id: &AssetId) -> Result<CollectionLocality, XcmError> {296 Self::foreign_asset_to_collection(asset_id)297 .map(CollectionLocality::Foreign)298 .or_else(|| Self::local_asset_id_to_collection(asset_id))299 .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into())300 }301302 /// Converts an XCM asset instance of local collection to the Unique Network's token ID.303 ///304 /// The asset instance corresponds to the Unique Network's token ID if it is in the following format:305 /// `AssetInstance::Index(<token ID>)`.306 ///307 /// If the asset instance is not in the valid format or the `<token ID>` can't fit into the valid token ID,308 /// `None` will be returned.309 fn local_asset_instance_to_token_id(asset_instance: &AssetInstance) -> Option<TokenId> {310 match asset_instance {311 AssetInstance::Index(token_id) => Some(TokenId((*token_id).try_into().ok()?)),312 _ => None,313 }314 }315316 /// Obtains the token ID of the `asset_instance` in the collection.317 fn asset_instance_to_token_id(318 collection_locality: CollectionLocality,319 asset_instance: &AssetInstance,320 ) -> Option<TokenId> {321 match collection_locality {322 CollectionLocality::Local(_) => Self::local_asset_instance_to_token_id(asset_instance),323 CollectionLocality::Foreign(collection_id) => {324 Self::foreign_reserve_asset_instance_to_token_id(collection_id, asset_instance)325 }326 }327 }328329 /// Creates a foreign item in the the collection.330 fn create_foreign_asset_instance(331 xcm_ext: &dyn XcmExtensions<T>,332 collection_id: CollectionId,333 asset_instance: &AssetInstance,334 to: T::CrossAccountId,335 ) -> DispatchResult {336 let asset_instance_encoded = asset_instance.encode();337338 let derivative_token_id = xcm_ext.create_item(339 &Self::pallet_account(),340 to,341 CreateItemData::NFT(CreateNftData {342 properties: vec![Property {343 key: Self::reserve_asset_instance_property_key(),344 value: asset_instance_encoded345 .try_into()346 .expect("asset instance length <= 32 bytes which is less than value length limit; qed"),347 }]348 .try_into()349 .expect("just one property can always be stored; qed"),350 }),351 &ZeroBudget,352 )?;353354 <ForeignReserveAssetInstanceToTokenId<T>>::insert(355 collection_id,356 asset_instance,357 derivative_token_id,358 );359360 <TokenIdToForeignReserveAssetInstance<T>>::insert(361 collection_id,362 derivative_token_id,363 asset_instance,364 );365366 Ok(())367 }368369 /// Deposits an asset instance to the `to` account.370 ///371 /// Either transfers an existing item from the pallet's account372 /// or creates a foreign item.373 fn deposit_asset_instance(374 xcm_ext: &dyn XcmExtensions<T>,375 collection_locality: CollectionLocality,376 asset_instance: &AssetInstance,377 to: T::CrossAccountId,378 ) -> XcmResult {379 let token_id = Self::asset_instance_to_token_id(collection_locality, asset_instance);380381 let deposit_result = match (collection_locality, token_id) {382 (_, Some(token_id)) => {383 let depositor = &Self::pallet_account();384 let from = depositor;385 let amount = 1;386387 xcm_ext.transfer_item(depositor, from, &to, token_id, amount, &ZeroBudget)388 }389 (CollectionLocality::Foreign(collection_id), None) => {390 Self::create_foreign_asset_instance(xcm_ext, collection_id, asset_instance, to)391 }392 (CollectionLocality::Local(_), None) => {393 return Err(XcmError::AssetNotFound);394 }395 };396397 deposit_result398 .map_err(|_| XcmError::FailedToTransactAsset("non-fungible item deposit failed"))399 }400401 /// Withdraws an asset instance from the `from` account.402 ///403 /// Transfers the asset instance to the pallet's account.404 fn withdraw_asset_instance(405 xcm_ext: &dyn XcmExtensions<T>,406 collection_locality: CollectionLocality,407 asset_instance: &AssetInstance,408 from: T::CrossAccountId,409 ) -> XcmResult {410 let token_id = Self::asset_instance_to_token_id(collection_locality, asset_instance)411 .ok_or(XcmError::AssetNotFound)?;412413 let depositor = &from;414 let to = Self::pallet_account();415 let amount = 1;416 xcm_ext417 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)418 .map_err(|_| XcmError::FailedToTransactAsset("non-fungible item withdraw failed"))?;419420 Ok(())421 }422}423424impl<T: Config> TransactAsset for Pallet<T> {425 fn can_check_in(426 _origin: &MultiLocation,427 _what: &MultiAsset,428 _context: &XcmContext,429 ) -> XcmResult {430 Err(XcmError::Unimplemented)431 }432433 fn check_in(_origin: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}434435 fn can_check_out(436 _dest: &MultiLocation,437 _what: &MultiAsset,438 _context: &XcmContext,439 ) -> XcmResult {440 Err(XcmError::Unimplemented)441 }442443 fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}444445 fn deposit_asset(what: &MultiAsset, to: &MultiLocation, _context: &XcmContext) -> XcmResult {446 let to = T::LocationToAccountId::convert_location(to)447 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;448449 let collection_locality = Self::asset_to_collection(&what.id)?;450 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)451 .map_err(|_| XcmError::AssetNotFound)?;452453 let collection = dispatch.as_dyn();454 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::Unimplemented)?;455456 match what.fun {457 Fungibility::Fungible(amount) => xcm_ext458 .create_item(459 &Self::pallet_account(),460 to,461 CreateItemData::Fungible(CreateFungibleData { value: amount }),462 &ZeroBudget,463 )464 .map(|_| ())465 .map_err(|_| XcmError::FailedToTransactAsset("fungible item deposit failed")),466467 Fungibility::NonFungible(asset_instance) => {468 Self::deposit_asset_instance(xcm_ext, collection_locality, &asset_instance, to)469 }470 }471 }472473 fn withdraw_asset(474 what: &MultiAsset,475 from: &MultiLocation,476 _maybe_context: Option<&XcmContext>,477 ) -> Result<staging_xcm_executor::Assets, XcmError> {478 let from = T::LocationToAccountId::convert_location(from)479 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;480481 let collection_locality = Self::asset_to_collection(&what.id)?;482 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)483 .map_err(|_| XcmError::AssetNotFound)?;484485 let collection = dispatch.as_dyn();486 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;487488 match what.fun {489 Fungibility::Fungible(amount) => xcm_ext490 .burn_item(from, TokenId::default(), amount)491 .map_err(|_| XcmError::FailedToTransactAsset("fungible item withdraw failed"))?,492493 Fungibility::NonFungible(asset_instance) => {494 Self::withdraw_asset_instance(xcm_ext, collection_locality, &asset_instance, from)?;495 }496 }497498 Ok(what.clone().into())499 }500501 fn internal_transfer_asset(502 what: &MultiAsset,503 from: &MultiLocation,504 to: &MultiLocation,505 _context: &XcmContext,506 ) -> Result<staging_xcm_executor::Assets, XcmError> {507 let from = T::LocationToAccountId::convert_location(from)508 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;509510 let to = T::LocationToAccountId::convert_location(to)511 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;512513 let collection_locality = Self::asset_to_collection(&what.id)?;514515 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)516 .map_err(|_| XcmError::AssetNotFound)?;517 let collection = dispatch.as_dyn();518 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;519520 let depositor = &from;521522 let token_id;523 let amount;524 let map_error: fn(DispatchError) -> XcmError;525526 match what.fun {527 Fungibility::Fungible(fungible_amount) => {528 token_id = TokenId::default();529 amount = fungible_amount;530 map_error = |_| XcmError::FailedToTransactAsset("fungible item transfer failed");531 }532533 Fungibility::NonFungible(asset_instance) => {534 token_id = Self::asset_instance_to_token_id(collection_locality, &asset_instance)535 .ok_or(XcmError::AssetNotFound)?;536537 amount = 1;538 map_error = |_| XcmError::FailedToTransactAsset("non-fungible item transfer failed")539 }540 }541542 xcm_ext543 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)544 .map_err(map_error)?;545546 Ok(what.clone().into())547 }548}549550#[derive(Clone, Copy)]551pub enum CollectionLocality {552 Local(CollectionId),553 Foreign(CollectionId),554}555556impl Deref for CollectionLocality {557 type Target = CollectionId;558559 fn deref(&self) -> &Self::Target {560 match self {561 Self::Local(id) => id,562 Self::Foreign(id) => id,563 }564 }565}566567pub struct CurrencyIdConvert<T: Config>(PhantomData<T>);568impl<T: Config> sp_runtime::traits::Convert<CollectionId, Option<MultiLocation>>569 for CurrencyIdConvert<T>570{571 fn convert(collection_id: CollectionId) -> Option<MultiLocation> {572 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {573 Some(T::SelfLocation::get())574 } else {575 <Pallet<T>>::collection_to_foreign_asset(collection_id)576 .and_then(|asset_id| match asset_id {577 AssetId::Concrete(location) => Some(location),578 _ => None,579 })580 .or_else(|| {581 T::SelfLocation::get()582 .pushed_with_interior(GeneralIndex(collection_id.0.into()))583 .ok()584 })585 }586 }587}588589#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]590pub enum ForeignCollectionMode {591 NFT,592 Fungible(u8),593}594595impl From<ForeignCollectionMode> for CollectionMode {596 fn from(value: ForeignCollectionMode) -> Self {597 match value {598 ForeignCollectionMode::NFT => Self::NFT,599 ForeignCollectionMode::Fungible(decimals) => Self::Fungible(decimals),600 }601 }602}603604pub struct FreeForAll;605606impl WeightTrader for FreeForAll {607 fn new() -> Self {608 Self609 }610611 fn buy_weight(612 &mut self,613 weight: Weight,614 payment: Assets,615 _xcm: &XcmContext,616 ) -> Result<Assets, XcmError> {617 log::trace!(target: "fassets::weight", "buy_weight weight: {:?}, payment: {:?}", weight, payment);618 Ok(payment)619 }620}1// Copyright 2019-2022 Unique Network (Gibraltar) Ltd.2// This file is part of Unique Network.34// Unique Network is free software: you can redistribute it and/or modify5// it under the terms of the GNU General Public License as published by6// the Free Software Foundation, either version 3 of the License, or7// (at your option) any later version.89// Unique Network is distributed in the hope that it will be useful,10// but WITHOUT ANY WARRANTY; without even the implied warranty of11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12// GNU General Public License for more details.1314// You should have received a copy of the GNU General Public License15// along with Unique Network. If not, see <http://www.gnu.org/licenses/>.1617//! # Foreign Assets18//!19//! ## Overview20//!21//! The Foreign Assets is a proxy that maps XCM operations to the Unique Network's pallets logic.2223#![cfg_attr(not(feature = "std"), no_std)]24#![allow(clippy::unused_unit)]2526use core::ops::Deref;2728use frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin, PalletId};29use frame_system::pallet_prelude::*;30use pallet_common::{31 dispatch::CollectionDispatch, erc::CrossAccountId, XcmExtensions, NATIVE_FUNGIBLE_COLLECTION_ID,32};33use sp_runtime::traits::AccountIdConversion;34use sp_std::{boxed::Box, vec, vec::Vec};35use staging_xcm::{36 opaque::latest::{prelude::XcmError, Weight},37 v3::{prelude::*, MultiAsset, XcmContext},38};39use staging_xcm_executor::{40 traits::{ConvertLocation, Error as XcmExecutorError, TransactAsset, WeightTrader},41 Assets,42};43use up_data_structs::{44 budget::ZeroBudget, CollectionId, CollectionMode, CollectionName, CollectionTokenPrefix,45 CreateCollectionData, CreateFungibleData, CreateItemData, CreateNftData, Property, PropertyKey,46 TokenId,47};4849pub mod weights;5051#[cfg(feature = "runtime-benchmarks")]52mod benchmarking;5354pub use module::*;55pub use weights::WeightInfo;5657#[frame_support::pallet]58pub mod module {59 use up_data_structs::{60 CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,61 };6263 use super::*;6465 #[pallet::config]66 pub trait Config:67 frame_system::Config68 + pallet_common::Config69 + pallet_fungible::Config70 + pallet_balances::Config71 {72 /// The overarching event type.73 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;7475 /// Origin for force registering of a foreign asset.76 type ForceRegisterOrigin: EnsureOrigin<Self::RuntimeOrigin>;7778 /// The ID of the foreign assets pallet.79 type PalletId: Get<PalletId>;8081 /// Self-location of this parachain.82 type SelfLocation: Get<MultiLocation>;8384 /// The converter from a MultiLocation to a CrossAccountId.85 type LocationToAccountId: ConvertLocation<Self::CrossAccountId>;8687 /// Weight information for the extrinsics in this module.88 type WeightInfo: WeightInfo;89 }9091 #[pallet::error]92 pub enum Error<T> {93 /// The foreign asset is already registered.94 ForeignAssetAlreadyRegistered,95 }9697 #[pallet::event]98 #[pallet::generate_deposit(fn deposit_event)]99 pub enum Event<T: Config> {100 /// The foreign asset registered.101 ForeignAssetRegistered {102 collection_id: CollectionId,103 asset_id: Box<AssetId>,104 },105 }106107 /// The corresponding collections of foreign assets.108 #[pallet::storage]109 #[pallet::getter(fn foreign_asset_to_collection)]110 pub type ForeignAssetToCollection<T: Config> =111 StorageMap<_, Twox64Concat, staging_xcm::v3::AssetId, CollectionId, OptionQuery>;112113 /// The corresponding foreign assets of collections.114 #[pallet::storage]115 #[pallet::getter(fn collection_to_foreign_asset)]116 pub type CollectionToForeignAsset<T: Config> =117 StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::AssetId, OptionQuery>;118119 /// The correponding NFT token id of reserve NFTs120 #[pallet::storage]121 #[pallet::getter(fn foreign_reserve_asset_instance_to_token_id)]122 pub type ForeignReserveAssetInstanceToTokenId<T: Config> = StorageDoubleMap<123 Hasher1 = Twox64Concat,124 Key1 = CollectionId,125 Hasher2 = Twox64Concat,126 Key2 = staging_xcm::v3::AssetInstance,127 Value = TokenId,128 QueryKind = OptionQuery,129 >;130131 /// The correponding reserve NFT of a token ID132 #[pallet::storage]133 #[pallet::getter(fn token_id_to_foreign_reserve_asset_instance)]134 pub type TokenIdToForeignReserveAssetInstance<T: Config> = StorageDoubleMap<135 Hasher1 = Twox64Concat,136 Key1 = CollectionId,137 Hasher2 = Twox64Concat,138 Key2 = TokenId,139 Value = staging_xcm::v3::AssetInstance,140 QueryKind = OptionQuery,141 >;142143 #[pallet::pallet]144 pub struct Pallet<T>(_);145146 #[pallet::call]147 impl<T: Config> Pallet<T> {148 #[pallet::call_index(0)]149 #[pallet::weight(<T as Config>::WeightInfo::register_foreign_asset())]150 pub fn force_register_foreign_asset(151 origin: OriginFor<T>,152 asset_id: Box<AssetId>,153 name: CollectionName,154 token_prefix: CollectionTokenPrefix,155 mode: ForeignCollectionMode,156 ) -> DispatchResult {157 T::ForceRegisterOrigin::ensure_origin(origin.clone())?;158159 ensure!(160 !<ForeignAssetToCollection<T>>::contains_key(*asset_id),161 <Error<T>>::ForeignAssetAlreadyRegistered,162 );163164 let foreign_collection_owner = Self::pallet_account();165166 let description: CollectionDescription = "Foreign Assets Collection"167 .encode_utf16()168 .collect::<Vec<_>>()169 .try_into()170 .expect("description length < max description length; qed");171172 let payer = None;173 let is_special_collection = true;174 let collection_id = T::CollectionDispatch::create_raw(175 foreign_collection_owner,176 payer,177 is_special_collection,178 CreateCollectionData {179 name,180 token_prefix,181 description,182 mode: mode.into(),183184 properties: vec![Property {185 key: Self::reserve_location_property_key(),186 value: asset_id187 .encode()188 .try_into()189 .expect("multilocation is less than 32k; qed"),190 }]191 .try_into()192 .expect("just one property can always be stored; qed"),193194 token_property_permissions: vec![PropertyKeyPermission {195 key: Self::reserve_asset_instance_property_key(),196 permission: PropertyPermission {197 mutable: false,198 collection_admin: true,199 token_owner: false,200 },201 }]202 .try_into()203 .expect("just one property permission can always be stored; qed"),204 ..Default::default()205 },206 )?;207208 <ForeignAssetToCollection<T>>::insert(*asset_id, collection_id);209 <CollectionToForeignAsset<T>>::insert(collection_id, *asset_id);210211 Self::deposit_event(Event::<T>::ForeignAssetRegistered {212 collection_id,213 asset_id,214 });215216 Ok(())217 }218 }219}220221impl<T: Config> Pallet<T> {222 fn pallet_account() -> T::CrossAccountId {223 let owner: T::AccountId = T::PalletId::get().into_account_truncating();224 T::CrossAccountId::from_sub(owner)225 }226227 fn reserve_location_property_key() -> PropertyKey {228 b"reserve-location"229 .to_vec()230 .try_into()231 .expect("key length < max property key length; qed")232 }233234 fn reserve_asset_instance_property_key() -> PropertyKey {235 b"reserve-asset-instance"236 .to_vec()237 .try_into()238 .expect("key length < max property key length; qed")239 }240241 /// Converts a concrete asset ID (the asset multilocation) to a local collection on Unique Network.242 ///243 /// The multilocation corresponds to a local collection if:244 /// * It is `Here` location that corresponds to the native token of this parachain.245 /// * It is `../Parachain(<Unique Network Para ID>)` that also corresponds to the native token of this parachain.246 /// * It is `../Parachain(<Unique Network Para ID>)/GeneralIndex(<Collection ID>)` that corresponds247 /// to the collection with the ID equal to `<Collection ID>`. The `<Collection ID>` must be in the valid range,248 /// otherwise `None` is returned.249 /// * It is `GeneralIndex(<Collection ID>)`. Same as the last one above.250 ///251 /// If the multilocation doesn't match the patterns listed above,252 /// or the `<Collection ID>` points to a foreign collection,253 /// `None` is returned, identifying that the given multilocation doesn't correspond to a local collection.254 fn local_asset_id_to_collection(asset_id: &AssetId) -> Option<CollectionLocality> {255 let AssetId::Concrete(asset_location) = asset_id else {256 return None;257 };258259 let self_location = T::SelfLocation::get();260261 if *asset_location == Here.into() || *asset_location == self_location {262 return Some(CollectionLocality::Local(NATIVE_FUNGIBLE_COLLECTION_ID));263 }264265 let prefix = if asset_location.parents == 0 {266 &Here267 } else if asset_location.parents == self_location.parents {268 &self_location.interior269 } else {270 return None;271 };272273 let GeneralIndex(collection_id) = asset_location.interior.match_and_split(prefix)? else {274 return None;275 };276277 let collection_id = CollectionId((*collection_id).try_into().ok()?);278279 Self::collection_to_foreign_asset(collection_id)280 .is_none()281 .then_some(CollectionLocality::Local(collection_id))282 }283284 /// Converts an asset ID to a Unique Network's collection locality (either foreign or a local one).285 ///286 /// The function will check if the asset's reserve location has the corresponding287 /// foreign collection on Unique Network,288 /// and will return the "foreign" locality containing the collection ID if found.289 ///290 /// If no corresponding foreign collection is found, the function will check291 /// if the asset's reserve location corresponds to a local collection.292 /// If the local collection is found, the "local" locality with the collection ID is returned.293 ///294 /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned.295 fn asset_to_collection(asset_id: &AssetId) -> Result<CollectionLocality, XcmError> {296 Self::foreign_asset_to_collection(asset_id)297 .map(CollectionLocality::Foreign)298 .or_else(|| Self::local_asset_id_to_collection(asset_id))299 .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into())300 }301302 /// Converts an XCM asset instance of local collection to the Unique Network's token ID.303 ///304 /// The asset instance corresponds to the Unique Network's token ID if it is in the following format:305 /// `AssetInstance::Index(<token ID>)`.306 ///307 /// If the asset instance is not in the valid format or the `<token ID>` can't fit into the valid token ID,308 /// `None` will be returned.309 fn local_asset_instance_to_token_id(asset_instance: &AssetInstance) -> Option<TokenId> {310 match asset_instance {311 AssetInstance::Index(token_id) => Some(TokenId((*token_id).try_into().ok()?)),312 _ => None,313 }314 }315316 /// Obtains the token ID of the `asset_instance` in the collection.317 fn asset_instance_to_token_id(318 collection_locality: CollectionLocality,319 asset_instance: &AssetInstance,320 ) -> Option<TokenId> {321 match collection_locality {322 CollectionLocality::Local(_) => Self::local_asset_instance_to_token_id(asset_instance),323 CollectionLocality::Foreign(collection_id) => {324 Self::foreign_reserve_asset_instance_to_token_id(collection_id, asset_instance)325 }326 }327 }328329 /// Creates a foreign item in the the collection.330 fn create_foreign_asset_instance(331 xcm_ext: &dyn XcmExtensions<T>,332 collection_id: CollectionId,333 asset_instance: &AssetInstance,334 to: T::CrossAccountId,335 ) -> DispatchResult {336 let asset_instance_encoded = asset_instance.encode();337338 let derivative_token_id = xcm_ext.create_item(339 &Self::pallet_account(),340 to,341 CreateItemData::NFT(CreateNftData {342 properties: vec![Property {343 key: Self::reserve_asset_instance_property_key(),344 value: asset_instance_encoded345 .try_into()346 .expect("asset instance length <= 32 bytes which is less than value length limit; qed"),347 }]348 .try_into()349 .expect("just one property can always be stored; qed"),350 }),351 &ZeroBudget,352 )?;353354 <ForeignReserveAssetInstanceToTokenId<T>>::insert(355 collection_id,356 asset_instance,357 derivative_token_id,358 );359360 <TokenIdToForeignReserveAssetInstance<T>>::insert(361 collection_id,362 derivative_token_id,363 asset_instance,364 );365366 Ok(())367 }368369 /// Deposits an asset instance to the `to` account.370 ///371 /// Either transfers an existing item from the pallet's account372 /// or creates a foreign item.373 fn deposit_asset_instance(374 xcm_ext: &dyn XcmExtensions<T>,375 collection_locality: CollectionLocality,376 asset_instance: &AssetInstance,377 to: T::CrossAccountId,378 ) -> XcmResult {379 let token_id = Self::asset_instance_to_token_id(collection_locality, asset_instance);380381 let deposit_result = match (collection_locality, token_id) {382 (_, Some(token_id)) => {383 let depositor = &Self::pallet_account();384 let from = depositor;385 let amount = 1;386387 xcm_ext.transfer_item(depositor, from, &to, token_id, amount, &ZeroBudget)388 }389 (CollectionLocality::Foreign(collection_id), None) => {390 Self::create_foreign_asset_instance(xcm_ext, collection_id, asset_instance, to)391 }392 (CollectionLocality::Local(_), None) => {393 return Err(XcmError::AssetNotFound);394 }395 };396397 deposit_result398 .map_err(|_| XcmError::FailedToTransactAsset("non-fungible item deposit failed"))399 }400401 /// Withdraws an asset instance from the `from` account.402 ///403 /// Transfers the asset instance to the pallet's account.404 fn withdraw_asset_instance(405 xcm_ext: &dyn XcmExtensions<T>,406 collection_locality: CollectionLocality,407 asset_instance: &AssetInstance,408 from: T::CrossAccountId,409 ) -> XcmResult {410 let token_id = Self::asset_instance_to_token_id(collection_locality, asset_instance)411 .ok_or(XcmError::AssetNotFound)?;412413 let depositor = &from;414 let to = Self::pallet_account();415 let amount = 1;416 xcm_ext417 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)418 .map_err(|_| XcmError::FailedToTransactAsset("non-fungible item withdraw failed"))?;419420 Ok(())421 }422}423424impl<T: Config> TransactAsset for Pallet<T> {425 fn can_check_in(426 _origin: &MultiLocation,427 _what: &MultiAsset,428 _context: &XcmContext,429 ) -> XcmResult {430 Err(XcmError::Unimplemented)431 }432433 fn check_in(_origin: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}434435 fn can_check_out(436 _dest: &MultiLocation,437 _what: &MultiAsset,438 _context: &XcmContext,439 ) -> XcmResult {440 Err(XcmError::Unimplemented)441 }442443 fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}444445 fn deposit_asset(446 what: &MultiAsset,447 to: &MultiLocation,448 _context: Option<&XcmContext>,449 ) -> XcmResult {450 let to = T::LocationToAccountId::convert_location(to)451 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;452453 let collection_locality = Self::asset_to_collection(&what.id)?;454 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)455 .map_err(|_| XcmError::AssetNotFound)?;456457 let collection = dispatch.as_dyn();458 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::Unimplemented)?;459460 match what.fun {461 Fungibility::Fungible(amount) => xcm_ext462 .create_item(463 &Self::pallet_account(),464 to,465 CreateItemData::Fungible(CreateFungibleData { value: amount }),466 &ZeroBudget,467 )468 .map(|_| ())469 .map_err(|_| XcmError::FailedToTransactAsset("fungible item deposit failed")),470471 Fungibility::NonFungible(asset_instance) => {472 Self::deposit_asset_instance(xcm_ext, collection_locality, &asset_instance, to)473 }474 }475 }476477 fn withdraw_asset(478 what: &MultiAsset,479 from: &MultiLocation,480 _maybe_context: Option<&XcmContext>,481 ) -> Result<staging_xcm_executor::Assets, XcmError> {482 let from = T::LocationToAccountId::convert_location(from)483 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;484485 let collection_locality = Self::asset_to_collection(&what.id)?;486 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)487 .map_err(|_| XcmError::AssetNotFound)?;488489 let collection = dispatch.as_dyn();490 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;491492 match what.fun {493 Fungibility::Fungible(amount) => xcm_ext494 .burn_item(from, TokenId::default(), amount)495 .map_err(|_| XcmError::FailedToTransactAsset("fungible item withdraw failed"))?,496497 Fungibility::NonFungible(asset_instance) => {498 Self::withdraw_asset_instance(xcm_ext, collection_locality, &asset_instance, from)?;499 }500 }501502 Ok(what.clone().into())503 }504505 fn internal_transfer_asset(506 what: &MultiAsset,507 from: &MultiLocation,508 to: &MultiLocation,509 _context: &XcmContext,510 ) -> Result<staging_xcm_executor::Assets, XcmError> {511 let from = T::LocationToAccountId::convert_location(from)512 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;513514 let to = T::LocationToAccountId::convert_location(to)515 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;516517 let collection_locality = Self::asset_to_collection(&what.id)?;518519 let dispatch = T::CollectionDispatch::dispatch(*collection_locality)520 .map_err(|_| XcmError::AssetNotFound)?;521 let collection = dispatch.as_dyn();522 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;523524 let depositor = &from;525526 let token_id;527 let amount;528 let map_error: fn(DispatchError) -> XcmError;529530 match what.fun {531 Fungibility::Fungible(fungible_amount) => {532 token_id = TokenId::default();533 amount = fungible_amount;534 map_error = |_| XcmError::FailedToTransactAsset("fungible item transfer failed");535 }536537 Fungibility::NonFungible(asset_instance) => {538 token_id = Self::asset_instance_to_token_id(collection_locality, &asset_instance)539 .ok_or(XcmError::AssetNotFound)?;540541 amount = 1;542 map_error = |_| XcmError::FailedToTransactAsset("non-fungible item transfer failed")543 }544 }545546 xcm_ext547 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)548 .map_err(map_error)?;549550 Ok(what.clone().into())551 }552}553554#[derive(Clone, Copy)]555pub enum CollectionLocality {556 Local(CollectionId),557 Foreign(CollectionId),558}559560impl Deref for CollectionLocality {561 type Target = CollectionId;562563 fn deref(&self) -> &Self::Target {564 match self {565 Self::Local(id) => id,566 Self::Foreign(id) => id,567 }568 }569}570571pub struct CurrencyIdConvert<T: Config>(PhantomData<T>);572impl<T: Config> sp_runtime::traits::Convert<CollectionId, Option<MultiLocation>>573 for CurrencyIdConvert<T>574{575 fn convert(collection_id: CollectionId) -> Option<MultiLocation> {576 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {577 Some(T::SelfLocation::get())578 } else {579 <Pallet<T>>::collection_to_foreign_asset(collection_id)580 .and_then(|asset_id| match asset_id {581 AssetId::Concrete(location) => Some(location),582 _ => None,583 })584 .or_else(|| {585 T::SelfLocation::get()586 .pushed_with_interior(GeneralIndex(collection_id.0.into()))587 .ok()588 })589 }590 }591}592593#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]594pub enum ForeignCollectionMode {595 NFT,596 Fungible(u8),597}598599impl From<ForeignCollectionMode> for CollectionMode {600 fn from(value: ForeignCollectionMode) -> Self {601 match value {602 ForeignCollectionMode::NFT => Self::NFT,603 ForeignCollectionMode::Fungible(decimals) => Self::Fungible(decimals),604 }605 }606}607608pub struct FreeForAll;609610impl WeightTrader for FreeForAll {611 fn new() -> Self {612 Self613 }614615 fn buy_weight(616 &mut self,617 weight: Weight,618 payment: Assets,619 _xcm: &XcmContext,620 ) -> Result<Assets, XcmError> {621 log::trace!(target: "fassets::weight", "buy_weight weight: {:?}, payment: {:?}", weight, payment);622 Ok(payment)623 }624}