pallet-revive 0.17.0

FRAME pallet for PolkaVM contracts.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
// This file is part of Substrate.

// Copyright (C) Parity Technologies (UK) Ltd.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// 	http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests for the [`PGasDeposit`] storage-deposit backend.

use crate::{
	Code, Config, DeletionQueue, FreezeReason, HoldReason, NativeDepositOf,
	deposit_payment::{Deposit, Funds},
	test_utils::{
		ALICE, BOB, CHARLIE, DJANGO_ADDR,
		builder::{BareCallBuilder, Contract},
	},
	tests::{
		Assets, AssetsFreezer, AssetsHolder, Balances, Contracts, ExtBuilder, PGAS_ASSET_ID,
		RuntimeOrigin, System, Test, builder, test_utils::get_contract_checked,
	},
};
use alloy_core::sol_types::SolCall;
use frame_support::{
	assert_ok,
	traits::{
		OnIdle,
		fungible::{Inspect as _, InspectHold, Mutate as _},
		tokens::{
			Fortitude, Precision, Preservation,
			fungibles::{
				Inspect as FungiblesInspect, InspectFreeze as FungiblesInspectFreeze,
				InspectHold as _, Mutate as FungiblesMutate,
			},
		},
	},
	weights::Weight,
};
use pallet_revive_fixtures::{
	FixtureType, MultiContributorStorage, compile_module, compile_module_with_type,
};
use pretty_assertions::assert_eq;
use sp_runtime::{AccountId32, DispatchResult};
use test_case::test_case;

/// Full observable state snapshot for a (payer, contract) pair.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
struct State {
	/// Payer's free native currency balance.
	payer_native: u128,
	/// Payer's free PGAS balance.
	payer_pgas: u128,
	/// Native currency currently held on the contract.
	contract_native_held: u128,
	/// PGAS currently held on the contract.
	contract_pgas_held: u128,
	/// `NativeDepositOf[contract][payer]`: the payer's outstanding native-currency
	native_entitlement: u128,
}

fn snapshot(payer: &AccountId32, contract: &AccountId32) -> State {
	let hold = HoldReason::StorageDepositReserve.into();
	State {
		payer_native: Balances::free_balance(payer),
		payer_pgas: Assets::balance(PGAS_ASSET_ID, payer),
		contract_native_held: Balances::balance_on_hold(&hold, contract),
		contract_pgas_held: AssetsHolder::balance_on_hold(PGAS_ASSET_ID, &hold, contract),
		native_entitlement: NativeDepositOf::<Test>::get(contract, payer),
	}
}

/// One charge with the state expected immediately afterwards.
struct Charge {
	/// Payer account.
	payer: AccountId32,
	/// Amount to charge.
	amount: u128,
	/// Expected state for the payer and contract right after the charge lands.
	expected: State,
}

/// Initial balances for one account.
struct AccountSetup {
	account: AccountId32,
	native: u128,
	pgas: u128,
}

/// A full scenario: initial balances, a sequence of charges, then one refund.
struct TestCase {
	/// Starting balances.
	accounts: Vec<AccountSetup>,
	/// Sequential charges applied to the contract account.
	charges: Vec<Charge>,
	/// Refund recipient and amount.
	refund: (AccountId32, u128),
	/// Expected states keyed by account after the refund.
	expected_after_refund: Vec<(AccountId32, State)>,
}

fn charge_and_hold(from: &AccountId32, to: &AccountId32, amount: u128) -> DispatchResult {
	<<Test as Config>::Deposit as Deposit<Test>>::charge_and_hold(
		HoldReason::StorageDepositReserve,
		Funds::Balance(from),
		to,
		amount,
	)
}

fn refund_on_hold(from: &AccountId32, to: &AccountId32, amount: u128) -> DispatchResult {
	<<Test as Config>::Deposit as Deposit<Test>>::refund_on_hold(
		HoldReason::StorageDepositReserve,
		from,
		Funds::Balance(to),
		amount,
	)
}

fn run(TestCase { accounts, charges, refund, expected_after_refund }: TestCase) {
	let pgas_balances = accounts
		.iter()
		.filter(|account| account.pgas > 0)
		.map(|account| (account.account.clone(), account.pgas))
		.collect();

	ExtBuilder::default()
		.with_pgas_balances(pgas_balances)
		.build()
		.execute_with(|| {
			for AccountSetup { account, native, .. } in accounts {
				Balances::set_balance(&account, native);
			}
			// Mint the native and PGAS ED onto the contract account, mirroring what
			// `init_contract` does at contract creation time.
			assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

			for (i, charge) in charges.iter().enumerate() {
				assert_ok!(charge_and_hold(&charge.payer, &BOB, charge.amount));
				assert_eq!(snapshot(&charge.payer, &BOB), charge.expected, "after charge {i}");
			}

			assert_ok!(refund_on_hold(&BOB, &refund.0, refund.1));
			for (payer, expected) in expected_after_refund {
				assert_eq!(snapshot(&payer, &BOB), expected, "after refund for {payer:?}");
			}
		});
}

/// Native-only: ALICE has no PGAS, so the 100-unit hold is fully backed by native currency
/// and [`NativeDepositOf`] tracks it; the refund returns the native currency.
#[test]
fn pay_native_refund_native() {
	run(TestCase {
		accounts: vec![AccountSetup { account: ALICE, native: 1_000, pgas: 0 }],
		charges: vec![Charge {
			payer: ALICE,
			amount: 100,
			expected: State {
				payer_native: 900,
				contract_native_held: 100,
				native_entitlement: 100,
				..State::default()
			},
		}],
		refund: (ALICE, 100),
		expected_after_refund: vec![(ALICE, State { payer_native: 1_000, ..State::default() })],
	});
}

/// PGAS-only: ALICE's PGAS covers the hold, so native currency is untouched and no
/// entitlement is recorded; the refund returns PGAS (at `PGasRefundPercent`).
#[test]
fn pay_pgas_refund_pgas() {
	run(TestCase {
		accounts: vec![AccountSetup { account: ALICE, native: 1_000, pgas: 1_000 }],
		charges: vec![Charge {
			payer: ALICE,
			amount: 100,
			expected: State {
				payer_native: 1_000,
				payer_pgas: 900,
				contract_pgas_held: 100,
				..State::default()
			},
		}],
		refund: (ALICE, 100),
		expected_after_refund: vec![(
			ALICE,
			State { payer_native: 1_000, payer_pgas: 910, ..State::default() },
		)],
	});
}

/// Mixed: first charge (40) fits into ALICE's 100 PGAS; second charge (80)
/// exceeds remaining PGAS (60) so falls back to native currency in full. Refund pays native
/// first (capped by the entitlement), then PGAS for the remainder.
#[test]
fn pay_mixed_refund_mixed() {
	run(TestCase {
		accounts: vec![AccountSetup { account: ALICE, native: 1_000, pgas: 100 }],
		charges: vec![
			Charge {
				payer: ALICE,
				amount: 40,
				expected: State {
					payer_native: 1_000,
					payer_pgas: 60,
					contract_pgas_held: 40,
					..State::default()
				},
			},
			Charge {
				payer: ALICE,
				amount: 80,
				expected: State {
					payer_native: 920,
					payer_pgas: 60,
					contract_native_held: 80,
					contract_pgas_held: 40,
					native_entitlement: 80,
				},
			},
		],
		refund: (ALICE, 120),
		expected_after_refund: vec![(
			ALICE,
			State { payer_native: 1_000, payer_pgas: 64, ..State::default() },
		)],
	});
}

/// Sub-ED hold: charge 50 PGAS with PGAS ED = 100, then refund — the 10% refund (5) credits
/// ALICE and the rest (45) is burned. `burn_held` should not reject the sub-ED hold.
#[test]
fn burn_held_on_sub_ed_hold_works() {
	ExtBuilder::default()
		.with_pgas_min_balance(100)
		.with_pgas_balances(vec![(ALICE, 1_000)])
		.build()
		.execute_with(|| {
			Balances::set_balance(&ALICE, 1_000);
			assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

			// PGAS branch: 50 transferred on top of the ED minted by `init_contract`.
			assert_ok!(charge_and_hold(&ALICE, &BOB, 50));
			assert_eq!(
				snapshot(&ALICE, &BOB),
				State {
					payer_native: 1_000,
					payer_pgas: 950,
					contract_pgas_held: 50,
					..State::default()
				},
				"after sub-ED charge",
			);

			// Refund: 10% of 50 = 5 goes to ALICE, 45 is burned via burn_held.
			assert_ok!(refund_on_hold(&BOB, &ALICE, 50));
			assert_eq!(
				snapshot(&ALICE, &BOB),
				State { payer_native: 1_000, payer_pgas: 955, ..State::default() },
				"after refund (5 refunded, 45 burned)",
			);
		});
}

/// Partial sub-ED refund: charge 50 PGAS (below PGAS ED = 100), refund 20. Expect 2 refunded
/// to ALICE, 18 burned, 30 still held.
#[test]
fn burn_held_on_sub_ed_hold_partial_refund() {
	ExtBuilder::default()
		.with_pgas_min_balance(100)
		.with_pgas_balances(vec![(ALICE, 1_000)])
		.build()
		.execute_with(|| {
			Balances::set_balance(&ALICE, 1_000);
			assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

			assert_ok!(charge_and_hold(&ALICE, &BOB, 50));
			assert_ok!(refund_on_hold(&BOB, &ALICE, 20));
			assert_eq!(
				snapshot(&ALICE, &BOB),
				State {
					payer_native: 1_000,
					payer_pgas: 952,
					contract_pgas_held: 30,
					..State::default()
				},
				"after partial refund (2 refunded, 18 burned, 30 still held)",
			);
		});
}

/// `init_contract` mints the native ED (deactivated) and the PGAS ED into the contract.
/// `destroy_contract` is its exact inverse: total_issuance, inactive_issuance, and active
/// issuance all return to their starting values.
#[test]
fn init_and_destroy_contract_round_trip() {
	ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
		let native_total_before = Balances::total_issuance();
		let native_inactive_before = Balances::inactive_issuance();
		let native_active_before = Balances::active_issuance();
		let pgas_total_before = Assets::total_issuance(PGAS_ASSET_ID);

		assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

		// BOB has the native ED in free balance, deactivated.
		assert_eq!(Balances::balance(&BOB), 50, "BOB should have native ED minted");
		assert_eq!(Balances::total_issuance(), native_total_before + 50);
		assert_eq!(Balances::inactive_issuance(), native_inactive_before + 50);
		assert_eq!(
			Balances::active_issuance(),
			native_active_before,
			"deactivate keeps active issuance pinned"
		);

		let pgas_ed = Assets::minimum_balance(PGAS_ASSET_ID);
		assert_eq!(Assets::balance(PGAS_ASSET_ID, &BOB), pgas_ed);
		assert_eq!(Assets::total_issuance(PGAS_ASSET_ID), pgas_total_before + pgas_ed);

		assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::destroy_contract(&BOB));

		assert_eq!(Balances::balance(&BOB), 0, "native ED has been burned out of BOB");
		assert_eq!(Assets::balance(PGAS_ASSET_ID, &BOB), 0);
		assert_eq!(Balances::total_issuance(), native_total_before);
		assert_eq!(Balances::inactive_issuance(), native_inactive_before);
		assert_eq!(Balances::active_issuance(), native_active_before);
		assert_eq!(Assets::total_issuance(PGAS_ASSET_ID), pgas_total_before);
	});
}

/// After `init_contract`, the contract has a PGAS asset account with at least the PGAS ED,
/// so a sub-ED PGAS transfer into it succeeds (would normally fail because transfers below the
/// asset's ED to a fresh account get rejected).
#[test]
fn minted_contract_can_receive_sub_ed_pgas() {
	ExtBuilder::default()
		.with_pgas_min_balance(100)
		.with_pgas_balances(vec![(ALICE, 1_000)])
		.build()
		.execute_with(|| {
			Balances::set_balance(&ALICE, 1_000);
			assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

			assert_eq!(Assets::balance(PGAS_ASSET_ID, &BOB), 100);

			assert_ok!(<Assets as FungiblesMutate<_>>::transfer(
				PGAS_ASSET_ID,
				&ALICE,
				&BOB,
				30,
				Preservation::Preserve,
			));
			assert_eq!(Assets::balance(PGAS_ASSET_ID, &BOB), 130);
			assert_eq!(Assets::balance(PGAS_ASSET_ID, &ALICE), 970);
		});
}

/// After `init_contract`, the contract has a native account too, so a sub-ED native
/// transfer into it also succeeds.
#[test]
fn minted_contract_can_receive_sub_ed_native() {
	ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
		Balances::set_balance(&ALICE, 1_000);
		assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

		assert_eq!(Balances::balance(&BOB), 50);

		assert_ok!(Balances::transfer(&ALICE, &BOB, 10, Preservation::Preserve));
		assert_eq!(Balances::balance(&BOB), 60);
		assert_eq!(Balances::balance(&ALICE), 990);
	});
}

/// The native ED minted by `init_contract` is NOT extractable while the contract has a
/// system consumer. Burning the ED directly is rejected by the underlying balance pallet
/// because `can_dec_provider` is false (consumer pinned).
#[test]
fn minted_contract_native_ed_not_extractable_with_consumer() {
	let (binary, _) = compile_module("dummy").unwrap();
	ExtBuilder::default().existential_deposit(50).build().execute_with(|| {
		Balances::set_balance(&ALICE, 1_000_000);
		let Contract { account_id, .. } =
			builder::bare_instantiate(Code::Upload(binary)).build_and_unwrap_contract();

		let before = Balances::balance(&account_id);
		let result = Balances::burn_from(
			&account_id,
			50,
			Preservation::Expendable,
			Precision::Exact,
			Fortitude::Force,
		);
		assert!(
			result.is_err(),
			"the consumer pin must keep the native ED non-extractable; got {result:?}"
		);
		assert_eq!(Balances::balance(&account_id), before, "balance unchanged");
	});
}

/// The PGAS ED minted by `init_contract` is NOT extractable: it is frozen under
/// [`FreezeReason::PGasMinBalance`]. Pallet-assets' `reducible_balance` returns
/// 0 even with the most permissive flags (`Preservation::Expendable`, `Fortitude::Force`),
/// because `untouchable = max(frozen - held, min_balance) = ED` whenever any freeze exists.
/// A direct transfer of even 1 unit is therefore rejected.
#[test]
fn minted_contract_pgas_ed_not_extractable_due_to_freeze() {
	let pgas_ed = 100u128;
	ExtBuilder::default().with_pgas_min_balance(pgas_ed).build().execute_with(|| {
		assert_ok!(<<Test as Config>::Deposit as Deposit<Test>>::init_contract(&BOB));

		// The ED sits in BOB's free balance and is fully frozen.
		assert_eq!(Assets::balance(PGAS_ASSET_ID, &BOB), pgas_ed);
		assert_eq!(
			<AssetsFreezer as FungiblesInspectFreeze<_>>::balance_frozen(
				PGAS_ASSET_ID,
				&FreezeReason::PGasMinBalance.into(),
				&BOB,
			),
			pgas_ed,
		);

		// Nothing is reducible, even under Expendable + Force.
		assert_eq!(
			<Assets as FungiblesInspect<_>>::reducible_balance(
				PGAS_ASSET_ID,
				&BOB,
				Preservation::Expendable,
				Fortitude::Force,
			),
			0,
			"the freeze pins the ED — Expendable/Force don't override it",
		);

		// A direct transfer of even 1 unit out of BOB is rejected.
		assert!(
			<Assets as FungiblesMutate<_>>::transfer(
				PGAS_ASSET_ID,
				&BOB,
				&ALICE,
				1,
				Preservation::Expendable,
			)
			.is_err(),
			"transfer of 1 unit must fail while the ED is frozen",
		);
	});
}

/// A contract whose storage was paid for by two different signers, both via the native
/// fallback path, can still be terminated. [`Deposit::refund_all`] bypasses the per-payer
/// [`NativeDepositOf`] cap (one recipient at termination, contract gone), so the full native
/// hold goes to the terminator and any PGAS hold is settled via `settle_pgas_refund`.
#[test_case(FixtureType::Solc)]
#[test_case(FixtureType::Resolc)]
fn refund_all_drains_multi_contributor_native_hold(fixture_type: FixtureType) {
	let (code, _) = compile_module_with_type("MultiContributorStorage", fixture_type).unwrap();
	ExtBuilder::default().build().execute_with(|| {
		Balances::set_balance(&ALICE, 100_000_000_000);
		Balances::set_balance(&CHARLIE, 100_000_000_000);

		let Contract { addr, account_id } =
			builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

		assert_ok!(
			builder::bare_call(addr)
				.data(MultiContributorStorage::growStorageCall {}.abi_encode())
				.build()
				.result,
		);
		assert_ok!(
			BareCallBuilder::<Test>::bare_call(RuntimeOrigin::signed(CHARLIE), addr)
				.data(MultiContributorStorage::growStorageCall {}.abi_encode())
				.build()
				.result,
		);

		let alice_entry = NativeDepositOf::<Test>::get(&account_id, &ALICE);
		let charlie_entry = NativeDepositOf::<Test>::get(&account_id, &CHARLIE);
		assert!(alice_entry > 0);
		assert!(charlie_entry > 0);

		let hold: <Test as Config>::RuntimeHoldReason = HoldReason::StorageDepositReserve.into();
		let native_held = Balances::balance_on_hold(&hold, &account_id);
		let pgas_held = AssetsHolder::balance_on_hold(PGAS_ASSET_ID, &hold, &account_id);
		assert_eq!(pgas_held, 0, "every charge fell back to native");
		assert_eq!(native_held, alice_entry + charlie_entry);

		let alice_before = Balances::balance(&ALICE);
		assert_ok!(
			builder::bare_call(addr)
				.data(
					MultiContributorStorage::terminateCall { beneficiary: DJANGO_ADDR.0.into() }
						.abi_encode(),
				)
				.build()
				.result,
		);
		let alice_after = Balances::balance(&ALICE);

		assert!(get_contract_checked(&addr).is_none(), "contract should be gone");
		assert_eq!(
			Balances::balance_on_hold(&hold, &account_id),
			0,
			"the full multi-contributor native hold has been released",
		);
		// ALICE receives the full storage-deposit hold (her own + CHARLIE's). The actual delta
		// also picks up the code-upload deposit refund and any tx-level deposit accounting,
		// so it is at least `native_held`.
		assert!(
			alice_after.saturating_sub(alice_before) >= native_held,
			"expected ALICE balance delta >= {}, got {}",
			native_held,
			alice_after.saturating_sub(alice_before),
		);
	});
}

/// Terminating a contract reaps its system account (native and PGAS EDs are burned by
/// `destroy_contract`, the manual consumer is decremented), and the `on_idle` deletion-queue
/// drain clears its [`NativeDepositOf`] rows. We charge a multi-contributor native deposit
/// first so the double map is genuinely populated and we can observe both rows disappear.
#[test_case(FixtureType::Solc)]
#[test_case(FixtureType::Resolc)]
fn destroy_contract_reaps_account_and_clears_native_deposit_map(fixture_type: FixtureType) {
	let (code, _) = compile_module_with_type("MultiContributorStorage", fixture_type).unwrap();
	ExtBuilder::default().build().execute_with(|| {
		Balances::set_balance(&ALICE, 100_000_000_000);
		Balances::set_balance(&CHARLIE, 100_000_000_000);

		let Contract { addr, account_id } =
			builder::bare_instantiate(Code::Upload(code)).build_and_unwrap_contract();

		// Two distinct payers grow distinct slots so that `NativeDepositOf[contract][_]` has
		// two rows once the deletion queue starts draining.
		assert_ok!(
			builder::bare_call(addr)
				.data(MultiContributorStorage::growStorageCall {}.abi_encode())
				.build()
				.result,
		);
		assert_ok!(
			BareCallBuilder::<Test>::bare_call(RuntimeOrigin::signed(CHARLIE), addr)
				.data(MultiContributorStorage::growStorageCall {}.abi_encode())
				.build()
				.result,
		);

		assert!(NativeDepositOf::<Test>::get(&account_id, &ALICE) > 0);
		assert!(NativeDepositOf::<Test>::get(&account_id, &CHARLIE) > 0);
		assert!(System::account_exists(&account_id), "contract account is alive pre-terminate");

		assert_ok!(
			builder::bare_call(addr)
				.data(
					MultiContributorStorage::terminateCall { beneficiary: DJANGO_ADDR.0.into() }
						.abi_encode(),
				)
				.build()
				.result,
		);

		assert!(get_contract_checked(&addr).is_none(), "contract info should be gone");
		assert!(
			!System::account_exists(&account_id),
			"system account should be reaped once destroy_contract burns the EDs",
		);
		assert_eq!(Balances::balance(&account_id), 0);
		assert_eq!(Assets::balance(PGAS_ASSET_ID, &account_id), 0);

		// `NativeDepositOf` rows survive termination; they're cleared lazily by `on_idle`.
		assert!(NativeDepositOf::<Test>::get(&account_id, &ALICE) > 0);
		assert!(NativeDepositOf::<Test>::get(&account_id, &CHARLIE) > 0);
		assert_eq!(DeletionQueue::<Test>::iter().count(), 1, "contract is queued for deletion");

		Contracts::on_idle(System::block_number(), Weight::MAX);

		assert_eq!(
			DeletionQueue::<Test>::iter().count(),
			0,
			"deletion queue drained to completion",
		);
		assert_eq!(NativeDepositOf::<Test>::iter_prefix(&account_id).count(), 0);
	});
}

/// Refunding a recipient with no [`NativeDepositOf`] credit on a contract whose deposit was
/// paid in native must not revert: the PGAS settlement path is capped at the (empty) PGAS hold
/// and is a no-op, leaving the original payer's native hold intact.
#[test]
fn refund_to_user_without_entitlement_does_not_revert() {
	let after_charge = State {
		payer_native: 900,
		contract_native_held: 100,
		native_entitlement: 100,
		..State::default()
	};
	run(TestCase {
		accounts: vec![AccountSetup { account: ALICE, native: 1_000, pgas: 0 }],
		charges: vec![Charge { payer: ALICE, amount: 100, expected: after_charge }],
		refund: (CHARLIE, 80),
		expected_after_refund: vec![(ALICE, after_charge)],
	});
}

/// Mixed native/PGAS holds must not revert when a PGAS-routed refund request exceeds the
/// contract's PGAS hold. PGAS settlement is capped to the PGAS actually held, and unrelated
/// native entitlements stay with their original contributor.
#[test]
fn mixed_native_pgas_refund_caps_pgas_without_reverting() {
	run(TestCase {
		accounts: vec![
			AccountSetup { account: ALICE, native: 1_000, pgas: 0 },
			AccountSetup { account: CHARLIE, native: 1_000, pgas: 1_000 },
		],
		charges: vec![
			Charge {
				payer: ALICE,
				amount: 100,
				expected: State {
					payer_native: 900,
					contract_native_held: 100,
					native_entitlement: 100,
					..State::default()
				},
			},
			Charge {
				payer: CHARLIE,
				amount: 40,
				expected: State {
					payer_native: 1_000,
					payer_pgas: 960,
					contract_native_held: 100,
					contract_pgas_held: 40,
					..State::default()
				},
			},
		],
		refund: (CHARLIE, 80),
		expected_after_refund: vec![
			(
				ALICE,
				State {
					payer_native: 900,
					contract_native_held: 100,
					native_entitlement: 100,
					..State::default()
				},
			),
			(
				CHARLIE,
				State {
					payer_native: 1_000,
					// CHARLIE pays 40 PGAS, then receives a 10% refund on the capped 40 PGAS
					// settlement: 1_000 - 40 + 4.
					payer_pgas: 964,
					contract_native_held: 100,
					..State::default()
				},
			),
		],
	});
}

/// Code upload and removal with the uploader holding only PGAS.
/// Exercises [`crate::vm::ContractBlob::store_code`] / [`crate::vm::ContractBlob::remove`]
/// against the [`PGasDeposit`] backend. Surfaces any issue from the pallet account not
/// having a pre-existing PGAS asset account.
#[test]
fn code_upload_and_remove_with_pgas() {
	let (binary, code_hash) = compile_module("dummy").unwrap();
	ExtBuilder::default()
		.with_pgas_balances(vec![(ALICE, 10_000_000)])
		.build()
		.execute_with(|| {
			Balances::set_balance(&ALICE, 0);
			let pallet_account = crate::Pallet::<Test>::account_id();

			assert_ok!(Contracts::upload_code(
				RuntimeOrigin::signed(ALICE),
				binary,
				crate::test_utils::deposit_limit::<Test>(),
			));

			let info = crate::CodeInfoOf::<Test>::get(&code_hash).unwrap();
			let deposit = info.deposit();

			assert_eq!(
				AssetsHolder::balance_on_hold(
					PGAS_ASSET_ID,
					&HoldReason::CodeUploadDepositReserve.into(),
					&pallet_account,
				),
				deposit,
				"deposit held in PGAS on the pallet account",
			);
			assert_eq!(
				NativeDepositOf::<Test>::get(&pallet_account, &ALICE),
				0,
				"PGAS path does not record a native entitlement",
			);

			let pgas_before_remove = Assets::balance(PGAS_ASSET_ID, &ALICE);
			assert_ok!(Contracts::remove_code(RuntimeOrigin::signed(ALICE), code_hash));
			let pgas_after_remove = Assets::balance(PGAS_ASSET_ID, &ALICE);

			let refund_pct = crate::tests::PGasRefundPercent::get();
			let expected_refund = refund_pct.mul_floor(deposit);
			assert_eq!(
				pgas_after_remove - pgas_before_remove,
				expected_refund,
				"PGAS partial refund credited to uploader",
			);
			assert_eq!(
				AssetsHolder::balance_on_hold(
					PGAS_ASSET_ID,
					&HoldReason::CodeUploadDepositReserve.into(),
					&pallet_account,
				),
				0,
				"hold released",
			);
		});
}