Skip to content

Commit 85b9ae1

Browse files
PanGan21github-actions[bot]raymondkfcheung
authored
Integrate asset test utilities for asset hub westend (paritytech#10721)
Related: paritytech#10694, paritytech#10515 Migrates all `exchange_asset` tests from the integration test suite to unit tests under the `AssetHubWestend` runtime module using `asset-test-utils`. Creates a reusable `exchange_asset_on_asset_hub_works` helper function reducing duplication and improving maintainability. --------- Co-authored-by: cmd[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Raymond Cheung <[email protected]>
1 parent 664643b commit 85b9ae1

File tree

4 files changed

+304
-147
lines changed

4 files changed

+304
-147
lines changed

cumulus/parachains/integration-tests/emulated/tests/assets/asset-hub-westend/src/tests/exchange_asset.rs

Lines changed: 4 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -13,149 +13,11 @@
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
1515

16-
use crate::{
17-
assets_balance_on, create_pool_with_wnd_on, foreign_balance_on,
18-
imports::{
19-
asset_hub_westend_runtime::{ExistentialDeposit, Runtime},
20-
*,
21-
},
22-
};
23-
use asset_hub_westend_runtime::{
24-
xcm_config::WestendLocation, Balances, ForeignAssets, PolkadotXcm, RuntimeOrigin,
25-
};
26-
use emulated_integration_tests_common::{accounts::ALICE, xcm_emulator::TestExt};
27-
use frame_support::{
28-
assert_err_ignore_postinfo, assert_ok,
29-
traits::fungible::{Inspect, Mutate},
30-
};
31-
use parachains_common::{AccountId, Balance};
32-
use sp_tracing::capture_test_logs;
16+
use crate::{assets_balance_on, create_pool_with_wnd_on, foreign_balance_on, imports::*};
17+
use emulated_integration_tests_common::xcm_emulator::TestExt;
18+
use frame_support::assert_ok;
3319
use std::convert::Into;
34-
use xcm::latest::{Assets, Error as XcmError, Location, Xcm};
35-
36-
const UNITS: Balance = 1_000_000_000;
37-
38-
#[test]
39-
fn exchange_asset_success() {
40-
test_exchange_asset(true, 500 * UNITS, 665 * UNITS, None);
41-
}
42-
43-
#[test]
44-
fn exchange_asset_insufficient_liquidity() {
45-
let log_capture = capture_test_logs!({
46-
test_exchange_asset(
47-
true,
48-
1_000 * UNITS,
49-
2_000 * UNITS,
50-
Some(InstructionError { index: 1, error: XcmError::NoDeal }),
51-
);
52-
});
53-
assert!(log_capture.contains("NoDeal"));
54-
}
55-
56-
#[test]
57-
fn exchange_asset_insufficient_balance() {
58-
let log_capture = capture_test_logs!({
59-
test_exchange_asset(
60-
true,
61-
5_000 * UNITS,
62-
1_665 * UNITS,
63-
Some(InstructionError { index: 0, error: XcmError::FailedToTransactAsset("") }),
64-
);
65-
});
66-
assert!(log_capture.contains("Funds are unavailable"));
67-
}
68-
69-
#[test]
70-
fn exchange_asset_pool_not_created() {
71-
test_exchange_asset(
72-
false,
73-
500 * UNITS,
74-
665 * UNITS,
75-
Some(InstructionError { index: 1, error: XcmError::NoDeal }),
76-
);
77-
}
78-
79-
fn test_exchange_asset(
80-
create_pool: bool,
81-
give_amount: Balance,
82-
want_amount: Balance,
83-
expected_error: Option<InstructionError>,
84-
) {
85-
let alice: AccountId = Westend::account_id_of(ALICE);
86-
let native_asset_location = WestendLocation::get();
87-
let native_asset_id = AssetId(native_asset_location.clone());
88-
let origin = RuntimeOrigin::signed(alice.clone());
89-
let asset_location = Location::new(1, [Parachain(2001)]);
90-
let asset_id = AssetId(asset_location.clone());
91-
92-
// Setup initial state
93-
AssetHubWestend::execute_with(|| {
94-
assert_ok!(<Balances as Mutate<_>>::mint_into(
95-
&alice,
96-
ExistentialDeposit::get() + (1_000 * UNITS)
97-
));
98-
99-
assert_ok!(ForeignAssets::force_create(
100-
RuntimeOrigin::root(),
101-
asset_location.clone().into(),
102-
alice.clone().into(),
103-
true,
104-
1
105-
));
106-
});
107-
108-
if create_pool {
109-
create_pool_with_wnd_on!(AssetHubWestend, asset_location.clone(), true, alice.clone());
110-
}
111-
112-
// Execute and verify swap
113-
AssetHubWestend::execute_with(|| {
114-
let foreign_balance_before = ForeignAssets::balance(asset_location.clone(), &alice);
115-
let wnd_balance_before = Balances::total_balance(&alice);
116-
117-
let give: Assets = (native_asset_id, give_amount).into();
118-
let want: Assets = (asset_id, want_amount).into();
119-
let xcm = Xcm(vec![
120-
WithdrawAsset(give.clone().into()),
121-
ExchangeAsset { give: give.into(), want: want.into(), maximal: true },
122-
DepositAsset { assets: Wild(All), beneficiary: alice.clone().into() },
123-
]);
124-
125-
let result = PolkadotXcm::execute(origin, bx!(xcm::VersionedXcm::from(xcm)), Weight::MAX);
126-
127-
let foreign_balance_after = ForeignAssets::balance(asset_location, &alice);
128-
let wnd_balance_after = Balances::total_balance(&alice);
129-
130-
if let Some(InstructionError { index, error }) = expected_error {
131-
assert_err_ignore_postinfo!(
132-
result,
133-
pallet_xcm::Error::<Runtime>::LocalExecutionIncompleteWithError {
134-
index,
135-
error: error.into()
136-
}
137-
);
138-
assert_eq!(
139-
foreign_balance_after, foreign_balance_before,
140-
"Foreign balance changed unexpectedly: got {foreign_balance_after}, expected {foreign_balance_before}"
141-
);
142-
assert_eq!(
143-
wnd_balance_after, wnd_balance_before,
144-
"WND balance changed unexpectedly: got {wnd_balance_after}, expected {wnd_balance_before}"
145-
);
146-
} else {
147-
assert_ok!(result);
148-
assert!(
149-
foreign_balance_after >= foreign_balance_before + want_amount,
150-
"Expected foreign balance to increase by at least {want_amount} units, got {foreign_balance_after} from {foreign_balance_before}"
151-
);
152-
assert_eq!(
153-
wnd_balance_after, wnd_balance_before - give_amount,
154-
"Expected WND balance to decrease by {give_amount} units, got {wnd_balance_after} from {wnd_balance_before}"
155-
);
156-
}
157-
});
158-
}
20+
use xcm::latest::{Location, Xcm};
15921

16022
#[test]
16123
fn exchange_asset_from_penpal_via_asset_hub_back_to_penpal() {

cumulus/parachains/runtimes/assets/asset-hub-westend/tests/tests.rs

Lines changed: 113 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ use asset_hub_westend_runtime::{
3535
};
3636
pub use asset_hub_westend_runtime::{AssetConversion, AssetDeposit, CollatorSelection, System};
3737
use asset_test_utils::{
38-
test_cases_over_bridge::TestBridgingConfig, CollatorSessionKey, CollatorSessionKeys,
39-
ExtBuilder, GovernanceOrigin, SlotDurations,
38+
test_cases::exchange_asset_on_asset_hub_works, test_cases_over_bridge::TestBridgingConfig,
39+
CollatorSessionKey, CollatorSessionKeys, ExtBuilder, GovernanceOrigin, SlotDurations,
4040
};
4141
use assets_common::local_and_foreign_assets::ForeignAssetReserveData;
4242
use codec::{Decode, Encode};
@@ -66,8 +66,10 @@ use parachains_common::{AccountId, AssetIdForTrustBackedAssets, AuraId, Balance}
6666
use sp_consensus_aura::SlotDuration;
6767
use sp_core::crypto::Ss58Codec;
6868
use sp_runtime::{traits::MaybeEquivalence, Either, MultiAddress};
69+
use sp_tracing::capture_test_logs;
6970
use std::convert::Into;
7071
use testnet_parachains_constants::westend::{consensus::*, currency::UNITS};
72+
use westend_runtime_constants::system_parachain::ASSET_HUB_ID;
7173
use xcm::{
7274
latest::{
7375
prelude::{Assets as XcmAssets, *},
@@ -1962,3 +1964,112 @@ fn expensive_erc20_runs_out_of_gas() {
19621964
.is_err());
19631965
});
19641966
}
1967+
1968+
#[test]
1969+
fn exchange_asset_success() {
1970+
exchange_asset_on_asset_hub_works::<
1971+
Runtime,
1972+
RuntimeCall,
1973+
RuntimeOrigin,
1974+
Block,
1975+
ForeignAssetsInstance,
1976+
>(
1977+
collator_session_keys(),
1978+
ASSET_HUB_ID,
1979+
AccountId::from(ALICE),
1980+
WestendLocation::get(),
1981+
true,
1982+
500 * UNITS,
1983+
665 * UNITS,
1984+
None,
1985+
);
1986+
}
1987+
1988+
#[test]
1989+
fn exchange_asset_insufficient_liquidity() {
1990+
let log_capture = capture_test_logs!({
1991+
exchange_asset_on_asset_hub_works::<
1992+
Runtime,
1993+
RuntimeCall,
1994+
RuntimeOrigin,
1995+
Block,
1996+
ForeignAssetsInstance,
1997+
>(
1998+
collator_session_keys(),
1999+
ASSET_HUB_ID,
2000+
AccountId::from(ALICE),
2001+
WestendLocation::get(),
2002+
true,
2003+
1_000 * UNITS,
2004+
2_000 * UNITS,
2005+
Some(xcm::v5::InstructionError { index: 1, error: xcm::v5::Error::NoDeal }),
2006+
);
2007+
});
2008+
assert!(log_capture.contains("NoDeal"));
2009+
}
2010+
2011+
#[test]
2012+
fn exchange_asset_insufficient_balance() {
2013+
let log_capture = capture_test_logs!({
2014+
exchange_asset_on_asset_hub_works::<
2015+
Runtime,
2016+
RuntimeCall,
2017+
RuntimeOrigin,
2018+
Block,
2019+
ForeignAssetsInstance,
2020+
>(
2021+
collator_session_keys(),
2022+
ASSET_HUB_ID,
2023+
AccountId::from(ALICE),
2024+
WestendLocation::get(),
2025+
true,
2026+
5_000 * UNITS, // This amount will be greater than initial balance
2027+
1_665 * UNITS,
2028+
Some(xcm::v5::InstructionError {
2029+
index: 0,
2030+
error: xcm::v5::Error::FailedToTransactAsset(""),
2031+
}),
2032+
);
2033+
});
2034+
assert!(log_capture.contains("Funds are unavailable"));
2035+
}
2036+
2037+
#[test]
2038+
fn exchange_asset_pool_not_created() {
2039+
exchange_asset_on_asset_hub_works::<
2040+
Runtime,
2041+
RuntimeCall,
2042+
RuntimeOrigin,
2043+
Block,
2044+
ForeignAssetsInstance,
2045+
>(
2046+
collator_session_keys(),
2047+
ASSET_HUB_ID,
2048+
AccountId::from(ALICE),
2049+
WestendLocation::get(),
2050+
false, // Pool not created
2051+
500 * UNITS,
2052+
665 * UNITS,
2053+
Some(xcm::v5::InstructionError { index: 1, error: xcm::v5::Error::NoDeal }),
2054+
);
2055+
}
2056+
2057+
#[test]
2058+
fn exchange_asset_from_penpal_via_asset_hub_back_to_penpal() {
2059+
exchange_asset_on_asset_hub_works::<
2060+
Runtime,
2061+
RuntimeCall,
2062+
RuntimeOrigin,
2063+
Block,
2064+
ForeignAssetsInstance,
2065+
>(
2066+
collator_session_keys(),
2067+
ASSET_HUB_ID,
2068+
AccountId::from(ALICE),
2069+
WestendLocation::get(),
2070+
true,
2071+
100_000_000_000u128,
2072+
1_000_000_000u128,
2073+
None,
2074+
);
2075+
}

0 commit comments

Comments
 (0)