Skip to content

Commit

Permalink
add integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acatangiu committed Oct 2, 2024
1 parent 91794c7 commit adb4ac2
Show file tree
Hide file tree
Showing 7 changed files with 394 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ use sp_core::{sr25519, storage::Storage};
// Cumulus
use emulated_integration_tests_common::{
accounts, build_genesis_storage, collators, get_account_id_from_seed,
PenpalSiblingSovereignAccount, PenpalTeleportableAssetLocation, RESERVABLE_ASSET_ID,
PenpalASiblingSovereignAccount, PenpalATeleportableAssetLocation,
PenpalBSiblingSovereignAccount, PenpalBTeleportableAssetLocation, RESERVABLE_ASSET_ID,
SAFE_XCM_VERSION, USDT_ID,
};
use parachains_common::{AccountId, Balance};
Expand Down Expand Up @@ -77,10 +78,17 @@ pub fn genesis() -> Storage {
},
foreign_assets: asset_hub_rococo_runtime::ForeignAssetsConfig {
assets: vec![
// Penpal's teleportable asset representation
// PenpalA's teleportable asset representation
(
PenpalTeleportableAssetLocation::get(),
PenpalSiblingSovereignAccount::get(),
PenpalATeleportableAssetLocation::get(),
PenpalASiblingSovereignAccount::get(),
false,
ED,
),
// PenpalB's teleportable asset representation
(
PenpalBTeleportableAssetLocation::get(),
PenpalBSiblingSovereignAccount::get(),
false,
ED,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use sp_core::{sr25519, storage::Storage};
// Cumulus
use emulated_integration_tests_common::{
accounts, build_genesis_storage, collators, get_account_id_from_seed,
PenpalBSiblingSovereignAccount, PenpalBTeleportableAssetLocation,
PenpalSiblingSovereignAccount, PenpalTeleportableAssetLocation, RESERVABLE_ASSET_ID,
PenpalASiblingSovereignAccount, PenpalATeleportableAssetLocation,
PenpalBSiblingSovereignAccount, PenpalBTeleportableAssetLocation, RESERVABLE_ASSET_ID,
SAFE_XCM_VERSION, USDT_ID,
};
use parachains_common::{AccountId, Balance};
Expand Down Expand Up @@ -75,10 +75,10 @@ pub fn genesis() -> Storage {
},
foreign_assets: asset_hub_westend_runtime::ForeignAssetsConfig {
assets: vec![
// Penpal's teleportable asset representation
// PenpalA's teleportable asset representation
(
PenpalTeleportableAssetLocation::get(),
PenpalSiblingSovereignAccount::get(),
PenpalATeleportableAssetLocation::get(),
PenpalASiblingSovereignAccount::get(),
false,
ED,
),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,26 @@ pub const TELEPORTABLE_ASSET_ID: u32 = 2;
// USDT registered on AH as (trust-backed) Asset and reserve-transferred between Parachain and AH
pub const USDT_ID: u32 = 1984;

pub const PENPAL_ID: u32 = 2000;
pub const PENPAL_A_ID: u32 = 2000;
pub const PENPAL_B_ID: u32 = 2001;
pub const ASSETS_PALLET_ID: u8 = 50;

parameter_types! {
pub PenpalTeleportableAssetLocation: xcm::v5::Location
pub PenpalATeleportableAssetLocation: xcm::v5::Location
= xcm::v5::Location::new(1, [
xcm::v5::Junction::Parachain(PENPAL_ID),
xcm::v5::Junction::Parachain(PENPAL_A_ID),
xcm::v5::Junction::PalletInstance(ASSETS_PALLET_ID),
xcm::v5::Junction::GeneralIndex(TELEPORTABLE_ASSET_ID.into()),
]
);
pub PenpalSiblingSovereignAccount: AccountId = Sibling::from(PENPAL_ID).into_account_truncating();
pub PenpalBTeleportableAssetLocation: xcm::v5::Location
= xcm::v5::Location::new(1, [
xcm::v5::Junction::Parachain(PENPAL_B_ID),
xcm::v5::Junction::PalletInstance(ASSETS_PALLET_ID),
xcm::v5::Junction::GeneralIndex(TELEPORTABLE_ASSET_ID.into()),
]
);
pub PenpalASiblingSovereignAccount: AccountId = Sibling::from(PENPAL_A_ID).into_account_truncating();
pub PenpalBSiblingSovereignAccount: AccountId = Sibling::from(PENPAL_B_ID).into_account_truncating();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
imports::*,
tests::teleport::do_bidirectional_teleport_foreign_assets_between_para_and_asset_hub_using_xt,
};
use xcm::latest::AssetTransferFilter;

fn para_to_para_assethub_hop_assertions(t: ParaToParaThroughAHTest) {
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent;
Expand Down Expand Up @@ -819,3 +820,70 @@ fn transfer_native_asset_from_relay_to_para_through_asset_hub() {
// should be non-zero
assert!(receiver_assets_after < receiver_assets_before + amount_to_send);
}

// ==============================================================================================
// ==== Bidirectional Transfer - Native + Teleportable Foreign Assets - Parachain<->AssetHub ====
// ==============================================================================================
/// Transfers of native asset plus teleportable foreign asset from Parachain to AssetHub and back
/// with fees paid using native asset.
#[test]
fn poc_new_instructions_test() {
fn execute_xcm_penpal_to_asset_hub(t: ParaToSystemParaTest) -> DispatchResult {
// let transport_fees: Asset = (Parent, 40_000_000_000u128).into();
let all_assets = t.args.assets.clone().into_inner();
let mut assets = all_assets.clone();
let fees = assets.remove(t.args.fee_asset_item as usize);
// xcm to be executed at dest
let xcm_on_dest =
Xcm(vec![DepositAsset { assets: Wild(All), beneficiary: t.args.beneficiary }]);
let xcm = Xcm::<()>(vec![
// WithdrawAsset(transport_fees.into()),
WithdrawAsset(all_assets.into()),
// TODO: replace this with `PayFees` when available
SetFeesMode { jit_withdraw: true },
InitiateTransfer {
destination: t.args.dest,
remote_fees: Some(AssetTransferFilter::ReserveWithdraw(fees.into())),
assets: vec![AssetTransferFilter::Teleport(assets.into())],
remote_xcm: xcm_on_dest,
},
]);
<PenpalA as PenpalAPallet>::PolkadotXcm::execute(
t.signed_origin,
bx!(xcm::VersionedXcm::V5(xcm.into())),
Weight::MAX,
)
.unwrap();
Ok(())
}
fn execute_xcm_asset_hub_to_penpal(t: SystemParaToParaTest) -> DispatchResult {
let all_assets = t.args.assets.clone().into_inner();
let mut assets = all_assets.clone();
let fees = assets.remove(t.args.fee_asset_item as usize);
// xcm to be executed at dest
let xcm_on_dest =
Xcm(vec![DepositAsset { assets: Wild(All), beneficiary: t.args.beneficiary }]);
let xcm = Xcm::<()>(vec![
WithdrawAsset(all_assets.into()),
// TODO: replace this with `PayFees` when available
SetFeesMode { jit_withdraw: true },
InitiateTransfer {
destination: t.args.dest,
remote_fees: Some(AssetTransferFilter::ReserveDeposit(fees.into())),
assets: vec![AssetTransferFilter::Teleport(assets.into())],
remote_xcm: xcm_on_dest,
},
]);
<AssetHubWestend as AssetHubWestendPallet>::PolkadotXcm::execute(
t.signed_origin,
bx!(xcm::VersionedXcm::V5(xcm.into())),
Weight::MAX,
)
.unwrap();
Ok(())
}
do_bidirectional_teleport_foreign_assets_between_para_and_asset_hub_using_xt(
execute_xcm_penpal_to_asset_hub,
execute_xcm_asset_hub_to_penpal,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ fn ah_to_penpal_foreign_assets_sender_assertions(t: SystemParaToParaTest) {
assert_expected_events!(
AssetHubWestend,
vec![
// native asset used for fees is transferred to Parachain's Sovereign account as reserve
RuntimeEvent::Balances(
pallet_balances::Event::Transfer { from, to, amount }
) => {
from: *from == t.sender.account_id,
to: *to == AssetHubWestend::sovereign_account_id_of(
t.args.dest.clone()
),
amount: *amount == t.args.amount,
},
// foreign asset is burned locally as part of teleportation
RuntimeEvent::ForeignAssets(pallet_assets::Event::Burned { asset_id, owner, balance }) => {
asset_id: *asset_id == expected_foreign_asset_id,
Expand Down Expand Up @@ -283,13 +273,13 @@ pub fn do_bidirectional_teleport_foreign_assets_between_para_and_asset_hub_using
ah_to_para_dispatchable: fn(SystemParaToParaTest) -> DispatchResult,
) {
// Init values for Parachain
let fee_amount_to_send: Balance = ASSET_HUB_WESTEND_ED * 100;
let fee_amount_to_send: Balance = ASSET_HUB_WESTEND_ED * 1000;
let asset_location_on_penpal = PenpalLocalTeleportableToAssetHub::get();
let asset_id_on_penpal = match asset_location_on_penpal.last() {
Some(Junction::GeneralIndex(id)) => *id as u32,
_ => unreachable!(),
};
let asset_amount_to_send = ASSET_HUB_WESTEND_ED * 100;
let asset_amount_to_send = ASSET_HUB_WESTEND_ED * 1000;
let asset_owner = PenpalAssetOwner::get();
let system_para_native_asset_location = RelayLocation::get();
let sender = PenpalASender::get();
Expand Down Expand Up @@ -318,7 +308,7 @@ pub fn do_bidirectional_teleport_foreign_assets_between_para_and_asset_hub_using
<PenpalA as Chain>::RuntimeOrigin::signed(asset_owner.clone()),
asset_id_on_penpal,
sender.clone(),
asset_amount_to_send,
asset_amount_to_send * 2,
);
// fund Parachain's check account to be able to teleport
PenpalA::fund_accounts(vec![(
Expand Down Expand Up @@ -433,6 +423,10 @@ pub fn do_bidirectional_teleport_foreign_assets_between_para_and_asset_hub_using
));
});

// Only send back half the amount.
let asset_amount_to_send = asset_amount_to_send / 2;
let fee_amount_to_send = fee_amount_to_send / 2;

let ah_to_penpal_beneficiary_id = PenpalAReceiver::get();
let penpal_as_seen_by_ah = AssetHubWestend::sibling_location_of(PenpalA::para_id());
let ah_assets: Assets = vec![
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ mod imports {
penpal_emulated_chain::{
penpal_runtime::xcm_config::{
CustomizableAssetFromSystemAssetHub as PenpalCustomizableAssetFromSystemAssetHub,
LocalTeleportableToAssetHub as PenpalLocalTeleportableToAssetHub,
UniversalLocation as PenpalUniversalLocation,
},
PenpalAssetOwner, PenpalBParaPallet as PenpalBPallet,
Expand Down
Loading

0 comments on commit adb4ac2

Please sign in to comment.