Skip to content

Commit

Permalink
[pallet_xcm] adapt paritytech/polkadot-sdk#2388 (new transfer_assets)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkontur committed Jan 12, 2024
1 parent 64fc19a commit 51bf27a
Show file tree
Hide file tree
Showing 14 changed files with 352 additions and 0 deletions.
14 changes: 14 additions & 0 deletions relay/kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2358,6 +2358,20 @@ sp_api::impl_runtime_apis! {
crate::Junction::Parachain(43211234).into(),
))
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// Relay supports only native token, either reserve transfer it to non-system parachains,
// or teleport it to system parachain. Use the teleport case for benchmarking as it's
// slightly heavier.
// Relay/native token can be teleported to/from AH.
let native_location = Here.into();
let dest = crate::xcm_config::AssetHubLocation::get();
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
native_location,
dest
)
}
}

parameter_types! {
Expand Down
26 changes: 26 additions & 0 deletions relay/kusama/src/weights/pallet_xcm.rs

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

14 changes: 14 additions & 0 deletions relay/polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2354,6 +2354,20 @@ sp_api::impl_runtime_apis! {
crate::Junction::Parachain(43211234).into(),
))
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// Relay supports only native token, either reserve transfer it to non-system parachains,
// or teleport it to system parachain. Use the teleport case for benchmarking as it's
// slightly heavier.
// Relay/native token can be teleported to/from AH.
let native_location = Here.into();
let dest = crate::xcm_config::AssetHubLocation::get();
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
native_location,
dest
)
}
}

parameter_types! {
Expand Down
26 changes: 26 additions & 0 deletions relay/polkadot/src/weights/pallet_xcm.rs

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

49 changes: 49 additions & 0 deletions system-parachains/asset-hubs/asset-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,6 +1310,55 @@ impl_runtime_apis! {
ParentThen(Parachain(random_para_id).into()).into(),
))
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// Transfer to Relay some local AH asset (local-reserve-transfer) while paying
// fees using teleported native token.
// (We don't care that Relay doesn't accept incoming unknown AH local asset)
let dest = Parent.into();

let fee_amount = EXISTENTIAL_DEPOSIT;
let fee_asset: MultiAsset = (MultiLocation::parent(), fee_amount).into();

let who = frame_benchmarking::whitelisted_caller();
// Give some multiple of the existential deposit
let balance = fee_amount + EXISTENTIAL_DEPOSIT * 1000;
let _ = <Balances as frame_support::traits::Currency<_>>::make_free_balance_be(
&who, balance,
);
// verify initial balance
assert_eq!(Balances::free_balance(&who), balance);

// set up local asset
let asset_amount = 10u128;
let initial_asset_amount = asset_amount * 10;
let (asset_id, _, _) = pallet_assets::benchmarking::create_default_minted_asset::<
Runtime,
pallet_assets::Instance1
>(true, initial_asset_amount);
let asset_location = MultiLocation::new(
0,
X2(PalletInstance(50), GeneralIndex(u32::from(asset_id).into()))
);
let transfer_asset: MultiAsset = (asset_location, asset_amount).into();

let assets: MultiAssets = vec![fee_asset.clone(), transfer_asset].into();
let fee_index = if assets.get(0).unwrap().eq(&fee_asset) { 0 } else { 1 };

// verify transferred successfully
let verify = Box::new(move || {
// verify native balance after transfer, decreased by transferred fee amount
// (plus transport fees)
assert!(Balances::free_balance(&who) <= balance - fee_amount);
// verify asset balance decreased by exactly transferred amount
assert_eq!(
Assets::balance(asset_id.into(), &who),
initial_asset_amount - asset_amount,
);
});
Some((assets, fee_index as u32, dest, verify))
}
}

parameter_types! {
Expand Down

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

49 changes: 49 additions & 0 deletions system-parachains/asset-hubs/asset-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,55 @@ impl_runtime_apis! {
ParentThen(Parachain(random_para_id).into()).into(),
))
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// Transfer to Relay some local AH asset (local-reserve-transfer) while paying
// fees using teleported native token.
// (We don't care that Relay doesn't accept incoming unknown AH local asset)
let dest = Parent.into();

let fee_amount = EXISTENTIAL_DEPOSIT;
let fee_asset: MultiAsset = (MultiLocation::parent(), fee_amount).into();

let who = frame_benchmarking::whitelisted_caller();
// Give some multiple of the existential deposit
let balance = fee_amount + EXISTENTIAL_DEPOSIT * 1000;
let _ = <Balances as frame_support::traits::Currency<_>>::make_free_balance_be(
&who, balance,
);
// verify initial balance
assert_eq!(Balances::free_balance(&who), balance);

// set up local asset
let asset_amount = 10u128;
let initial_asset_amount = asset_amount * 10;
let (asset_id, _, _) = pallet_assets::benchmarking::create_default_minted_asset::<
Runtime,
pallet_assets::Instance1
>(true, initial_asset_amount);
let asset_location = MultiLocation::new(
0,
X2(PalletInstance(50), GeneralIndex(u32::from(asset_id).into()))
);
let transfer_asset: MultiAsset = (asset_location, asset_amount).into();

let assets: MultiAssets = vec![fee_asset.clone(), transfer_asset].into();
let fee_index = if assets.get(0).unwrap().eq(&fee_asset) { 0 } else { 1 };

// verify transferred successfully
let verify = Box::new(move || {
// verify native balance after transfer, decreased by transferred fee amount
// (plus transport fees)
assert!(Balances::free_balance(&who) <= balance - fee_amount);
// verify asset balance decreased by exactly transferred amount
assert_eq!(
Assets::balance(asset_id.into(), &who),
initial_asset_amount - asset_amount,
);
});
Some((assets, fee_index as u32, dest, verify))
}
}

parameter_types! {
Expand Down

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

12 changes: 12 additions & 0 deletions system-parachains/bridge-hubs/bridge-hub-kusama/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,18 @@ impl_runtime_apis! {
// Reserve transfers are disabled on BH.
None
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// BH only supports teleports to system parachain.
// Relay/native token can be teleported between BH and Relay.
let native_location = Parent.into();
let dest = Parent.into();
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
native_location,
dest
)
}
}

parameter_types! {
Expand Down

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

12 changes: 12 additions & 0 deletions system-parachains/bridge-hubs/bridge-hub-polkadot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,18 @@ impl_runtime_apis! {
// Reserve transfers are disabled on BH.
None
}

fn set_up_complex_asset_transfer(
) -> Option<(MultiAssets, u32, MultiLocation, Box<dyn FnOnce()>)> {
// BH only supports teleports to system parachain.
// Relay/native token can be teleported between BH and Relay.
let native_location = Parent.into();
let dest = Parent.into();
pallet_xcm::benchmarking::helpers::native_teleport_as_asset_transfer::<Runtime>(
native_location,
dest
)
}
}

parameter_types! {
Expand Down

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

Loading

0 comments on commit 51bf27a

Please sign in to comment.