Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fungigbles adapter to withdraw (burn) assets and not get trapped #1088

Merged
merged 3 commits into from
Dec 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions pallets/xcm-transactor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ pub mod pallet {
use sp_std::convert::TryFrom;
use sp_std::prelude::*;
use xcm::{latest::prelude::*, VersionedMultiLocation};
use xcm_executor::traits::{InvertLocation, WeightBounds};
use xcm_executor::traits::{InvertLocation, TransactAsset, WeightBounds};
use xcm_primitives::{UtilityAvailableCalls, UtilityEncodeCall, XcmTransact};

#[pallet::pallet]
Expand Down Expand Up @@ -94,6 +94,10 @@ pub mod pallet {
// utility call encoding and multilocation gathering
type Transactor: Parameter + Member + Clone + XcmTransact;

/// AssetTransactor allows us to withdraw asset without being trapped
/// This should change in xcm v3, which allows us to burn assets
type AssetTransactor: TransactAsset;

// The origin that is allowed to register derivative address indices
type DerivativeAddressRegistrationOrigin: EnsureOrigin<Self::Origin>;

Expand Down Expand Up @@ -179,6 +183,7 @@ pub mod pallet {
NotCrossChainTransferableCurrency,
XcmExecuteError,
BadVersion,
UnableToWithdrawAsset,
}

#[pallet::event]
Expand Down Expand Up @@ -443,28 +448,8 @@ pub mod pallet {

// Construct the local withdraw message with the previous calculated amount
// This message deducts and burns "amount" from the caller when executed
let mut withdraw_message = Xcm(vec![WithdrawAsset(fee.clone().into())]);

// Calculate weight of message
let weight = T::Weigher::weight(&mut withdraw_message)
.map_err(|()| Error::<T>::UnweighableMessage)?;

// This execution ensures we withdraw assets from the calling account
let outcome = T::XcmExecutor::execute_xcm_in_credit(
origin_as_mult,
withdraw_message,
weight,
weight,
);

// Let's check if the execution was succesful
let maybe_xcm_err: Option<XcmError> = match outcome {
Outcome::Complete(_w) => Option::None,
Outcome::Incomplete(_w, err) => Some(err),
Outcome::Error(err) => Some(err),
};

ensure!(maybe_xcm_err.is_none(), Error::<T>::XcmExecuteError);
T::AssetTransactor::withdraw_asset(&fee.clone().into(), &origin_as_mult)
.map_err(|_| Error::<T>::UnableToWithdrawAsset)?;

// Construct the transact message. This is composed of WithdrawAsset||BuyExecution||
// Transact.
Expand Down
1 change: 1 addition & 0 deletions pallets/xcm-transactor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ impl Config for Test {
type Transactor = Transactors;
type DerivativeAddressRegistrationOrigin = EnsureRoot<u64>;
type SovereignAccountDispatcherOrigin = EnsureRoot<u64>;
type AssetTransactor = DummyAssetTransactor;
type CurrencyId = CurrencyId;
type CurrencyIdToMultiLocation = CurrencyIdToMultiLocation;
type AccountIdToMultiLocation = AccountIdToMultiLocation;
Expand Down
1 change: 1 addition & 0 deletions precompiles/xcm_transactor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,7 @@ impl xcm_transactor::Config for Test {
type LocationInverter = InvertNothing;
type BaseXcmWeight = BaseXcmWeight;
type XcmSender = DoNothingRouter;
type AssetTransactor = DummyAssetTransactor;
}

// We need to use the encoding from the relay mock runtime
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbase/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,7 @@ impl xcm_transactor::Config for Runtime {
type Weigher = xcm_builder::FixedWeightBounds<UnitWeightCost, Call, MaxInstructions>;
type LocationInverter = LocationInverter<Ancestry>;
type BaseXcmWeight = BaseXcmWeight;
type AssetTransactor = AssetTransactors;
}

/// Call filter used during Phase 3 of the Moonriver rollout
Expand Down
1 change: 1 addition & 0 deletions runtime/moonbase/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,7 @@ impl xcm_transactor::Config for Runtime {
type LocationInverter = LocationInverter<Ancestry>;
type XcmSender = XcmRouter;
type BaseXcmWeight = BaseXcmWeight;
type AssetTransactor = AssetTransactors;
}

pub struct NormalFilter;
Expand Down
11 changes: 11 additions & 0 deletions runtime/moonbase/tests/xcm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,17 @@ fn transact_through_derivative_multilocation() {
4000000000,
encoded,
));
let event_found: Option<parachain::Event> =
parachain::para_events()
.iter()
.find_map(|event| match event.clone() {
parachain::Event::PolkadotXcm(pallet_xcm::Event::AssetsTrapped(_, _, _)) => {
Some(event.clone())
}
_ => None,
});
// Assert that the events do not contain the assets being trapped
assert!(event_found.is_none());
});

Relay::execute_with(|| {
Expand Down