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
// Copyright 2021 Centrifuge Foundation (centrifuge.io).
//
// This file is part of the Centrifuge chain project.
// Centrifuge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version (see http://www.gnu.org/licenses).
// Centrifuge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

use frame_support::traits::{
	fungibles::{Inspect, Mutate},
	Get, OnRuntimeUpgrade,
};
use sp_runtime::traits::AccountIdConversion;
#[cfg(feature = "try-runtime")]
use sp_runtime::DispatchError;
use sp_std::marker::PhantomData;

use crate::{BalanceOf, Config};

pub struct FundExistentialDeposit<T, I, Currency, ED>(PhantomData<(T, I, Currency, ED)>);

fn pallet_is_funded<T, I, Currency, ED>() -> bool
where
	T: Config<I>,
	I: 'static,
	Currency: Get<<T as Config<I>>::CurrencyId>,
	ED: Get<BalanceOf<T, I>>,
{
	let balance = <<T as Config<I>>::Currency as Inspect<T::AccountId>>::balance(
		Currency::get(),
		&<T as Config<I>>::PalletId::get().into_account_truncating(),
	);
	balance >= ED::get()
}

impl<T, I, Currency, ED> OnRuntimeUpgrade for FundExistentialDeposit<T, I, Currency, ED>
where
	T: frame_system::Config + Config<I>,
	I: 'static,
	Currency: Get<<T as Config<I>>::CurrencyId>,
	ED: Get<BalanceOf<T, I>>,
{
	#[cfg(feature = "try-runtime")]
	fn pre_upgrade() -> Result<sp_std::vec::Vec<u8>, DispatchError> {
		assert!(!pallet_is_funded::<T, I, Currency, ED>());

		log::info!("💶 Rewards: Pre funding ED checks successful");
		Ok(sp_std::vec![])
	}

	fn on_runtime_upgrade() -> frame_support::weights::Weight {
		if !pallet_is_funded::<T, I, Currency, ED>() {
			log::info!("💶 Rewards: Initiating ED funding to sovereign pallet account");
			T::Currency::mint_into(
				Currency::get(),
				&T::PalletId::get().into_account_truncating(),
				ED::get(),
			)
			.map_err(|_| log::error!("💶 Rewards: Failed to mint ED for sovereign pallet account",))
			.ok();

			T::DbWeight::get().reads_writes(1, 1)
		} else {
			log::info!(
				"💶 Rewards: ED funding for sovereign pallet account not required anymore. This probably should be removed"
			);
			T::DbWeight::get().reads_writes(1, 0)
		}
	}

	#[cfg(feature = "try-runtime")]
	fn post_upgrade(_pre_state: sp_std::vec::Vec<u8>) -> Result<(), DispatchError> {
		assert!(pallet_is_funded::<T, I, Currency, ED>());

		log::info!("💶 Rewards: Post funding ED checks successful");
		Ok(())
	}
}