From 663325313a4fc8ed23e449dc72d4285315bfc4b9 Mon Sep 17 00:00:00 2001 From: Sergej Sakac <73715684+Szegoo@users.noreply.github.com> Date: Fri, 26 Jan 2024 09:23:19 +0100 Subject: [PATCH] Add `NonFungibleAdapter` (#2924) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR introduces a new `NonFungibleAdapter`. It will be useful for enabling cross-chain Coretime region transfers, as the existing `NonFungiblesAdapter` is unsuitable for this purpose. This is due to the fact that there is only one class of items within the `pallet-broker`, i.e., the Coretime regions. --------- Co-authored-by: Francisco Aguirre Co-authored-by: Dónal Murray --- polkadot/xcm/xcm-builder/src/lib.rs | 5 + .../xcm-builder/src/nonfungible_adapter.rs | 326 ++++++++++++++++++ .../xcm-builder/src/nonfungibles_adapter.rs | 11 +- prdoc/pr_2924.prdoc | 13 + 4 files changed, 354 insertions(+), 1 deletion(-) create mode 100644 polkadot/xcm/xcm-builder/src/nonfungible_adapter.rs create mode 100644 prdoc/pr_2924.prdoc diff --git a/polkadot/xcm/xcm-builder/src/lib.rs b/polkadot/xcm/xcm-builder/src/lib.rs index e3907eee01e9..42522c64d8a4 100644 --- a/polkadot/xcm/xcm-builder/src/lib.rs +++ b/polkadot/xcm/xcm-builder/src/lib.rs @@ -94,6 +94,11 @@ pub use nonfungibles_adapter::{ NonFungiblesAdapter, NonFungiblesMutateAdapter, NonFungiblesTransferAdapter, }; +mod nonfungible_adapter; +pub use nonfungible_adapter::{ + NonFungibleAdapter, NonFungibleMutateAdapter, NonFungibleTransferAdapter, +}; + mod origin_aliases; pub use origin_aliases::AliasForeignAccountId32; diff --git a/polkadot/xcm/xcm-builder/src/nonfungible_adapter.rs b/polkadot/xcm/xcm-builder/src/nonfungible_adapter.rs new file mode 100644 index 000000000000..b69002eafc5b --- /dev/null +++ b/polkadot/xcm/xcm-builder/src/nonfungible_adapter.rs @@ -0,0 +1,326 @@ +// Copyright (C) Parity Technologies (UK) Ltd. +// This file is part of Polkadot. + +// Polkadot is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Polkadot is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Polkadot. If not, see . + +//! Adapters to work with [`frame_support::traits::tokens::nonfungible`] through XCM. + +use crate::MintLocation; +use frame_support::{ + ensure, + traits::{tokens::nonfungible, Get}, +}; +use sp_std::{marker::PhantomData, prelude::*, result}; +use xcm::latest::prelude::*; +use xcm_executor::traits::{ + ConvertLocation, Error as MatchError, MatchesNonFungible, TransactAsset, +}; + +const LOG_TARGET: &str = "xcm::nonfungible_adapter"; + +/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for +/// handling an asset in the XCM executor. +/// Only works for transfers. +pub struct NonFungibleTransferAdapter( + PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId)>, +); +impl< + NonFungible: nonfungible::Transfer, + Matcher: MatchesNonFungible, + AccountIdConverter: ConvertLocation, + AccountId: Clone, // can't get away without it since Currency is generic over it. + > TransactAsset + for NonFungibleTransferAdapter +{ + fn transfer_asset( + what: &Asset, + from: &Location, + to: &Location, + context: &XcmContext, + ) -> result::Result { + log::trace!( + target: LOG_TARGET, + "transfer_asset what: {:?}, from: {:?}, to: {:?}, context: {:?}", + what, + from, + to, + context, + ); + // Check we handle this asset. + let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?; + let destination = AccountIdConverter::convert_location(to) + .ok_or(MatchError::AccountIdConversionFailed)?; + NonFungible::transfer(&instance, &destination) + .map_err(|e| XcmError::FailedToTransactAsset(e.into()))?; + Ok(what.clone().into()) + } +} + +/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for +/// handling an asset in the XCM executor. +/// Works for teleport bookkeeping. +pub struct NonFungibleMutateAdapter< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, +>(PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>); + +impl< + NonFungible: nonfungible::Mutate, + Matcher: MatchesNonFungible, + AccountIdConverter: ConvertLocation, + AccountId: Clone + Eq, // can't get away without it since Currency is generic over it. + CheckingAccount: Get>, + > NonFungibleMutateAdapter +{ + fn can_accrue_checked(instance: NonFungible::ItemId) -> XcmResult { + ensure!(NonFungible::owner(&instance).is_none(), XcmError::NotDepositable); + Ok(()) + } + fn can_reduce_checked(checking_account: AccountId, instance: NonFungible::ItemId) -> XcmResult { + // This is an asset whose teleports we track. + let owner = NonFungible::owner(&instance); + ensure!(owner == Some(checking_account), XcmError::NotWithdrawable); + ensure!(NonFungible::can_transfer(&instance), XcmError::NotWithdrawable); + Ok(()) + } + fn accrue_checked(checking_account: AccountId, instance: NonFungible::ItemId) { + let ok = NonFungible::mint_into(&instance, &checking_account).is_ok(); + debug_assert!(ok, "`mint_into` cannot generally fail; qed"); + } + fn reduce_checked(instance: NonFungible::ItemId) { + let ok = NonFungible::burn(&instance, None).is_ok(); + debug_assert!(ok, "`can_check_in` must have returned `true` immediately prior; qed"); + } +} + +impl< + NonFungible: nonfungible::Mutate, + Matcher: MatchesNonFungible, + AccountIdConverter: ConvertLocation, + AccountId: Clone + Eq, // can't get away without it since Currency is generic over it. + CheckingAccount: Get>, + > TransactAsset + for NonFungibleMutateAdapter +{ + fn can_check_in(_origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult { + log::trace!( + target: LOG_TARGET, + "can_check_in origin: {:?}, what: {:?}, context: {:?}", + _origin, + what, + context, + ); + // Check we handle this asset. + let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?; + match CheckingAccount::get() { + // We track this asset's teleports to ensure no more come in than have gone out. + Some((checking_account, MintLocation::Local)) => + Self::can_reduce_checked(checking_account, instance), + // We track this asset's teleports to ensure no more go out than have come in. + Some((_, MintLocation::NonLocal)) => Self::can_accrue_checked(instance), + _ => Ok(()), + } + } + + fn check_in(_origin: &Location, what: &Asset, context: &XcmContext) { + log::trace!( + target: LOG_TARGET, + "check_in origin: {:?}, what: {:?}, context: {:?}", + _origin, + what, + context, + ); + if let Some(instance) = Matcher::matches_nonfungible(what) { + match CheckingAccount::get() { + // We track this asset's teleports to ensure no more come in than have gone out. + Some((_, MintLocation::Local)) => Self::reduce_checked(instance), + // We track this asset's teleports to ensure no more go out than have come in. + Some((checking_account, MintLocation::NonLocal)) => + Self::accrue_checked(checking_account, instance), + _ => (), + } + } + } + + fn can_check_out(_dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult { + log::trace!( + target: LOG_TARGET, + "can_check_out dest: {:?}, what: {:?}, context: {:?}", + _dest, + what, + context, + ); + // Check we handle this asset. + let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?; + match CheckingAccount::get() { + // We track this asset's teleports to ensure no more come in than have gone out. + Some((_, MintLocation::Local)) => Self::can_accrue_checked(instance), + // We track this asset's teleports to ensure no more go out than have come in. + Some((checking_account, MintLocation::NonLocal)) => + Self::can_reduce_checked(checking_account, instance), + _ => Ok(()), + } + } + + fn check_out(_dest: &Location, what: &Asset, context: &XcmContext) { + log::trace!( + target: LOG_TARGET, + "check_out dest: {:?}, what: {:?}, context: {:?}", + _dest, + what, + context, + ); + if let Some(instance) = Matcher::matches_nonfungible(what) { + match CheckingAccount::get() { + // We track this asset's teleports to ensure no more come in than have gone out. + Some((checking_account, MintLocation::Local)) => + Self::accrue_checked(checking_account, instance), + // We track this asset's teleports to ensure no more go out than have come in. + Some((_, MintLocation::NonLocal)) => Self::reduce_checked(instance), + _ => (), + } + } + } + + fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult { + log::trace!( + target: LOG_TARGET, + "deposit_asset what: {:?}, who: {:?}, context: {:?}", + what, + who, + context, + ); + // Check we handle this asset. + let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?; + let who = AccountIdConverter::convert_location(who) + .ok_or(MatchError::AccountIdConversionFailed)?; + NonFungible::mint_into(&instance, &who) + .map_err(|e| XcmError::FailedToTransactAsset(e.into())) + } + + fn withdraw_asset( + what: &Asset, + who: &Location, + maybe_context: Option<&XcmContext>, + ) -> result::Result { + log::trace!( + target: LOG_TARGET, + "withdraw_asset what: {:?}, who: {:?}, maybe_context: {:?}", + what, + who, + maybe_context, + ); + // Check we handle this asset. + let who = AccountIdConverter::convert_location(who) + .ok_or(MatchError::AccountIdConversionFailed)?; + let instance = Matcher::matches_nonfungible(what).ok_or(MatchError::AssetNotHandled)?; + NonFungible::burn(&instance, Some(&who)) + .map_err(|e| XcmError::FailedToTransactAsset(e.into()))?; + Ok(what.clone().into()) + } +} + +/// [`TransactAsset`] implementation that allows the use of a [`nonfungible`] implementation for +/// handling an asset in the XCM executor. +/// Works for everything. +pub struct NonFungibleAdapter( + PhantomData<(NonFungible, Matcher, AccountIdConverter, AccountId, CheckingAccount)>, +); +impl< + NonFungible: nonfungible::Mutate + nonfungible::Transfer, + Matcher: MatchesNonFungible, + AccountIdConverter: ConvertLocation, + AccountId: Clone + Eq, // can't get away without it since Currency is generic over it. + CheckingAccount: Get>, + > TransactAsset + for NonFungibleAdapter +{ + fn can_check_in(origin: &Location, what: &Asset, context: &XcmContext) -> XcmResult { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::can_check_in(origin, what, context) + } + + fn check_in(origin: &Location, what: &Asset, context: &XcmContext) { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::check_in(origin, what, context) + } + + fn can_check_out(dest: &Location, what: &Asset, context: &XcmContext) -> XcmResult { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::can_check_out(dest, what, context) + } + + fn check_out(dest: &Location, what: &Asset, context: &XcmContext) { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::check_out(dest, what, context) + } + + fn deposit_asset(what: &Asset, who: &Location, context: Option<&XcmContext>) -> XcmResult { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::deposit_asset(what, who, context) + } + + fn withdraw_asset( + what: &Asset, + who: &Location, + maybe_context: Option<&XcmContext>, + ) -> result::Result { + NonFungibleMutateAdapter::< + NonFungible, + Matcher, + AccountIdConverter, + AccountId, + CheckingAccount, + >::withdraw_asset(what, who, maybe_context) + } + + fn transfer_asset( + what: &Asset, + from: &Location, + to: &Location, + context: &XcmContext, + ) -> result::Result { + NonFungibleTransferAdapter::::transfer_asset( + what, from, to, context, + ) + } +} diff --git a/polkadot/xcm/xcm-builder/src/nonfungibles_adapter.rs b/polkadot/xcm/xcm-builder/src/nonfungibles_adapter.rs index b4801d3a23a1..3fce953848eb 100644 --- a/polkadot/xcm/xcm-builder/src/nonfungibles_adapter.rs +++ b/polkadot/xcm/xcm-builder/src/nonfungibles_adapter.rs @@ -14,7 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Polkadot. If not, see . -//! Adapters to work with [`frame_support::traits::fungibles`] through XCM. +//! Adapters to work with [`frame_support::traits::tokens::nonfungibles`] through XCM. use crate::{AssetChecking, MintLocation}; use frame_support::{ @@ -29,6 +29,9 @@ use xcm_executor::traits::{ const LOG_TARGET: &str = "xcm::nonfungibles_adapter"; +/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for +/// handling an asset in the XCM executor. +/// Only works for transfers. pub struct NonFungiblesTransferAdapter( PhantomData<(Assets, Matcher, AccountIdConverter, AccountId)>, ); @@ -63,6 +66,9 @@ impl< } } +/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for +/// handling an asset in the XCM executor. +/// Only works for teleport bookkeeping. pub struct NonFungiblesMutateAdapter< Assets, Matcher, @@ -245,6 +251,9 @@ impl< } } +/// [`TransactAsset`] implementation that allows the use of a [`nonfungibles`] implementation for +/// handling an asset in the XCM executor. +/// Works for everything. pub struct NonFungiblesAdapter< Assets, Matcher, diff --git a/prdoc/pr_2924.prdoc b/prdoc/pr_2924.prdoc new file mode 100644 index 000000000000..f22c5365b46d --- /dev/null +++ b/prdoc/pr_2924.prdoc @@ -0,0 +1,13 @@ +# Schema: Polkadot SDK PRDoc Schema (prdoc) v1.0.0 +# See doc at https://raw.githubusercontent.com/paritytech/polkadot-sdk/master/prdoc/schema_user.json + +title: Add NonFungibleAdapter + +doc: + - audience: Runtime Dev + description: | + Introduces a new adapter, `NonFungibleAdapter`, to work with `frame_support::traits::tokens::nonfungible` + through XCM. + +crates: + - name: staging-xcm-builder