Skip to content
Snippets Groups Projects

add pallet treasury

Merged Éloïs requested to merge elois-treasury into master
Files
2
@@ -49,6 +49,7 @@ pub mod pallet {
frame_system::Config<AccountData = AccountData<Self::Balance>>
+ pallet_balances::Config
+ pallet_provide_randomness::Config<Currency = pallet_balances::Pallet<Self>>
+ pallet_treasury::Config<Currency = pallet_balances::Pallet<Self>>
{
type AccountIdToSalt: Convert<Self::AccountId, [u8; 32]>;
/// The overarching event type.
@@ -89,6 +90,16 @@ pub mod pallet {
#[pallet::genesis_build]
impl<T: Config> GenesisBuild<T> for GenesisConfig<T> {
fn build(&self) {
// Treasury
frame_system::Account::<T>::mutate(
pallet_treasury::Pallet::<T>::account_id(),
|account| {
account.data.random_id = None;
account.data.free = T::ExistentialDeposit::get();
account.providers = 1;
},
);
// Classic accounts
for (
account_id,
GenesisAccountData {
@@ -118,12 +129,16 @@ pub mod pallet {
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Force the destruction of an account because its free balance is insufficient to pay
/// the account creation price.
/// [who, balance]
ForceDestroy {
who: T::AccountId,
balance: T::Balance,
},
/// Random id assigned
/// [account_id, random_id]
RandomIdAssigned {
account_id: T::AccountId,
random_id: H256,
},
RandomIdAssigned { who: T::AccountId, random_id: H256 },
}
// HOOKS //
@@ -145,47 +160,58 @@ pub mod pallet {
total_weight += 100_000;
} else {
// If the account is not self-sufficient, it must pay the account creation fees
frame_system::Pallet::<T>::inc_providers(&account_id);
let res = T::Currency::withdraw(
&account_id,
T::NewAccountPrice::get(),
frame_support::traits::WithdrawReasons::FEE,
ExistenceRequirement::KeepAlive,
);
if let Ok(imbalance) = res {
// The fees have been succesfully collected, we should:
// 1. Increment consumers to prevent the destruction of the account before
let account_data = frame_system::Pallet::<T>::get(&account_id);
let price = T::NewAccountPrice::get();
if account_data.free > price {
// The account can pay the new account price, we should:
// 1. Increment providers to create the account for frame_system point of view
// 2. Withdraw the "new account price" amount
// 3. Increment consumers to prevent the destruction of the account before
// the random id is assigned
// 2. Manage the funds collected
// 3. Submit random id generation request
// 4. Save the id of the random generation request.
let res =
frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id);
// 4. Manage the funds collected
// 5. Submit random id generation request
// 6. Save the id of the random generation request.
frame_system::Pallet::<T>::inc_providers(&account_id);
let res = <pallet_balances::Pallet<T> as Currency<T::AccountId>>::withdraw(
&account_id,
price,
frame_support::traits::WithdrawReasons::FEE,
ExistenceRequirement::KeepAlive,
);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
T::OnUnbalanced::on_unbalanced(imbalance);
let request_id = pallet_provide_randomness::Pallet::<T>::force_request(
pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo,
H256(T::AccountIdToSalt::convert(account_id.clone())),
"Cannot fail because we checked that the free balance was sufficient"
);
PendingRandomIdAssignments::<T>::insert(request_id, account_id);
total_weight += 200_000;
if let Ok(imbalance) = res {
let res =
frame_system::Pallet::<T>::inc_consumers_without_limit(&account_id);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
T::OnUnbalanced::on_unbalanced(imbalance);
let request_id = pallet_provide_randomness::Pallet::<T>::force_request(
pallet_provide_randomness::RandomnessType::RandomnessFromTwoEpochsAgo,
H256(T::AccountIdToSalt::convert(account_id.clone())),
);
PendingRandomIdAssignments::<T>::insert(request_id, account_id);
total_weight += 200_000;
}
} else {
// The charges could not be deducted, we slash the account
let res = frame_system::Pallet::<T>::dec_providers(&account_id);
debug_assert!(
res.is_ok(),
"Cannot fail because providers are incremented just before"
);
let account_data = frame_system::Pallet::<T>::get(&account_id);
let (imbalance, rest) = pallet_balances::Pallet::<T>::slash(
&account_id,
account_data.free.saturating_add(account_data.reserved),
);
debug_assert!(rest.is_zero());
T::OnUnbalanced::on_unbalanced(imbalance);
let balance_to_suppr =
account_data.free.saturating_add(account_data.reserved);
// Force account data supression
frame_system::Account::<T>::mutate(&account_id, |a| {
a.data.set_balances(Default::default())
});
Self::deposit_event(Event::ForceDestroy {
who: account_id,
balance: balance_to_suppr,
});
T::OnUnbalanced::on_unbalanced(pallet_balances::NegativeImbalance::new(
balance_to_suppr,
));
total_weight += 300_000;
}
}
@@ -206,7 +232,7 @@ where
account.data.random_id = Some(randomness);
});
Self::deposit_event(Event::RandomIdAssigned {
account_id,
who: account_id,
random_id: randomness,
});
200_000
Loading