Skip to content

Commit

Permalink
add multi currency wrapper to register callback when xcm deposit succ…
Browse files Browse the repository at this point in the history
…ess (#3707)
  • Loading branch information
RustNinja authored Jun 16, 2023
2 parents e6711cb + e0831b9 commit 02aa203
Showing 1 changed file with 123 additions and 1 deletion.
124 changes: 123 additions & 1 deletion code/parachain/runtime/composable/src/xcmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use orml_traits::{
location::{AbsoluteReserveProvider, RelativeReserveProvider},
parameter_type_with_key,
};
use codec::FullCodec;

use orml_xcm_support::{
DepositToAlternative, IsNativeConcrete, MultiCurrencyAdapter, MultiNativeAsset,
Expand All @@ -28,7 +29,7 @@ use xcm_builder::{
TakeWeightCredit,
};
use xcm_executor::{
traits::{ConvertOrigin, DropAssets},
traits::{ConvertOrigin, DropAssets, MatchesFungible},
Assets, XcmExecutor,
};

Expand Down Expand Up @@ -99,8 +100,37 @@ pub type LocationToAccountId = (
SiblingParachainConvertsVia<Sibling, AccountId>,
// Straight up local `AccountId32` origins just alias directly to `AccountId`.
AccountId32Aliases<RelayNetwork, AccountId>,
//Convert Multilication X4(PalletInstance, AccountId, GeneralIndex, AccountId32) into AccountId
AccountId32BatchTx<AccountId>
);

pub struct AccountId32BatchTx<AccountId>(PhantomData<AccountId>);
impl<AccountId: From<[u8; 32]> + Into<[u8; 32]> + Clone>
xcm_executor::traits::Convert<MultiLocation, AccountId> for AccountId32BatchTx<AccountId>
{
fn convert(location: MultiLocation) -> Result<AccountId, MultiLocation> {
let id = match location {
MultiLocation { parents: 0, interior: X4(
PalletInstance(_),
AccountId32{ id, network: None },
GeneralIndex(_),
AccountId32{ id: _, network: None } ) } => id,
_ => return Err(location),
};
Ok(id.into())
}

fn reverse(who: AccountId) -> Result<MultiLocation, AccountId> {
// let m = MultiLocation { parents: 0, interior: X4(
// PalletInstance(0),
// AccountId32{ id : who.clone().into(), network: None }.into(),
// GeneralIndex(0),
// AccountId32{ id: who.into(), network: None } ) };
// Ok(m)
Err(who)
}
}

/// This is the type we use to convert an (incoming) XCM origin into a local `Origin` instance,
/// ready for dispatching a transaction with Xcm's `Transact`. There is an `OriginKind` which can
/// biases the kind of local `Origin` it will become.
Expand Down Expand Up @@ -133,6 +163,98 @@ pub type LocalAssetTransactor = MultiCurrencyAdapter<
DepositToAlternative<TreasuryAccount, Tokens, CurrencyId, AccountId, Balance>,
>;

struct MultiCurrencyAdapterWrapper<
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
>(
PhantomData<(
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
)>,
);

impl<
MultiCurrency: orml_traits::MultiCurrency<AccountId, CurrencyId = CurrencyId>,
UnknownAsset: orml_xcm_support::UnknownAsset,
Match: MatchesFungible<MultiCurrency::Balance>,
AccountId: sp_std::fmt::Debug + Clone,
AccountIdConvert: xcm_executor::traits::Convert<MultiLocation, AccountId>,
CurrencyId: FullCodec + Eq + PartialEq + Copy + MaybeSerializeDeserialize + sp_std::fmt::Debug,
CurrencyIdConvert: Convert<MultiAsset, Option<CurrencyId>>,
DepositFailureHandler: orml_xcm_support::OnDepositFail<CurrencyId, AccountId, MultiCurrency::Balance>,
> xcm_executor::traits::TransactAsset
for MultiCurrencyAdapterWrapper<
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
>
{
fn deposit_asset(asset: &MultiAsset, location: &MultiLocation, context: &XcmContext) -> xcm::v3::Result {
MultiCurrencyAdapter::<
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
>::deposit_asset(asset, location, context)
}

fn withdraw_asset(
asset: &MultiAsset,
location: &MultiLocation,
maybe_context: Option<&XcmContext>,
) -> sp_std::result::Result<Assets, XcmError> {
MultiCurrencyAdapter::<
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
>::withdraw_asset(asset, location, maybe_context)
}

fn transfer_asset(
asset: &MultiAsset,
from: &MultiLocation,
to: &MultiLocation,
context: &XcmContext,
) -> sp_std::result::Result<Assets, XcmError> {
MultiCurrencyAdapter::<
MultiCurrency,
UnknownAsset,
Match,
AccountId,
AccountIdConvert,
CurrencyId,
CurrencyIdConvert,
DepositFailureHandler,
>::transfer_asset(asset, from, to, context)
}
}

pub struct ForeignXcm;

impl Convert<CurrencyId, Option<MultiLocation>> for ForeignXcm {
Expand Down

0 comments on commit 02aa203

Please sign in to comment.