referrerpolicy=no-referrer-when-downgrade

emulated_integration_tests_common/
dap_helpers.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use frame_support::{
17	assert_ok,
18	traits::{
19		fungible::{Inspect as FungibleInspect, Mutate as FungibleMutate},
20		Get, Hooks,
21	},
22	weights::Weight,
23};
24use parachains_common::{AccountId, Balance};
25
26use xcm_emulator::{Chain, TestExt};
27
28/// Tests that the accumulate-and-forward pallet accumulates native tokens, teleports them to
29/// the staging account of `pallet-dap` on AssetHub, and that `pallet-dap`'s `on_idle`
30/// subsequently drains and deactivates those funds into the main DAP buffer account.
31pub fn test_accumulate_forward_transfers_to_asset_hub<Sender, AH>()
32where
33	Sender: Chain + TestExt,
34	Sender::Runtime: pallet_accumulate_and_forward::Config
35		+ pallet_balances::Config<Balance = Balance>
36		+ frame_system::Config<AccountId = AccountId>,
37	Sender::RuntimeEvent: TryInto<pallet_accumulate_and_forward::Event<Sender::Runtime>>,
38	pallet_accumulate_and_forward::Pallet<Sender::Runtime>: Hooks<u32>,
39	<Sender::Runtime as pallet_accumulate_and_forward::Config>::MinTransferAmount: Get<Balance>,
40	AH: Chain + TestExt,
41	AH::Runtime: pallet_xcm::Config
42		+ pallet_dap::Config
43		+ pallet_balances::Config<Balance = Balance>
44		+ pallet_message_queue::Config
45		+ frame_system::Config<AccountId = AccountId>,
46	AH::RuntimeEvent: TryInto<pallet_message_queue::Event<AH::Runtime>>,
47	pallet_dap::Pallet<AH::Runtime>: Hooks<u32>,
48{
49	let sender_ed = <Sender::Runtime as pallet_balances::Config>::ExistentialDeposit::get();
50	let ah_ed = <AH::Runtime as pallet_balances::Config>::ExistentialDeposit::get();
51	let accumulation_account =
52		pallet_accumulate_and_forward::Pallet::<Sender::Runtime>::accumulation_account();
53	let dap_staging_account: AccountId = pallet_dap::Pallet::<AH::Runtime>::staging_account();
54	let dap_buffer_account: AccountId = pallet_dap::Pallet::<AH::Runtime>::buffer_account();
55
56	// The fund amount should slightly exceed MinTransferAmount to trigger a transfer.
57	let fund_amount =
58		<Sender::Runtime as pallet_accumulate_and_forward::Config>::MinTransferAmount::get() + 1;
59
60	// Pre-fund AH's CheckingAccount, as during testing the sender mints its own tokens rather
61	// than receiving them from AH via teleport (which would normally accrue them).
62	let check_account: AccountId =
63		AH::execute_with(|| pallet_xcm::Pallet::<AH::Runtime>::check_account());
64	AH::execute_with(|| {
65		assert_ok!(pallet_balances::Pallet::<AH::Runtime>::mint_into(
66			&check_account,
67			fund_amount + ah_ed
68		));
69	});
70
71	let (ah_total_issuance_before, ah_inactive_issuance_before, buffer_balance_before) =
72		AH::execute_with(|| {
73			(
74				pallet_balances::Pallet::<AH::Runtime>::total_issuance(),
75				pallet_balances::Pallet::<AH::Runtime>::inactive_issuance(),
76				pallet_balances::Pallet::<AH::Runtime>::balance(&dap_buffer_account),
77			)
78		});
79
80	// Fund and forward in the same block: every emulated block ends with `on_idle`, which would
81	// forward the funds before this block gets to observe it.
82	let (available_funds, sender_total_issuance_before) = Sender::execute_with(|| {
83		assert_ok!(pallet_balances::Pallet::<Sender::Runtime>::mint_into(
84			&accumulation_account,
85			sender_ed + fund_amount
86		));
87		let available_funds =
88			pallet_balances::Pallet::<Sender::Runtime>::balance(&accumulation_account) - sender_ed;
89		let total_issuance_before = pallet_balances::Pallet::<Sender::Runtime>::total_issuance();
90
91		let _ = <pallet_accumulate_and_forward::Pallet<Sender::Runtime> as Hooks<u32>>::on_idle(
92			1,
93			Weight::MAX,
94		);
95		let forward_succeeded = Sender::events().into_iter().any(|e| {
96			matches!(
97				e.try_into(),
98				Ok(pallet_accumulate_and_forward::Event::ForwardSucceeded { .. })
99			)
100		});
101		assert!(forward_succeeded, "Expected AccumulateForward::ForwardSucceeded event");
102
103		(available_funds, total_issuance_before)
104	});
105
106	// Delivery fees are waived for the accumulation account, so it retains exactly the ED.
107	let accumulation_balance_after = Sender::account_data_of(accumulation_account).free;
108	assert_eq!(accumulation_balance_after, sender_ed);
109
110	let sender_total_issuance_after =
111		Sender::execute_with(|| pallet_balances::Pallet::<Sender::Runtime>::total_issuance());
112	assert_eq!(sender_total_issuance_after, sender_total_issuance_before - available_funds);
113
114	// The XCM message is delivered to AH on the first execute_with call. Funds land in the
115	// DAP staging account; the buffer and inactive issuance are unchanged at this point.
116	let amount_received = AH::execute_with(|| {
117		let mq_processed = AH::events().into_iter().any(|e| {
118			matches!(e.try_into(), Ok(pallet_message_queue::Event::Processed { success: true, .. }))
119		});
120		assert!(mq_processed, "Expected MessageQueue::Processed(success: true) on AssetHub");
121
122		let staging_balance = pallet_balances::Pallet::<AH::Runtime>::balance(&dap_staging_account);
123		let ah_ed_balance = <AH::Runtime as pallet_balances::Config>::ExistentialDeposit::get();
124		let received = staging_balance.saturating_sub(ah_ed_balance);
125		assert!(received > 0, "DAP staging account should have received funds");
126
127		// Buffer and inactive issuance are still unchanged — drain hasn't happened yet.
128		assert_eq!(
129			pallet_balances::Pallet::<AH::Runtime>::balance(&dap_buffer_account),
130			buffer_balance_before,
131		);
132		assert_eq!(
133			pallet_balances::Pallet::<AH::Runtime>::inactive_issuance(),
134			ah_inactive_issuance_before,
135		);
136
137		// Total issuance is unchanged (teleport is burn-on-send / mint-on-receive).
138		assert_eq!(
139			pallet_balances::Pallet::<AH::Runtime>::total_issuance(),
140			ah_total_issuance_before
141		);
142
143		received
144	});
145
146	// Trigger `pallet_dap::on_idle` to drain the staging account into the buffer and
147	// deactivate the funds.
148	AH::execute_with(|| {
149		let _ = <pallet_dap::Pallet<AH::Runtime> as Hooks<u32>>::on_idle(1, Weight::MAX);
150
151		let buffer_balance_after =
152			pallet_balances::Pallet::<AH::Runtime>::balance(&dap_buffer_account);
153		assert_eq!(buffer_balance_after, buffer_balance_before + amount_received);
154
155		assert_eq!(
156			pallet_balances::Pallet::<AH::Runtime>::inactive_issuance(),
157			ah_inactive_issuance_before + amount_received
158		);
159	});
160}