Skip to content

Commit

Permalink
add pool helper macro
Browse files Browse the repository at this point in the history
  • Loading branch information
acatangiu committed Oct 22, 2024
1 parent 9f1895c commit 6737a77
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,7 @@ mod imports {
BridgeHubWestendParaReceiver as BridgeHubWestendReceiver,
CollectivesWestendPara as CollectivesWestend, PenpalAPara as PenpalA,
PenpalAParaReceiver as PenpalAReceiver, PenpalAParaSender as PenpalASender,
PenpalBPara as PenpalB, PenpalBParaReceiver as PenpalBReceiver,
PenpalBParaSender as PenpalBSender, WestendRelay as Westend,
PenpalBPara as PenpalB, PenpalBParaReceiver as PenpalBReceiver, WestendRelay as Westend,
WestendRelayReceiver as WestendReceiver, WestendRelaySender as WestendSender,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,67 @@ macro_rules! foreign_balance_on {
}
};
}

#[macro_export]
macro_rules! create_pool_with_wnd_on {
( $chain:ident, $asset_id:expr, $is_foreign:expr, $asset_owner:expr ) => {
emulated_integration_tests_common::impls::paste::paste! {
<$chain>::execute_with(|| {
type RuntimeEvent = <$chain as Chain>::RuntimeEvent;
let owner = $asset_owner;
let signed_owner = <$chain as Chain>::RuntimeOrigin::signed(owner.clone());
let wnd_location: Location = Parent.into();
if $is_foreign {
assert_ok!(<$chain as [<$chain Pallet>]>::ForeignAssets::mint(
signed_owner.clone(),
$asset_id.clone().into(),
owner.clone().into(),
10_000_000_000_000, // For it to have more than enough.
));
} else {
let asset_id = match $asset_id.interior.last() {
Some(GeneralIndex(id)) => *id as u32,
_ => unreachable!(),
};
assert_ok!(<$chain as [<$chain Pallet>]>::Assets::mint(
signed_owner.clone(),
asset_id.into(),
owner.clone().into(),
10_000_000_000_000, // For it to have more than enough.
));
}

assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::create_pool(
signed_owner.clone(),
Box::new(wnd_location.clone()),
Box::new($asset_id.clone()),
));

assert_expected_events!(
$chain,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);

assert_ok!(<$chain as [<$chain Pallet>]>::AssetConversion::add_liquidity(
signed_owner,
Box::new(wnd_location),
Box::new($asset_id),
1_000_000_000_000,
2_000_000_000_000, // $asset_id is worth half of wnd
0,
0,
owner.into()
));

assert_expected_events!(
$chain,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
]
);
});
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::{foreign_balance_on, imports::*};
use crate::{create_pool_with_wnd_on, foreign_balance_on, imports::*};
use frame_support::traits::tokens::fungibles::Mutate;
use xcm::DoubleEncoded;
use xcm_builder::{DescribeAllTerminal, DescribeFamily, HashedDescription};
Expand Down Expand Up @@ -102,153 +102,25 @@ fn transact_from_para_to_para_through_asset_hub() {
]);

// Prefund USDT to sov account of sender.
let usdt_id = 1984;
let usdt_id = 1984u32;
AssetHubWestend::execute_with(|| {
type Assets = <AssetHubWestend as AssetHubWestendPallet>::Assets;
assert_ok!(<Assets as Mutate<_>>::mint_into(
usdt_id.into(),
usdt_id,
&sov_of_sender_on_asset_hub.clone().into(),
fee_amount_to_send,
));
});

// We create a pool between WND and USDT in AssetHub.
let native_asset: Location = Parent.into();
let usdt = Location::new(0, [PalletInstance(ASSETS_PALLET_ID), GeneralIndex(usdt_id.into())]);

// set up pool with USDT <> native pair
AssetHubWestend::execute_with(|| {
type RuntimeEvent = <AssetHubWestend as Chain>::RuntimeEvent;

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::Assets::mint(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
usdt_id.into(),
AssetHubWestendSender::get().into(),
10_000_000_000_000, // For it to have more than enough.
));

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::create_pool(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
Box::new(native_asset.clone()),
Box::new(usdt.clone()),
));

assert_expected_events!(
AssetHubWestend,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);

assert_ok!(<AssetHubWestend as AssetHubWestendPallet>::AssetConversion::add_liquidity(
<AssetHubWestend as Chain>::RuntimeOrigin::signed(AssetHubWestendSender::get()),
Box::new(native_asset),
Box::new(usdt),
1_000_000_000_000,
2_000_000_000_000, // usdt is worth half of `native_asset`
0,
0,
AssetHubWestendSender::get().into()
));

assert_expected_events!(
AssetHubWestend,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
]
);
});

let usdt_from_asset_hub = PenpalUsdtFromAssetHub::get();

create_pool_with_wnd_on!(AssetHubWestend, usdt, false, AssetHubWestendSender::get());
// We also need a pool between WND and USDT on PenpalA.
PenpalA::execute_with(|| {
type RuntimeEvent = <PenpalA as Chain>::RuntimeEvent;
let relay_asset = RelayLocation::get();

assert_ok!(<PenpalA as PenpalAPallet>::ForeignAssets::mint(
<PenpalA as Chain>::RuntimeOrigin::signed(PenpalAssetOwner::get()),
usdt_from_asset_hub.clone().into(),
PenpalASender::get().into(),
10_000_000_000_000, // For it to have more than enough.
));

assert_ok!(<PenpalA as PenpalAPallet>::AssetConversion::create_pool(
<PenpalA as Chain>::RuntimeOrigin::signed(PenpalASender::get()),
Box::new(relay_asset.clone()),
Box::new(usdt_from_asset_hub.clone()),
));

assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);

assert_ok!(<PenpalA as PenpalAPallet>::AssetConversion::add_liquidity(
<PenpalA as Chain>::RuntimeOrigin::signed(PenpalASender::get()),
Box::new(relay_asset),
Box::new(usdt_from_asset_hub.clone()),
1_000_000_000_000,
2_000_000_000_000, // `usdt_from_asset_hub` is worth half of `relay_asset`
0,
0,
PenpalASender::get().into()
));

assert_expected_events!(
PenpalA,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
]
);
});

create_pool_with_wnd_on!(PenpalA, PenpalUsdtFromAssetHub::get(), true, PenpalAssetOwner::get());
// We also need a pool between WND and USDT on PenpalB.
PenpalB::execute_with(|| {
type RuntimeEvent = <PenpalB as Chain>::RuntimeEvent;
let relay_asset = RelayLocation::get();

assert_ok!(<PenpalB as PenpalBPallet>::ForeignAssets::mint(
<PenpalB as Chain>::RuntimeOrigin::signed(PenpalAssetOwner::get()),
usdt_from_asset_hub.clone().into(),
PenpalBSender::get().into(),
10_000_000_000_000, // For it to have more than enough.
));

assert_ok!(<PenpalB as PenpalBPallet>::AssetConversion::create_pool(
<PenpalB as Chain>::RuntimeOrigin::signed(PenpalBSender::get()),
Box::new(relay_asset.clone()),
Box::new(usdt_from_asset_hub.clone()),
));

assert_expected_events!(
PenpalB,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::PoolCreated { .. }) => {},
]
);

assert_ok!(<PenpalB as PenpalBPallet>::AssetConversion::add_liquidity(
<PenpalB as Chain>::RuntimeOrigin::signed(PenpalBSender::get()),
Box::new(relay_asset),
Box::new(usdt_from_asset_hub.clone()),
1_000_000_000_000,
2_000_000_000_000, // `usdt_from_asset_hub` is worth half of `relay_asset`
0,
0,
PenpalBSender::get().into()
));

assert_expected_events!(
PenpalB,
vec![
RuntimeEvent::AssetConversion(pallet_asset_conversion::Event::LiquidityAdded { .. }) => {},
]
);
});
create_pool_with_wnd_on!(PenpalB, PenpalUsdtFromAssetHub::get(), true, PenpalAssetOwner::get());

let usdt_from_asset_hub = PenpalUsdtFromAssetHub::get();
PenpalA::execute_with(|| {
type ForeignAssets = <PenpalA as PenpalAPallet>::ForeignAssets;
assert_ok!(<ForeignAssets as Mutate<_>>::mint_into(
Expand Down

0 comments on commit 6737a77

Please sign in to comment.