difftreelog
fix use the term local instead of native in local_asset_location_to_collection
in: master
1 file changed
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 frame_support::{dispatch::DispatchResult, pallet_prelude::*, traits::EnsureOrigin, PalletId};27use frame_system::pallet_prelude::*;28use pallet_common::{29 dispatch::CollectionDispatch, erc::CrossAccountId, XcmExtensions, NATIVE_FUNGIBLE_COLLECTION_ID,30};31use sp_runtime::traits::AccountIdConversion;32use sp_std::{boxed::Box, vec, vec::Vec};33use staging_xcm::{34 opaque::latest::{prelude::XcmError, Weight},35 v3::{prelude::*, MultiAsset, XcmContext},36};37use staging_xcm_executor::{38 traits::{ConvertLocation, Error as XcmExecutorError, TransactAsset, WeightTrader},39 Assets,40};41use up_data_structs::{42 budget::ZeroBudget, CollectionId, CollectionMode, CollectionName, CollectionTokenPrefix,43 CreateCollectionData, CreateFungibleData, CreateItemData, CreateNftData, Property, PropertyKey,44 TokenId,45};4647pub mod weights;4849#[cfg(feature = "runtime-benchmarks")]50mod benchmarking;5152pub use module::*;53pub use weights::WeightInfo;5455#[frame_support::pallet]56pub mod module {57 use up_data_structs::{58 CollectionDescription, Property, PropertyKeyPermission, PropertyPermission,59 };6061 use super::*;6263 #[pallet::config]64 pub trait Config:65 frame_system::Config66 + pallet_common::Config67 + pallet_fungible::Config68 + pallet_balances::Config69 {70 /// The overarching event type.71 type RuntimeEvent: From<Event<Self>> + IsType<<Self as frame_system::Config>::RuntimeEvent>;7273 /// Origin for force registering of a foreign asset.74 type ForceRegisterOrigin: EnsureOrigin<Self::RuntimeOrigin>;7576 /// The ID of the foreign assets pallet.77 type PalletId: Get<PalletId>;7879 /// Self-location of this parachain.80 type SelfLocation: Get<MultiLocation>;8182 /// The converter from a MultiLocation to a CrossAccountId.83 type LocationToAccountId: ConvertLocation<Self::CrossAccountId>;8485 /// Weight information for the extrinsics in this module.86 type WeightInfo: WeightInfo;87 }8889 #[pallet::error]90 pub enum Error<T> {91 /// The foreign asset is already registered.92 ForeignAssetAlreadyRegistered,93 }9495 #[pallet::event]96 #[pallet::generate_deposit(fn deposit_event)]97 pub enum Event<T: Config> {98 /// The foreign asset registered.99 ForeignAssetRegistered {100 asset_id: CollectionId,101 reserve_location: Box<MultiLocation>,102 },103 }104105 /// The corresponding collections of reserve locations.106 #[pallet::storage]107 #[pallet::getter(fn foreign_reserve_location_to_collection)]108 pub type ForeignReserveLocationToCollection<T: Config> =109 StorageMap<_, Twox64Concat, staging_xcm::v3::MultiLocation, CollectionId, OptionQuery>;110111 /// The corresponding reserve location of collections.112 #[pallet::storage]113 #[pallet::getter(fn collection_to_foreign_reserve_location)]114 pub type CollectionToForeignReserveLocation<T: Config> =115 StorageMap<_, Twox64Concat, CollectionId, staging_xcm::v3::MultiLocation, OptionQuery>;116117 /// The correponding NFT token id of reserve NFTs118 #[pallet::storage]119 #[pallet::getter(fn foreign_reserve_asset_instance_to_token_id)]120 pub type ForeignReserveAssetInstanceToTokenId<T: Config> = StorageDoubleMap<121 Hasher1 = Twox64Concat,122 Key1 = CollectionId,123 Hasher2 = Twox64Concat,124 Key2 = staging_xcm::v3::AssetInstance,125 Value = TokenId,126 QueryKind = OptionQuery,127 >;128129 #[pallet::pallet]130 pub struct Pallet<T>(_);131132 #[pallet::call]133 impl<T: Config> Pallet<T> {134 #[pallet::call_index(0)]135 #[pallet::weight(<T as Config>::WeightInfo::register_foreign_asset())]136 pub fn force_register_foreign_asset(137 origin: OriginFor<T>,138 reserve_location: Box<MultiLocation>,139 name: CollectionName,140 token_prefix: CollectionTokenPrefix,141 mode: ForeignCollectionMode,142 ) -> DispatchResult {143 T::ForceRegisterOrigin::ensure_origin(origin.clone())?;144145 if <ForeignReserveLocationToCollection<T>>::contains_key(*reserve_location) {146 return Err(<Error<T>>::ForeignAssetAlreadyRegistered.into());147 }148149 let foreign_collection_owner = Self::pallet_account();150151 let description: CollectionDescription = "Foreign Assets Collection"152 .encode_utf16()153 .collect::<Vec<_>>()154 .try_into()155 .expect("description length < max description length; qed");156157 let payer = None;158 let is_special_collection = true;159 let collection_id = T::CollectionDispatch::create_raw(160 foreign_collection_owner,161 payer,162 is_special_collection,163 CreateCollectionData {164 name,165 token_prefix,166 description,167 mode: mode.into(),168169 properties: vec![Property {170 key: Self::reserve_location_property_key(),171 value: reserve_location172 .encode()173 .try_into()174 .expect("multilocation is less than 32k; qed"),175 }]176 .try_into()177 .expect("just one property can always be stored; qed"),178179 token_property_permissions: vec![PropertyKeyPermission {180 key: Self::reserve_asset_instance_property_key(),181 permission: PropertyPermission {182 mutable: false,183 collection_admin: true,184 token_owner: false,185 },186 }]187 .try_into()188 .expect("just one property permission can always be stored; qed"),189 ..Default::default()190 },191 )?;192193 <ForeignReserveLocationToCollection<T>>::insert(*reserve_location, collection_id);194 <CollectionToForeignReserveLocation<T>>::insert(collection_id, *reserve_location);195196 Self::deposit_event(Event::<T>::ForeignAssetRegistered {197 asset_id: collection_id,198 reserve_location,199 });200201 Ok(())202 }203 }204}205206impl<T: Config> Pallet<T> {207 fn pallet_account() -> T::CrossAccountId {208 let owner: T::AccountId = T::PalletId::get().into_account_truncating();209 T::CrossAccountId::from_sub(owner)210 }211212 fn reserve_location_property_key() -> PropertyKey {213 b"reserve-location"214 .to_vec()215 .try_into()216 .expect("key length < max property key length; qed")217 }218219 fn reserve_asset_instance_property_key() -> PropertyKey {220 b"reserve-asset-instance"221 .to_vec()222 .try_into()223 .expect("key length < max property key length; qed")224 }225226 /// Converts a multilocation to a local collection on Unique Network.227 ///228 /// The multilocation corresponds to a local collection if:229 /// * It is `Here` location that corresponds to the native token of this parachain.230 /// * It is `../Parachain(<Unique Network Para ID>)` that also corresponds to the native token of this parachain.231 /// * It is `../Parachain(<Unique Network Para ID>)/GeneralIndex(<Collection ID>)` that corresponds232 /// to the collection with the ID equal to `<Collection ID>`. The `<Collection ID>` must be in the valid range,233 /// otherwise `None` is returned.234 ///235 /// If the multilocation doesn't match the patterns listed above,236 /// or the `<Collection ID>` points to a foreign collection,237 /// `None` is returned, identifying that the given multilocation doesn't correspond to a native collection.238 fn local_asset_location_to_collection(asset_location: &MultiLocation) -> Option<CollectionId> {239 let self_location = T::SelfLocation::get();240241 if *asset_location == Here.into() || *asset_location == self_location {242 Some(NATIVE_FUNGIBLE_COLLECTION_ID)243 } else if asset_location.parents == self_location.parents {244 match asset_location245 .interior246 .match_and_split(&self_location.interior)247 {248 Some(GeneralIndex(collection_id)) => {249 let collection_id = CollectionId((*collection_id).try_into().ok()?);250251 Self::collection_to_foreign_reserve_location(collection_id)252 .is_none()253 .then_some(collection_id)254 }255 _ => None,256 }257 } else {258 None259 }260 }261262 /// Converts an asset ID to a Unique Network's collection (either foreign or a local one).263 ///264 /// The function will check if the asset's reserve location has the corresponding265 /// foreign collection on Unique Network, and will return the collection ID if found.266 ///267 /// If no corresponding foreign collection is found, the function will check268 /// if the asset's reserve location corresponds to a local collection.269 /// If the local collection is found, its ID is returned.270 ///271 /// If all of the above have failed, the `AssetIdConversionFailed` error will be returned.272 fn asset_to_collection(asset_id: &AssetId) -> Result<CollectionId, XcmError> {273 let AssetId::Concrete(asset_reserve_location) = asset_id else {274 return Err(XcmExecutorError::AssetNotHandled.into());275 };276277 Self::foreign_reserve_location_to_collection(asset_reserve_location)278 .or_else(|| Self::local_asset_location_to_collection(asset_reserve_location))279 .ok_or_else(|| XcmExecutorError::AssetIdConversionFailed.into())280 }281282 /// Converts an XCM asset instance to the Unique Network's token ID.283 ///284 /// The asset instance corresponds to the Unique Network's token ID if it is in the following format:285 /// `AssetInstance::Index(<token ID>)`.286 ///287 /// If the asset instance is not in the valid format or the `<token ID>` can't fit into the valid token ID,288 /// the `AssetNotFound` error will be returned.289 fn native_asset_instance_to_token_id(290 asset_instance: &AssetInstance,291 ) -> Result<TokenId, XcmError> {292 match asset_instance {293 AssetInstance::Index(token_id) => Ok(TokenId(294 (*token_id)295 .try_into()296 .map_err(|_| XcmError::AssetNotFound)?,297 )),298 _ => Err(XcmError::AssetNotFound),299 }300 }301302 /// Obtains the token ID of the `asset_instance` in the collection.303 ///304 /// Returns `Ok(None)` only if the `asset_instance` is a part of a foreign collection305 /// and the item hasn't yet been created on this blockchain.306 ///307 /// If the `asset_instance` exists and it is a part of a foreign collection, the function will return `Ok(Some(<token ID>))`.308 ///309 /// If the `asset_instance` is a part of a local collection,310 /// the function will return either `Ok(Some(<token ID>))` or an error if the token is not found.311 fn asset_instance_to_token_id(312 collection_id: CollectionId,313 asset_instance: &AssetInstance,314 ) -> Result<Option<TokenId>, XcmError> {315 if <CollectionToForeignReserveLocation<T>>::contains_key(collection_id) {316 Ok(Self::foreign_reserve_asset_instance_to_token_id(317 collection_id,318 asset_instance,319 ))320 } else {321 Self::native_asset_instance_to_token_id(asset_instance).map(Some)322 }323 }324325 /// Creates a foreign item in the the collection.326 fn create_foreign_asset_instance(327 xcm_ext: &dyn XcmExtensions<T>,328 collection_id: CollectionId,329 asset_instance: &AssetInstance,330 to: T::CrossAccountId,331 ) -> DispatchResult {332 let asset_instance_encoded = asset_instance.encode();333334 let derivative_token_id = xcm_ext.create_item(335 &Self::pallet_account(),336 to,337 CreateItemData::NFT(CreateNftData {338 properties: vec![Property {339 key: Self::reserve_asset_instance_property_key(),340 value: asset_instance_encoded341 .try_into()342 .expect("asset instance length <= 32 bytes which is less than value length limit; qed"),343 }]344 .try_into()345 .expect("just one property can always be stored; qed"),346 }),347 &ZeroBudget,348 )?;349350 <ForeignReserveAssetInstanceToTokenId<T>>::insert(351 collection_id,352 asset_instance,353 derivative_token_id,354 );355356 Ok(())357 }358359 /// Deposits an asset instance to the `to` account.360 ///361 /// Either transfers an existing item from the pallet's account362 /// or creates a foreign item.363 fn deposit_asset_instance(364 xcm_ext: &dyn XcmExtensions<T>,365 collection_id: CollectionId,366 asset_instance: &AssetInstance,367 to: T::CrossAccountId,368 ) -> XcmResult {369 let deposit_result = if let Some(token_id) =370 Self::asset_instance_to_token_id(collection_id, asset_instance)?371 {372 let depositor = &Self::pallet_account();373 let from = depositor;374 let amount = 1;375376 xcm_ext.transfer_item(depositor, from, &to, token_id, amount, &ZeroBudget)377 } else {378 Self::create_foreign_asset_instance(xcm_ext, collection_id, asset_instance, to)379 };380381 deposit_result382 .map_err(|_| XcmError::FailedToTransactAsset("non-fungible item deposit failed"))383 }384385 /// Withdraws an asset instance from the `from` account.386 ///387 /// Transfers the asset instance to the pallet's account.388 fn withdraw_asset_instance(389 xcm_ext: &dyn XcmExtensions<T>,390 collection_id: CollectionId,391 asset_instance: &AssetInstance,392 from: T::CrossAccountId,393 ) -> XcmResult {394 let token_id = Self::asset_instance_to_token_id(collection_id, asset_instance)?395 .ok_or(XcmError::AssetNotFound)?;396397 let depositor = &from;398 let to = Self::pallet_account();399 let amount = 1;400 xcm_ext401 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)402 .map_err(|_| XcmError::FailedToTransactAsset("nonfungible item withdraw failed"))?;403404 Ok(())405 }406}407408impl<T: Config> TransactAsset for Pallet<T> {409 fn can_check_in(410 _origin: &MultiLocation,411 _what: &MultiAsset,412 _context: &XcmContext,413 ) -> XcmResult {414 Err(XcmError::Unimplemented)415 }416417 fn check_in(_origin: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}418419 fn can_check_out(420 _dest: &MultiLocation,421 _what: &MultiAsset,422 _context: &XcmContext,423 ) -> XcmResult {424 Err(XcmError::Unimplemented)425 }426427 fn check_out(_dest: &MultiLocation, _what: &MultiAsset, _context: &XcmContext) {}428429 fn deposit_asset(what: &MultiAsset, to: &MultiLocation, _context: &XcmContext) -> XcmResult {430 let to = T::LocationToAccountId::convert_location(to)431 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;432433 let collection_id = Self::asset_to_collection(&what.id)?;434 let dispatch =435 T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;436437 let collection = dispatch.as_dyn();438 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::Unimplemented)?;439440 match what.fun {441 Fungibility::Fungible(amount) => xcm_ext442 .create_item(443 &Self::pallet_account(),444 to,445 CreateItemData::Fungible(CreateFungibleData { value: amount }),446 &ZeroBudget,447 )448 .map(|_| ())449 .map_err(|_| XcmError::FailedToTransactAsset("fungible item deposit failed")),450451 Fungibility::NonFungible(asset_instance) => {452 Self::deposit_asset_instance(xcm_ext, collection_id, &asset_instance, to)453 }454 }455 }456457 fn withdraw_asset(458 what: &MultiAsset,459 from: &MultiLocation,460 _maybe_context: Option<&XcmContext>,461 ) -> Result<staging_xcm_executor::Assets, XcmError> {462 let from = T::LocationToAccountId::convert_location(from)463 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;464465 let collection_id = Self::asset_to_collection(&what.id)?;466 let dispatch =467 T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;468469 let collection = dispatch.as_dyn();470 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;471472 match what.fun {473 Fungibility::Fungible(amount) => xcm_ext474 .burn_item(from, TokenId::default(), amount)475 .map_err(|_| XcmError::FailedToTransactAsset("fungible item withdraw failed"))?,476477 Fungibility::NonFungible(asset_instance) => {478 Self::withdraw_asset_instance(xcm_ext, collection_id, &asset_instance, from)?;479 }480 }481482 Ok(what.clone().into())483 }484485 fn internal_transfer_asset(486 what: &MultiAsset,487 from: &MultiLocation,488 to: &MultiLocation,489 _context: &XcmContext,490 ) -> Result<staging_xcm_executor::Assets, XcmError> {491 let from = T::LocationToAccountId::convert_location(from)492 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;493494 let to = T::LocationToAccountId::convert_location(to)495 .ok_or(XcmExecutorError::AccountIdConversionFailed)?;496497 let collection_id = Self::asset_to_collection(&what.id)?;498499 let dispatch =500 T::CollectionDispatch::dispatch(collection_id).map_err(|_| XcmError::AssetNotFound)?;501 let collection = dispatch.as_dyn();502 let xcm_ext = collection.xcm_extensions().ok_or(XcmError::NoPermission)?;503504 let depositor = &from;505506 let token_id;507 let amount;508 let map_error: fn(DispatchError) -> XcmError;509510 match what.fun {511 Fungibility::Fungible(fungible_amount) => {512 token_id = TokenId::default();513 amount = fungible_amount;514 map_error = |_| XcmError::FailedToTransactAsset("fungible item transfer failed");515 }516517 Fungibility::NonFungible(asset_instance) => {518 token_id = Self::asset_instance_to_token_id(collection_id, &asset_instance)?519 .ok_or(XcmError::AssetNotFound)?;520521 amount = 1;522 map_error = |_| XcmError::FailedToTransactAsset("nonfungible item transfer failed")523 }524 }525526 xcm_ext527 .transfer_item(depositor, &from, &to, token_id, amount, &ZeroBudget)528 .map_err(map_error)?;529530 Ok(what.clone().into())531 }532}533534pub struct CurrencyIdConvert<T: Config>(PhantomData<T>);535impl<T: Config> sp_runtime::traits::Convert<CollectionId, Option<MultiLocation>>536 for CurrencyIdConvert<T>537{538 fn convert(collection_id: CollectionId) -> Option<MultiLocation> {539 if collection_id == NATIVE_FUNGIBLE_COLLECTION_ID {540 Some(T::SelfLocation::get())541 } else {542 <Pallet<T>>::collection_to_foreign_reserve_location(collection_id).or_else(|| {543 T::SelfLocation::get()544 .pushed_with_interior(GeneralIndex(collection_id.0.into()))545 .ok()546 })547 }548 }549}550551pub use frame_support::{552 traits::{553 fungibles::Balanced, tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT,554 },555 weights::{WeightToFee, WeightToFeePolynomial},556};557558#[derive(Encode, Decode, Eq, Debug, Clone, PartialEq, TypeInfo, MaxEncodedLen)]559pub enum ForeignCollectionMode {560 NFT,561 Fungible(u8),562}563564impl From<ForeignCollectionMode> for CollectionMode {565 fn from(value: ForeignCollectionMode) -> Self {566 match value {567 ForeignCollectionMode::NFT => Self::NFT,568 ForeignCollectionMode::Fungible(decimals) => Self::Fungible(decimals),569 }570 }571}572573pub struct FreeForAll;574575impl WeightTrader for FreeForAll {576 fn new() -> Self {577 Self578 }579580 fn buy_weight(581 &mut self,582 weight: Weight,583 payment: Assets,584 _xcm: &XcmContext,585 ) -> Result<Assets, XcmError> {586 log::trace!(target: "fassets::weight", "buy_weight weight: {:?}, payment: {:?}", weight, payment);587 Ok(payment)588 }589}