Skip to content

Commit 20ebb1a

Browse files
bkonturclaravanstaden
authored andcommitted
[pallet_xcm] adapt paritytech/polkadot-sdk#2388 (new transfer_assets)
1 parent 70f49b1 commit 20ebb1a

File tree

14 files changed

+352
-0
lines changed

14 files changed

+352
-0
lines changed

relay/kusama/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,6 +2352,20 @@ sp_api::impl_runtime_apis! {
23522352
crate::Junction::Parachain(43211234).into(),
23532353
))
23542354
}
2355+
2356+
fn set_up_complex_asset_transfer(
2357+
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
2358+
// Relay supports only native token, either reserve transfer it to non-system parachains,
2359+
// or teleport it to system parachain. Use the teleport case for benchmarking as it's
2360+
// slightly heavier.
2361+
// Relay/native token can be teleported to/from AH.
2362+
let native_location = Here.into();
2363+
let dest = crate::xcm_config::AssetHubLocation::get();
2364+
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
2365+
native_location,
2366+
dest
2367+
)
2368+
}
23552369
}
23562370

23572371
parameter_types! {

relay/kusama/src/weights/pallet_xcm.rs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

relay/polkadot/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2354,6 +2354,20 @@ sp_api::impl_runtime_apis! {
23542354
crate::Junction::Parachain(43211234).into(),
23552355
))
23562356
}
2357+
2358+
fn set_up_complex_asset_transfer(
2359+
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
2360+
// Relay supports only native token, either reserve transfer it to non-system parachains,
2361+
// or teleport it to system parachain. Use the teleport case for benchmarking as it's
2362+
// slightly heavier.
2363+
// Relay/native token can be teleported to/from AH.
2364+
let native_location = Here.into();
2365+
let dest = crate::xcm_config::AssetHubLocation::get();
2366+
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
2367+
native_location,
2368+
dest
2369+
)
2370+
}
23572371
}
23582372

23592373
parameter_types! {

relay/polkadot/src/weights/pallet_xcm.rs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,55 @@ impl_runtime_apis! {
13081308
ParentThen(Parachain(random_para_id).into()).into(),
13091309
))
13101310
}
1311+
1312+
fn set_up_complex_asset_transfer(
1313+
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
1314+
// Transfer to Relay some local AH asset (local-reserve-transfer) while paying
1315+
// fees using teleported native token.
1316+
// (We don't care that Relay doesn't accept incoming unknown AH local asset)
1317+
let dest = Parent.into();
1318+
1319+
let fee_amount = EXISTENTIAL_DEPOSIT;
1320+
let fee_asset: MultiAsset = (MultiLocation::parent(), fee_amount).into();
1321+
1322+
let who = frame_benchmarking::whitelisted_caller();
1323+
// Give some multiple of the existential deposit
1324+
let balance = fee_amount + EXISTENTIAL_DEPOSIT * 1000;
1325+
let _ = <Balances as frame_support::traits::Currency<_>>::make_free_balance_be(
1326+
&who, balance,
1327+
);
1328+
// verify initial balance
1329+
assert_eq!(Balances::free_balance(&who), balance);
1330+
1331+
// set up local asset
1332+
let asset_amount = 10u128;
1333+
let initial_asset_amount = asset_amount * 10;
1334+
let (asset_id, _, _) = pallet_assets::benchmarking::create_default_minted_asset::<
1335+
Runtime,
1336+
pallet_assets::Instance1
1337+
>(true, initial_asset_amount);
1338+
let asset_location = MultiLocation::new(
1339+
0,
1340+
X2(PalletInstance(50), GeneralIndex(u32::from(asset_id).into()))
1341+
);
1342+
let transfer_asset: MultiAsset = (asset_location, asset_amount).into();
1343+
1344+
let assets: MultiAssets = vec![fee_asset.clone(), transfer_asset].into();
1345+
let fee_index = if assets.get(0).unwrap().eq(&fee_asset) { 0 } else { 1 };
1346+
1347+
// verify transferred successfully
1348+
let verify = Box::new(move || {
1349+
// verify native balance after transfer, decreased by transferred fee amount
1350+
// (plus transport fees)
1351+
assert!(Balances::free_balance(&who) <= balance - fee_amount);
1352+
// verify asset balance decreased by exactly transferred amount
1353+
assert_eq!(
1354+
Assets::balance(asset_id.into(), &who),
1355+
initial_asset_amount - asset_amount,
1356+
);
1357+
});
1358+
Some((assets, fee_index as u32, dest, verify))
1359+
}
13111360
}
13121361

13131362
parameter_types! {

system-parachains/asset-hubs/asset-hub-kusama/src/weights/pallet_xcm.rs

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,6 +1200,55 @@ impl_runtime_apis! {
12001200
ParentThen(Parachain(random_para_id).into()).into(),
12011201
))
12021202
}
1203+
1204+
fn set_up_complex_asset_transfer(
1205+
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
1206+
// Transfer to Relay some local AH asset (local-reserve-transfer) while paying
1207+
// fees using teleported native token.
1208+
// (We don't care that Relay doesn't accept incoming unknown AH local asset)
1209+
let dest = Parent.into();
1210+
1211+
let fee_amount = EXISTENTIAL_DEPOSIT;
1212+
let fee_asset: MultiAsset = (MultiLocation::parent(), fee_amount).into();
1213+
1214+
let who = frame_benchmarking::whitelisted_caller();
1215+
// Give some multiple of the existential deposit
1216+
let balance = fee_amount + EXISTENTIAL_DEPOSIT * 1000;
1217+
let _ = <Balances as frame_support::traits::Currency<_>>::make_free_balance_be(
1218+
&who, balance,
1219+
);
1220+
// verify initial balance
1221+
assert_eq!(Balances::free_balance(&who), balance);
1222+
1223+
// set up local asset
1224+
let asset_amount = 10u128;
1225+
let initial_asset_amount = asset_amount * 10;
1226+
let (asset_id, _, _) = pallet_assets::benchmarking::create_default_minted_asset::<
1227+
Runtime,
1228+
pallet_assets::Instance1
1229+
>(true, initial_asset_amount);
1230+
let asset_location = MultiLocation::new(
1231+
0,
1232+
X2(PalletInstance(50), GeneralIndex(u32::from(asset_id).into()))
1233+
);
1234+
let transfer_asset: MultiAsset = (asset_location, asset_amount).into();
1235+
1236+
let assets: MultiAssets = vec![fee_asset.clone(), transfer_asset].into();
1237+
let fee_index = if assets.get(0).unwrap().eq(&fee_asset) { 0 } else { 1 };
1238+
1239+
// verify transferred successfully
1240+
let verify = Box::new(move || {
1241+
// verify native balance after transfer, decreased by transferred fee amount
1242+
// (plus transport fees)
1243+
assert!(Balances::free_balance(&who) <= balance - fee_amount);
1244+
// verify asset balance decreased by exactly transferred amount
1245+
assert_eq!(
1246+
Assets::balance(asset_id.into(), &who),
1247+
initial_asset_amount - asset_amount,
1248+
);
1249+
});
1250+
Some((assets, fee_index as u32, dest, verify))
1251+
}
12031252
}
12041253

12051254
parameter_types! {

system-parachains/asset-hubs/asset-hub-polkadot/src/weights/pallet_xcm.rs

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,18 @@ impl_runtime_apis! {
996996
// Reserve transfers are disabled on BH.
997997
None
998998
}
999+
1000+
fn set_up_complex_asset_transfer(
1001+
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
1002+
// BH only supports teleports to system parachain.
1003+
// Relay/native token can be teleported between BH and Relay.
1004+
let native_location = Parent.into();
1005+
let dest = Parent.into();
1006+
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
1007+
native_location,
1008+
dest
1009+
)
1010+
}
9991011
}
10001012

10011013
parameter_types! {

system-parachains/bridge-hubs/bridge-hub-kusama/src/weights/pallet_xcm.rs

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)