#![cfg_attr(not(feature = "std"), no_std)]
use codec::Encode;
use cumulus_primitives_core::UpwardMessageSender;
use frame_support::{
traits::tokens::{fungibles, fungibles::Inspect},
weights::Weight,
};
use sp_runtime::{traits::Saturating, SaturatedConversion};
use sp_std::marker::PhantomData;
use xcm::{
latest::{prelude::*, Weight as XCMWeight},
WrapVersion,
};
use xcm_builder::TakeRevenue;
use xcm_executor::traits::{MatchesFungibles, TransactAsset, WeightTrader};
pub struct ParentAsUmp<T, W>(PhantomData<(T, W)>);
impl<T: UpwardMessageSender, W: WrapVersion> SendXcm for ParentAsUmp<T, W> {
fn send_xcm(dest: impl Into<MultiLocation>, msg: Xcm<()>) -> Result<(), SendError> {
let dest = dest.into();
if dest.contains_parents_only(1) {
let versioned_xcm =
W::wrap_version(&dest, msg).map_err(|()| SendError::DestinationUnsupported)?;
let data = versioned_xcm.encode();
T::send_upward_message(data).map_err(|e| SendError::Transport(e.into()))?;
Ok(())
} else {
Err(SendError::CannotReachDestination(dest, msg))
}
}
}
#[derive(Clone, Eq, PartialEq, Debug)]
struct AssetTraderRefunder {
weight_outstanding: Weight,
outstanding_concrete_asset: MultiAsset,
}
pub struct TakeFirstAssetTrader<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId> + fungibles::Transfer<AccountId> + fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
>(
Option<AssetTraderRefunder>,
PhantomData<(AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund)>,
);
impl<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId>
+ fungibles::Transfer<AccountId>
+ fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
> WeightTrader
for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
{
fn new() -> Self {
Self(None, PhantomData)
}
fn buy_weight(
&mut self,
weight: XCMWeight,
payment: xcm_executor::Assets,
) -> Result<xcm_executor::Assets, XcmError> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::buy_weight weight: {:?}, payment: {:?}", weight, payment);
if self.0.is_some() {
return Err(XcmError::NotWithdrawable)
}
let weight = Weight::from_ref_time(weight);
let multiassets: MultiAssets = payment.clone().into();
let first = multiassets.get(0).ok_or(XcmError::AssetNotFound)?;
let (local_asset_id, _) =
Matcher::matches_fungibles(&first).map_err(|_| XcmError::AssetNotFound)?;
let asset_balance: u128 = FeeCharger::charge_weight_in_fungibles(local_asset_id, weight)
.map(|amount| {
let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id);
if amount < minimum_balance {
minimum_balance
} else {
amount
}
})?
.try_into()
.map_err(|_| XcmError::Overflow)?;
let required = first.id.clone().into_multiasset(asset_balance.into());
let unused = payment.checked_sub(required.clone()).map_err(|_| XcmError::TooExpensive)?;
self.0 = Some(AssetTraderRefunder {
weight_outstanding: weight,
outstanding_concrete_asset: required,
});
Ok(unused)
}
fn refund_weight(&mut self, weight: XCMWeight) -> Option<MultiAsset> {
log::trace!(target: "xcm::weight", "TakeFirstAssetTrader::refund_weight weight: {:?}", weight);
if let Some(AssetTraderRefunder {
mut weight_outstanding,
outstanding_concrete_asset: MultiAsset { id, fun },
}) = self.0.clone()
{
let weight = Weight::from_ref_time(weight).min(weight_outstanding);
let (local_asset_id, outstanding_balance) =
Matcher::matches_fungibles(&(id.clone(), fun).into()).ok()?;
let minimum_balance = ConcreteAssets::minimum_balance(local_asset_id);
let (asset_balance, outstanding_minus_substracted) =
FeeCharger::charge_weight_in_fungibles(local_asset_id, weight).ok().map(
|asset_balance| {
if outstanding_balance.saturating_sub(asset_balance) > minimum_balance {
(asset_balance, outstanding_balance.saturating_sub(asset_balance))
}
else {
(outstanding_balance.saturating_sub(minimum_balance), minimum_balance)
}
},
)?;
let outstanding_minus_substracted: u128 =
outstanding_minus_substracted.saturated_into();
let asset_balance: u128 = asset_balance.saturated_into();
let outstanding_concrete_asset: MultiAsset =
(id.clone(), outstanding_minus_substracted).into();
weight_outstanding = weight_outstanding.saturating_sub(weight);
self.0 = Some(AssetTraderRefunder { weight_outstanding, outstanding_concrete_asset });
if asset_balance > 0 {
Some((id, asset_balance).into())
} else {
None
}
} else {
None
}
}
}
impl<
AccountId,
FeeCharger: ChargeWeightInFungibles<AccountId, ConcreteAssets>,
Matcher: MatchesFungibles<ConcreteAssets::AssetId, ConcreteAssets::Balance>,
ConcreteAssets: fungibles::Mutate<AccountId>
+ fungibles::Transfer<AccountId>
+ fungibles::Balanced<AccountId>,
HandleRefund: TakeRevenue,
> Drop for TakeFirstAssetTrader<AccountId, FeeCharger, Matcher, ConcreteAssets, HandleRefund>
{
fn drop(&mut self) {
if let Some(asset_trader) = self.0.clone() {
HandleRefund::take_revenue(asset_trader.outstanding_concrete_asset);
}
}
}
pub struct XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>(
PhantomData<(FungiblesMutateAdapter, AccountId, ReceiverAccount)>,
);
impl<
FungiblesMutateAdapter: TransactAsset,
AccountId: Clone + Into<[u8; 32]>,
ReceiverAccount: frame_support::traits::Get<Option<AccountId>>,
> TakeRevenue for XcmFeesTo32ByteAccount<FungiblesMutateAdapter, AccountId, ReceiverAccount>
{
fn take_revenue(revenue: MultiAsset) {
if let Some(receiver) = ReceiverAccount::get() {
let ok = FungiblesMutateAdapter::deposit_asset(
&revenue,
&(X1(AccountId32 { network: Any, id: receiver.into() }).into()),
)
.is_ok();
debug_assert!(ok, "`deposit_asset` cannot generally fail; qed");
}
}
}
pub trait ChargeWeightInFungibles<AccountId, Assets: fungibles::Inspect<AccountId>> {
fn charge_weight_in_fungibles(
asset_id: <Assets as Inspect<AccountId>>::AssetId,
weight: Weight,
) -> Result<<Assets as Inspect<AccountId>>::Balance, XcmError>;
}