git.delta.rocks / unique-network / refs/commits / d59d4711fcdc

difftreelog

Assets transactor

Ilja Khabarov2022-08-19parent: #538e923.patch.diff
in: master

2 files changed

modifiedruntime/common/config/xcm.rsdiffbeforeafterboth
1616
17use frame_support::{17use frame_support::{
18 traits::{18 traits::{
19 tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get, Everything,19 Contains, tokens::currency::Currency as CurrencyT, OnUnbalanced as OnUnbalancedT, Get, Everything,
20 fungibles,
20 },21 },
21 weights::{Weight, WeightToFeePolynomial, WeightToFee},22 weights::{Weight, WeightToFeePolynomial, WeightToFee},
22 parameter_types, match_types,23 parameter_types, match_types,
34 Fungibility::Fungible as XcmFungible,35 Fungibility::Fungible as XcmFungible,
35 MultiAsset, Error as XcmError,36 MultiAsset, Error as XcmError,
36};37};
37use xcm_executor::traits::{MatchesFungible, WeightTrader};38use xcm_executor::traits::{Convert as ConvertXcm, MatchesFungible, WeightTrader};
38use xcm_builder::{39use xcm_builder::{
39 AccountId32Aliases, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin,40 AccountId32Aliases, AllowTopLevelPaidExecutionFrom, CurrencyAdapter, EnsureXcmOrigin,
40 FixedWeightBounds, LocationInverter, NativeAsset, ParentAsSuperuser, RelayChainAsNative,41 FixedWeightBounds, FungiblesAdapter, LocationInverter, NativeAsset, ParentAsSuperuser, RelayChainAsNative,
41 SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,42 SiblingParachainAsNative, SiblingParachainConvertsVia, SignedAccountId32AsNative,
42 SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, ParentIsPreset,43 SignedToAccountId32, SovereignSignedViaLocation, TakeWeightCredit, ParentIsPreset,
43};44};
44use xcm_executor::{Config, XcmExecutor, Assets};45use xcm_executor::{Config, XcmExecutor, Assets};
46use pallet_foreing_assets::{
47 AssetIds, AssetIdMapping, XcmForeignAssetIdMapping, CurrencyId, NativeCurrency,
48 UsingAnyCurrencyComponents, TryAsForeing, ForeignAssetId,
49};
45use sp_std::marker::PhantomData;50use sp_std::{borrow::Borrow, marker::PhantomData, vec, vec::Vec};
46use crate::{51use crate::{
47 Runtime, Call, Event, Origin, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue,52 Runtime, Call, Event, Origin, Balances, ParachainInfo, ParachainSystem, PolkadotXcm, XcmpQueue
48};53};
54#[cfg(feature = "foreign-assets")]
55use crate::ForeingAssets;
56
49use up_common::{57use up_common::{
50 types::{AccountId, Balance},58 types::{AccountId, Balance},
233 }241 }
234}242}
243
244parameter_types! {
245 pub CheckingAccount: AccountId = PolkadotXcm::check_account();
246}
247/// Allow checking in assets that have issuance > 0.
248#[cfg(feature = "foreign-assets")]
249pub struct NonZeroIssuance<AccountId, ForeingAssets>(PhantomData<(AccountId, ForeingAssets)>);
250
251#[cfg(feature = "foreign-assets")]
252impl<AccountId, ForeingAssets> Contains<<ForeingAssets as fungibles::Inspect<AccountId>>::AssetId>
253for NonZeroIssuance<AccountId, ForeingAssets>
254 where
255 ForeingAssets: fungibles::Inspect<AccountId>,
256{
257 fn contains(id: &<ForeingAssets as fungibles::Inspect<AccountId>>::AssetId) -> bool {
258 !ForeingAssets::total_issuance(*id).is_zero()
259 }
260}
261
262#[cfg(feature = "foreign-assets")]
263pub struct AsInnerId<AssetId, ConvertAssetId>(PhantomData<(AssetId, ConvertAssetId)>);
264#[cfg(feature = "foreign-assets")]
265impl<AssetId: Clone + PartialEq, ConvertAssetId: ConvertXcm<AssetId, AssetId>>
266ConvertXcm<MultiLocation, AssetId> for AsInnerId<AssetId, ConvertAssetId>
267 where
268 AssetId: Borrow<AssetId>,
269 AssetId: TryAsForeing<AssetId, ForeignAssetId>,
270 AssetIds: Borrow<AssetId>,
271{
272 fn convert_ref(id: impl Borrow<MultiLocation>) -> Result<AssetId, ()> {
273 let id = id.borrow();
274
275 log::trace!(
276 target: "xcm::AsInnerId::Convert",
277 "AsInnerId {:?}",
278 id
279 );
280
281 let parent = MultiLocation::parent();
282 let here = MultiLocation::here();
283 let self_location = MultiLocation::new(1, X1(Parachain(ParachainInfo::get().into())));
284
285 if *id == parent {
286 return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent));
287 }
288
289 if *id == here || *id == self_location {
290 return ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here));
291 }
292
293 match XcmForeignAssetIdMapping::<Runtime>::get_currency_id(id.clone()) {
294 Some(AssetIds::ForeignAssetId(foreign_asset_id)) => {
295 ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(foreign_asset_id))
296 }
297 _ => ConvertAssetId::convert_ref(AssetIds::ForeignAssetId(0)),
298 }
299 }
300
301 fn reverse_ref(what: impl Borrow<AssetId>) -> Result<MultiLocation, ()> {
302 log::trace!(
303 target: "xcm::AsInnerId::Reverse",
304 "AsInnerId",
305 );
306
307 let asset_id = what.borrow();
308
309 let parent_id =
310 ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Parent)).unwrap();
311 let here_id =
312 ConvertAssetId::convert_ref(AssetIds::NativeAssetId(NativeCurrency::Here)).unwrap();
313
314 if asset_id.clone() == parent_id {
315 return Ok(MultiLocation::parent());
316 }
317
318 if asset_id.clone() == here_id {
319 return Ok(MultiLocation::new(
320 1,
321 X1(Parachain(ParachainInfo::get().into())),
322 ));
323 }
324
325 match <AssetId as TryAsForeing<AssetId, ForeignAssetId>>::try_as_foreing(asset_id.clone()) {
326 Some(fid) => match XcmForeignAssetIdMapping::<Runtime>::get_multi_location(fid) {
327 Some(location) => Ok(location),
328 None => Err(()),
329 },
330 None => Err(()),
331 }
332 }
333}
334
335/// Means for transacting assets besides the native currency on this chain.
336#[cfg(feature = "foreign-assets")]
337pub type FungiblesTransactor = FungiblesAdapter<
338 // Use this fungibles implementation:
339 ForeingAssets,
340 // Use this currency when it is a fungible asset matching the given location or name:
341 ConvertedConcreteAssetId<AssetIds, Balance, AsInnerId<AssetIds, JustTry>, JustTry>,
342 // Convert an XCM MultiLocation into a local account id:
343 LocationToAccountId,
344 // Our chain's account ID type (we can't get away without mentioning it explicitly):
345 AccountId,
346 // We only want to allow teleports of known assets. We use non-zero issuance as an indication
347 // that this asset is known.
348 NonZeroIssuance<AccountId, ForeingAssets>,
349 // The account to use for tracking teleports.
350 CheckingAccount,
351>;
352
353/// Means for transacting assets on this chain.
354#[cfg(feature = "foreign-assets")]
355pub type AssetTransactors = FungiblesTransactor;
356#[cfg(not(feature = "foreign-assets"))]
357pub type AssetTransactors = LocalAssetTransactor;
235358
236pub struct XcmConfig<T>(PhantomData<T>);359pub struct XcmConfig<T>(PhantomData<T>);
237impl<T> Config for XcmConfig<T>360impl<T> Config for XcmConfig<T>
addedruntime/opal/src/xcm_config.rsdiffbeforeafterboth

no changes