// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Pezkuwi.
// Pezkuwi 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.
// Pezkuwi 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 Pezkuwi. If not, see .
//! Adapters to work with `pezframe_support::traits::Currency` through XCM.
#![allow(deprecated)]
use super::MintLocation;
use core::{fmt::Debug, marker::PhantomData, result};
use pezframe_support::traits::{ExistenceRequirement::AllowDeath, Get, WithdrawReasons};
use pezsp_runtime::traits::CheckedSub;
use xcm::latest::{Asset, Error as XcmError, Location, Result, XcmContext};
use xcm_executor::{
traits::{ConvertLocation, MatchesFungible, TransactAsset},
AssetsInHolding,
};
/// Asset transaction errors.
enum Error {
/// The given asset is not handled. (According to [`XcmError::AssetNotFound`])
AssetNotHandled,
/// `Location` to `AccountId` conversion failed.
AccountIdConversionFailed,
}
impl From for XcmError {
fn from(e: Error) -> Self {
use XcmError::FailedToTransactAsset;
match e {
Error::AssetNotHandled => XcmError::AssetNotFound,
Error::AccountIdConversionFailed => FailedToTransactAsset("AccountIdConversionFailed"),
}
}
}
/// Simple adapter to use a currency as asset transactor. This type can be used as `type
/// AssetTransactor` in `xcm::Config`.
///
/// # Example
/// ```
/// use codec::Decode;
/// use pezframe_support::{parameter_types, PalletId};
/// use pezsp_runtime::traits::{AccountIdConversion, TrailingZeroInput};
/// use xcm::latest::prelude::*;
/// use pezstaging_xcm_builder::{ParentIsPreset, CurrencyAdapter, IsConcrete};
///
/// /// Our chain's account id.
/// type AccountId = pezsp_runtime::AccountId32;
///
/// /// Our relay chain's location.
/// parameter_types! {
/// pub RelayChain: Location = Parent.into();
/// pub CheckingAccount: AccountId = PalletId(*b"checking").into_account_truncating();
/// }
///
/// /// Some items that implement `ConvertLocation`. Can be more, but for now we just assume we accept
/// /// messages from the parent (relay chain).
/// pub type LocationConverter = (ParentIsPreset);
///
/// /// Just a dummy implementation of `Currency`. Normally this would be `Balances`.
/// pub type CurrencyImpl = ();
///
/// /// Final currency adapter. This can be used in `xcm::Config` to specify how asset related transactions happen.
/// pub type AssetTransactor = CurrencyAdapter<
/// // Use this `Currency` impl instance:
/// CurrencyImpl,
/// // The matcher: use the currency when the asset is a concrete asset in our relay chain.
/// IsConcrete,
/// // The local converter: default account of the parent relay chain.
/// LocationConverter,
/// // Our chain's account ID type.
/// AccountId,
/// // The checking account. Can be any deterministic inaccessible account.
/// CheckingAccount,
/// >;
/// ```
#[deprecated = "Use `FungibleAdapter` instead"]
pub struct CurrencyAdapter(
PhantomData<(Currency, Matcher, AccountIdConverter, AccountId, CheckedAccount)>,
);
impl<
Currency: pezframe_support::traits::Currency,
Matcher: MatchesFungible,
AccountIdConverter: ConvertLocation,
AccountId: Clone, // can't get away without it since Currency is generic over it.
CheckedAccount: Get