Skip to content

Commit

Permalink
Add pallet-xcm patch with claim_assets extrinsic (#7)
Browse files Browse the repository at this point in the history
* feat(pallet-xcm): bump version

* feat(claim_assets): add a test for claim_assets in all runtimes

* fix(pallet_xcm::benchmarking): add missing get_asset function to runtimes

* fix: fmt

* chore: move claim_asset tests to their own file under each runtime

* chore(claim_assets): add weights

* feat(claim_assets): more test cases

* fix: fmt
  • Loading branch information
franciscoaguirre authored Mar 4, 2024
1 parent ab960bc commit 21fd3d1
Show file tree
Hide file tree
Showing 41 changed files with 360 additions and 35 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chain-spec-generator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pallet-staking = "29.0.0"
sc-consensus-grandpa = "0.20.0"
sp-runtime = "32.0.0"
sp-consensus-beefy = "14.0.0"
xcm = { package = "staging-xcm", version = "8.0.0" }
xcm = { package = "staging-xcm", version = "8.0.1" }
parachains-common = { version = "8.0.0" }
cumulus-primitives-core = { version = "0.8.0" }

Expand Down
4 changes: 2 additions & 2 deletions integration-tests/emulated/helpers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ pallet-balances = { version = "29.0.0" }
pallet-message-queue = { version = "32.0.0" }

# Polkadot
xcm = { package = "staging-xcm", version = "8.0.0" }
pallet-xcm = { version = "8.0.1" }
xcm = { package = "staging-xcm", version = "8.0.1" }
pallet-xcm = { version = "8.0.2" }

# Cumulus
xcm-emulator = { version = "0.6.0" }
Expand Down
100 changes: 99 additions & 1 deletion integration-tests/emulated/helpers/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub use pallet_message_queue;

// Polkadot
pub use pallet_xcm;
pub use xcm::prelude::{AccountId32, WeightLimit};
pub use xcm::prelude::{AccountId32, VersionedAssets, Weight, WeightLimit};

// Cumulus
pub use asset_test_utils;
Expand Down Expand Up @@ -122,3 +122,101 @@ macro_rules! test_parachain_is_trusted_teleporter {
}
};
}

#[macro_export]
macro_rules! test_chain_can_claim_assets {
( $sender_para:ty, $runtime_call:ty, $network_id:expr, $assets:expr, $amount:expr ) => {
$crate::paste::paste! {
let sender = [<$sender_para Sender>]::get();
let origin = <$sender_para as $crate::Chain>::RuntimeOrigin::signed(sender.clone());
// Receiver is the same as sender
let beneficiary: Location =
$crate::AccountId32 { network: Some($network_id), id: sender.clone().into() }.into();
let versioned_assets: $crate::VersionedAssets = $assets.clone().into();

<$sender_para>::execute_with(|| {
// Assets are trapped for whatever reason.
// The possible reasons for this might differ from runtime to runtime, so here we just drop them directly.
<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::drop_assets(
&beneficiary,
$assets.clone().into(),
&XcmContext { origin: None, message_id: [0u8; 32], topic: None },
);

type RuntimeEvent = <$sender_para as $crate::Chain>::RuntimeEvent;
assert_expected_events!(
$sender_para,
vec![
RuntimeEvent::PolkadotXcm(
$crate::pallet_xcm::Event::AssetsTrapped { origin: beneficiary, assets: versioned_assets, .. }
) => {},
]
);

let balance_before = <$sender_para as [<$sender_para Pallet>]>::Balances::free_balance(&sender);

// Different origin or different assets won't work.
let other_origin = <$sender_para as $crate::Chain>::RuntimeOrigin::signed([<$sender_para Receiver>]::get());
assert!(<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::claim_assets(
other_origin,
bx!(versioned_assets.clone().into()),
bx!(beneficiary.clone().into()),
).is_err());
let other_versioned_assets: $crate::VersionedAssets = Assets::new().into();
assert!(<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::claim_assets(
origin.clone(),
bx!(other_versioned_assets.into()),
bx!(beneficiary.clone().into()),
).is_err());

// Assets will be claimed to `beneficiary`, which is the same as `sender`.
assert_ok!(<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::claim_assets(
origin.clone(),
bx!(versioned_assets.clone().into()),
bx!(beneficiary.clone().into()),
));

assert_expected_events!(
$sender_para,
vec![
RuntimeEvent::PolkadotXcm(
$crate::pallet_xcm::Event::AssetsClaimed { origin: beneficiary, assets: versioned_assets, .. }
) => {},
]
);

// After claiming the assets, the balance has increased.
let balance_after = <$sender_para as [<$sender_para Pallet>]>::Balances::free_balance(&sender);
assert_eq!(balance_after, balance_before + $amount);

// Claiming the assets again doesn't work.
assert!(<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::claim_assets(
origin.clone(),
bx!(versioned_assets.clone().into()),
bx!(beneficiary.clone().into()),
).is_err());

let balance = <$sender_para as [<$sender_para Pallet>]>::Balances::free_balance(&sender);
assert_eq!(balance, balance_after);

// You can also claim assets and send them to a different account.
<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::drop_assets(
&beneficiary,
$assets.clone().into(),
&XcmContext { origin: None, message_id: [0u8; 32], topic: None },
);
let receiver = [<$sender_para Receiver>]::get();
let other_beneficiary: Location =
$crate::AccountId32 { network: Some($network_id), id: receiver.clone().into() }.into();
let balance_before = <$sender_para as [<$sender_para Pallet>]>::Balances::free_balance(&receiver);
assert_ok!(<$sender_para as [<$sender_para Pallet>]>::PolkadotXcm::claim_assets(
origin.clone(),
bx!(versioned_assets.clone().into()),
bx!(other_beneficiary.clone().into()),
));
let balance_after = <$sender_para as [<$sender_para Pallet>]>::Balances::free_balance(&receiver);
assert_eq!(balance_after, balance_before + $amount);
});
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ pallet-asset-conversion = { version = "11.0.0" }
pallet-message-queue = { version = "32.0.0" }

# Polkadot
xcm = { package = "staging-xcm", version = "8.0.0" }
xcm = { package = "staging-xcm", version = "8.0.1" }
xcm-executor = { package = "staging-xcm-executor", default-features = false, version = "8.0.1" }
pallet-xcm = { version = "8.0.1" }
pallet-xcm = { version = "8.0.2" }

# Cumulus
parachains-common = { version = "8.0.0" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests related to claiming assets trapped during XCM execution.
use crate::*;

use asset_hub_kusama_runtime::ExistentialDeposit;
use integration_tests_helpers::test_chain_can_claim_assets;
use xcm_executor::traits::DropAssets;

#[test]
fn assets_can_be_claimed() {
let amount = ExistentialDeposit::get();
let assets: Assets = (Parent, amount).into();

test_chain_can_claim_assets!(AssetHubKusama, RuntimeCall, NetworkId::Kusama, assets, amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod claim_assets;
mod reserve_transfer;
mod send;
mod set_xcm_versions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ pallet-message-queue = { version = "32.0.0" }

# Polkadot
polkadot-runtime-common = { version = "8.0.1" }
xcm = { package = "staging-xcm", version = "8.0.0" }
pallet-xcm = { version = "8.0.1" }
xcm = { package = "staging-xcm", version = "8.0.1" }
pallet-xcm = { version = "8.0.2" }
xcm-executor = { package = "staging-xcm-executor", version = "8.0.1" }

# Cumulus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests related to claiming assets trapped during XCM execution.
use crate::*;

use asset_hub_polkadot_runtime::ExistentialDeposit;
use integration_tests_helpers::test_chain_can_claim_assets;
use xcm_executor::traits::DropAssets;

#[test]
fn assets_can_be_claimed() {
let amount = ExistentialDeposit::get();
let assets: Assets = (Parent, amount).into();

test_chain_can_claim_assets!(
AssetHubPolkadot,
RuntimeCall,
NetworkId::Polkadot,
assets,
amount
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

mod claim_assets;
mod fellowship_treasury;
mod reserve_transfer;
mod send;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pallet-assets = { version = "30.0.0" }
pallet-message-queue = { version = "32.0.0" }

# Polkadot
xcm = { package = "staging-xcm", version = "8.0.0" }
pallet-xcm = { version = "8.0.1" }
xcm = { package = "staging-xcm", version = "8.0.1" }
pallet-xcm = { version = "8.0.2" }
xcm-executor = { package = "staging-xcm-executor", version = "8.0.1" }

# Cumulus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests related to claiming assets trapped during XCM execution.
use crate::*;

use bridge_hub_kusama_runtime::ExistentialDeposit;
use integration_tests_helpers::test_chain_can_claim_assets;
use xcm_executor::traits::DropAssets;

#[test]
fn assets_can_be_claimed() {
let amount = ExistentialDeposit::get();
let assets: Assets = (Parent, amount).into();

test_chain_can_claim_assets!(AssetHubKusama, RuntimeCall, NetworkId::Kusama, assets, amount);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use crate::*;

mod asset_transfers;
mod claim_assets;
mod send_xcm;
mod teleport;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pallet-assets = { version = "30.0.0" }
pallet-message-queue = { version = "32.0.0" }

# Polkadot
xcm = { package = "staging-xcm", version = "8.0.0" }
pallet-xcm = { version = "8.0.1" }
xcm = { package = "staging-xcm", version = "8.0.1" }
pallet-xcm = { version = "8.0.2" }
xcm-executor = { package = "staging-xcm-executor", version = "8.0.1" }

# Cumulus
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests related to claiming assets trapped during XCM execution.
use crate::*;

use bridge_hub_polkadot_runtime::ExistentialDeposit;
use integration_tests_helpers::test_chain_can_claim_assets;
use xcm_executor::traits::DropAssets;

#[test]
fn assets_can_be_claimed() {
let amount = ExistentialDeposit::get();
let assets: Assets = (Parent, amount).into();

test_chain_can_claim_assets!(
AssetHubPolkadot,
RuntimeCall,
NetworkId::Polkadot,
assets,
amount
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use crate::*;

mod asset_transfers;
mod claim_assets;
mod send_xcm;
mod teleport;

Expand Down
4 changes: 2 additions & 2 deletions relay/kusama/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pallet-treasury = { default-features = false , version = "28.0.0" }
pallet-utility = { default-features = false , version = "29.0.0" }
pallet-vesting = { default-features = false , version = "29.0.0" }
pallet-whitelist = { default-features = false , version = "28.0.0" }
pallet-xcm = { default-features = false , version = "8.0.1" }
pallet-xcm = { default-features = false , version = "8.0.2" }
pallet-xcm-benchmarks = { default-features = false, optional = true , version = "8.0.2" }
frame-election-provider-support = { default-features = false , version = "29.0.0" }

Expand All @@ -98,7 +98,7 @@ runtime-common = { package = "polkadot-runtime-common", default-features = false
runtime-parachains = { package = "polkadot-runtime-parachains", default-features = false , version = "8.0.1" }
primitives = { package = "polkadot-primitives", default-features = false , version = "8.0.1" }

xcm = { package = "staging-xcm", default-features = false , version = "8.0.0" }
xcm = { package = "staging-xcm", default-features = false , version = "8.0.1" }
xcm-executor = { package = "staging-xcm-executor", default-features = false , version = "8.0.1" }
xcm-builder = { package = "staging-xcm-builder", default-features = false , version = "8.0.1" }

Expand Down
7 changes: 7 additions & 0 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,6 +2599,13 @@ sp_api::impl_runtime_apis! {
dest
)
}

fn get_asset() -> Asset {
Asset {
id: AssetId(Location::here()),
fun: Fungible(ExistentialDeposit::get()),
}
}
}

impl pallet_xcm_benchmarks::Config for Runtime {
Expand Down
Loading

0 comments on commit 21fd3d1

Please sign in to comment.